Class: Lingo::Language::Dictionary

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

Constant Summary

KEY_REF_RE =
%r{\A#{Database::KEY_REF_ESC}\d+}

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Dictionary) initialize(config, lingo)

A new instance of Dictionary



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lingo/language/dictionary.rb', line 41

def initialize(config, lingo)
  unless config.has_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

+ (Object) open(*args)



35
36
37
38
39
# File 'lib/lingo/language/dictionary.rb', line 35

def self.open(*args)
  yield dictionary = new(*args)
ensure
  dictionary.close if dictionary
end

Instance Method Details

- (Object) close



65
66
67
# File 'lib/lingo/language/dictionary.rb', line 65

def close
  @src.each { |i| i.close }
end

- (Object) find_synonyms(obj, syn = [])



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lingo/language/dictionary.rb', line 81

def find_synonyms(obj, syn = [])
  lex = obj.lexicals
  lex = [obj] if lex.empty? && obj.unknown?

  com, ref = obj.attr == WA_COMPOUND, KEY_REF_RE

  lex.each { |l|
    select(l.form, syn) { |i| i =~ ref } unless com &&
      l.attr != LA_COMPOUND || l.attr == LA_SYNONYM
  }

  syn
end

- (Object) find_word(str)

dic.find_word( aString ) -> aNewWord

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



72
73
74
75
76
77
78
79
# File 'lib/lingo/language/dictionary.rb', line 72

def find_word(str)
  (@_word ||= {})[str] ||= Word.new(str, WA_UNKNOWN).tap { |w|
    unless (lexicals = select_with_suffix(str)).empty?
      w.lexicals = lexicals
      w.attr = WA_IDENTIFIED
    end
  }
end

- (Object) infix_lexicals(str)

dic.gap_lexicals( aString ) -> ArrayOfLexicals

Gibt alle möglichen Lexicals zurück, die von der Endung her auf den String anwendbar sind:



139
140
141
# File 'lib/lingo/language/dictionary.rb', line 139

def infix_lexicals(str)
  affix_lexicals(:infix, str)
end

- (Object) select(str, lex = [])

dic.select( aString ) -> ArrayOfLexicals

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



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/lingo/language/dictionary.rb', line 98

def select(str, lex = [])
  @src.each { |src|
    l = src[str] or next
    lex.concat(block_given? ? l.delete_if { |i| yield i } : l)
    break unless @all
  }

  lex.sort!
  lex.uniq!

  lex
end

- (Object) select_with_infix(str)

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.



123
124
125
# File 'lib/lingo/language/dictionary.rb', line 123

def select_with_infix(str)
  select_with_affix(:infix, str)
end

- (Object) select_with_suffix(str)

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.



115
116
117
# File 'lib/lingo/language/dictionary.rb', line 115

def select_with_suffix(str)
  select_with_affix(:suffix, str)
end

- (Object) suffix_lexicals(str)

dic.suffix_lexicals( aString ) -> ArrayOfLexicals

Gibt alle möglichen Lexicals zurück, die von der Endung her auf den String anwendbar sind:

dic.suffix_lexicals(“Hasens”) -> [(hasen/s), (hasen/e), (has/e)]



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

def suffix_lexicals(str)
  affix_lexicals(:suffix, str)
end