class Zenlish::Lex::Lexicon

A lexicon is a collection of lexical entries. Every entry is associated with one one more lexemes.

Attributes

entries[R]

@return [Array<Lex::LexicalEntry>] entries in the lexicon

lemma2entry[R]

@return [Hash{String => Lex::LexicalEntry}] the lexical entry for the given lemma.

name2terminal[R]
terminals[R]

The list of terminal symbols. Examples of terminal symbols:

  • word classes,

  • punctuation signs,…

Public Class Methods

new() click to toggle source
# File lib/zenlish/lex/lexicon.rb, line 20
def initialize
  @entries = []
  @lemma2entry = {}
  @terminals = []
  @name2terminal = {}
end

Public Instance Methods

add_entry(anEntry) click to toggle source

@param anEntry [Lex::LexicalEntry]

# File lib/zenlish/lex/lexicon.rb, line 67
def add_entry(anEntry)
  entries << anEntry
  lemma = anEntry.lemma

  update_mapping(lemma2entry, lemma, anEntry)
end
add_terminal(aTerminal) click to toggle source

@param aTerminal [Rley::Syntax::Terminal]

# File lib/zenlish/lex/lexicon.rb, line 61
def add_terminal(aTerminal)
  terminals << aTerminal
  name2terminal[aTerminal.name] = aTerminal
end
get_lexeme(aLemma, aWordClass = nil) click to toggle source

@param aLemma retrieve the lexeme form the given “head word”. @param aWordClass [WordClasses::WordClass, NilClass] the word class of

the lexeme.

@return [Lex::Lexeme]

# File lib/zenlish/lex/lexicon.rb, line 31
def get_lexeme(aLemma, aWordClass = nil)
  if aWordClass
    lexeme = nil
    candidate = nil

    entries = lemma2entry.fetch(aLemma)
    if entries.kind_of?(Array)
      entries.each do |e|
        candidate = e.lexemes.first
        break if candidate.wclass.kind_of?(aWordClass)
      end
      lexeme = candidate
    else
      candidate = entries.lexemes.first
      lexeme = candidate if candidate.wclass.kind_of?(aWordClass)
    end

    lexeme
  else
    entry = lemma2entry.fetch(aLemma)
    if entry.kind_of?(Array)
      err_msg = "Multiple lexemes for #{aLemma}"
      raise StandardError, err_msg
    else
      entry.lexemes.first
    end
  end
end

Private Instance Methods

update_mapping(aHash, aKey, aValue) click to toggle source
# File lib/zenlish/lex/lexicon.rb, line 76
def update_mapping(aHash, aKey, aValue)
  if aHash.include?(aKey)
    hit = aHash[aKey]
    if hit.is_a?(Array)
      hit << aValue
    else
      aHash[aKey] = [hit, aValue]
    end
  else
    aHash[aKey] = aValue
  end
end