Class: Lingo::Language::Dictionary
- Inherits:
-
Object
- Object
- Lingo::Language::Dictionary
- Defined in:
- lib/lingo/language/dictionary.rb
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #each_affix(str, affix = :suffix) ⇒ Object
- #find_synonyms(obj, syn = [], com = true) ⇒ Object
- #find_word(str, token = nil) ⇒ Object
-
#initialize(config, lingo) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #select(str, lex = []) ⇒ Object
- #select_with_infix(str) ⇒ Object
- #select_with_suffix(str) ⇒ Object
Constructor Details
#initialize(config, lingo) ⇒ Dictionary
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/lingo/language/dictionary.rb', line 39 def initialize(config, lingo) unless config.key?('source') raise ArgumentError, "Required parameter `source' missing." end @suffixes, @infixes = [], [] Array(lingo.dictionary_config['suffix']).each { |t, s| t.downcase! a = t == 'f' ? @infixes : @suffixes s.split.each { |r| f, e = r.split('/') a << [/#{f}$/i, e || '*', t] } } @src = config['source'].map { |src| lingo.lexical_hash(src) } @all = config['mode'].nil? || config['mode'].downcase == 'all' lingo.dictionaries << self end |
Class Method Details
.open(*args) ⇒ Object
33 34 35 36 37 |
# File 'lib/lingo/language/dictionary.rb', line 33 def self.open(*args) yield dictionary = new(*args) ensure dictionary.close if dictionary end |
Instance Method Details
#close ⇒ Object
63 64 65 |
# File 'lib/lingo/language/dictionary.rb', line 63 def close @src.each { |i| i.close } end |
#each_affix(str, affix = :suffix) ⇒ Object
115 116 117 118 119 |
# File 'lib/lingo/language/dictionary.rb', line 115 def each_affix(str, affix = :suffix) instance_variable_get("@#{affix}es").each { |r, e, t| yield "#{$`}#{e == '*' ? '' : e}#{$'}", t if str =~ r } end |
#find_synonyms(obj, syn = [], com = true) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/lingo/language/dictionary.rb', line 71 def find_synonyms(obj, syn = [], com = true) lex = obj.lexicals lex = [obj] if lex.empty? && obj.unknown? com &&= obj.attr == WA_COMPOUND lex.each { |l| select(l.form, syn) unless com && l.attr != LA_COMPOUND || l.attr == LA_SYNONYM } syn end |
#find_word(str, token = nil) ⇒ Object
67 68 69 |
# File 'lib/lingo/language/dictionary.rb', line 67 def find_word(str, token = nil) Word.new(str, WA_UNKNOWN, token).identify(select_with_suffix(str)) end |
#select(str, lex = []) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/lingo/language/dictionary.rb', line 85 def select(str, lex = []) @src.each { |src| lex.concat(src[str] || next) break unless @all } lex.empty? && block_given? ? yield(lex) : lex.uniq! lex end |
#select_with_infix(str) ⇒ Object
109 110 111 112 113 |
# File 'lib/lingo/language/dictionary.rb', line 109 def select_with_infix(str) select(str) { |lex| each_affix(str, :infix) { |form, _| select(form, lex) } } end |
#select_with_suffix(str) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/lingo/language/dictionary.rb', line 95 def select_with_suffix(str) select(str) { |lex| each_affix(str) { |form, attr| unless (selected = select(form)).empty? if selected.first.attr == LA_COMPOUND lex.concat(selected) if selected.last.attr?(attr) else selected.each { |l| lex << l if l.attr?(attr) } end end } } end |