Class: Lingo::Language::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/lingo/language/dictionary.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, lingo) ⇒ Dictionary

Returns a new instance of 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

#closeObject



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



130
131
132
133
134
# File 'lib/lingo/language/dictionary.rb', line 130

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



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lingo/language/dictionary.rb', line 75

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) ⇒ Object

dic.find_word( aString ) -> aNewWord

Erstellt aus dem String ein Wort und sucht nach diesem im Wörterbuch.



70
71
72
73
# File 'lib/lingo/language/dictionary.rb', line 70

def find_word(str)
  (@_word ||= {})[str] ||=
    Word.new(str, WA_UNKNOWN).identify(select_with_suffix(str))
end

#select(str, lex = []) ⇒ Object

dic.select( aString ) -> ArrayOfLexicals

Sucht alle Wörterbücher durch und gibt den ersten Treffer zurück (+mode = first+), oder alle Treffer (+mode = all+)



92
93
94
95
96
97
98
99
100
# File 'lib/lingo/language/dictionary.rb', line 92

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

dic.select_with_infix( aString ) -> ArrayOfLexicals

Sucht alle Wörterbücher durch und gibt den ersten Treffer zurück (+mode = first+), oder alle Treffer (+mode = all+). Sucht dabei auch Wörter, die eine Fugung am Ende haben.



124
125
126
127
128
# File 'lib/lingo/language/dictionary.rb', line 124

def select_with_infix(str)
  select(str) { |lex|
    each_affix(str, :infix) { |form, _| select(form, lex) }
  }
end

#select_with_suffix(str) ⇒ Object

dic.select_with_suffix( aString ) -> ArrayOfLexicals

Sucht alle Wörterbücher durch und gibt den ersten Treffer zurück (+mode = first+), oder alle Treffer (+mode = all+). Sucht dabei auch Wörter, die um wortklassenspezifische Suffixe bereinigt wurden.



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/lingo/language/dictionary.rb', line 106

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