class Lingo::Language::Dictionary

Public Class Methods

new(config, lingo) click to toggle source
# File lib/lingo/language/dictionary.rb, line 38
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
open(*args) { |dictionary = new(*args)| ... } click to toggle source
# File lib/lingo/language/dictionary.rb, line 32
def self.open(*args)
  yield dictionary = new(*args)
ensure
  dictionary.close if dictionary
end

Public Instance Methods

close() click to toggle source
# File lib/lingo/language/dictionary.rb, line 62
def close
  @src.each { |i| i.close }
end
each_affix(str, affix = :suffix) { |"#{$`}#{e == '*' ? '' : e}#{$'}", t| ... } click to toggle source
# File lib/lingo/language/dictionary.rb, line 114
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) click to toggle source
# File lib/lingo/language/dictionary.rb, line 70
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) click to toggle source
# File lib/lingo/language/dictionary.rb, line 66
def find_word(str, token = nil)
  Word.new(str, WA_UNKNOWN, token).identify(select_with_suffix(str))
end
select(str, lex = []) { |lex| ... } click to toggle source
# File lib/lingo/language/dictionary.rb, line 84
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) click to toggle source
# File lib/lingo/language/dictionary.rb, line 108
def select_with_infix(str)
  select(str) { |lex|
    each_affix(str, :infix) { |form, _| select(form, lex) }
  }
end
select_with_suffix(str) click to toggle source
# File lib/lingo/language/dictionary.rb, line 94
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