Class: Zenlish::Lex::Lexicon

Inherits:
Object
  • Object
show all
Defined in:
lib/zenlish/lex/lexicon.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLexicon

Returns a new instance of Lexicon.



18
19
20
21
22
23
# File 'lib/zenlish/lex/lexicon.rb', line 18

def initialize
  @entries = []
  @lemma2entry = {}
  @terminals = []
  @name2terminal = {}
end

Instance Attribute Details

#entriesArray<Lex::LexicalEntry> (readonly)

Returns entries in the lexicon.

Returns:



7
8
9
# File 'lib/zenlish/lex/lexicon.rb', line 7

def entries
  @entries
end

#lemma2entryHash{String => Lex::LexicalEntry} (readonly)

Returns the lexical entry for the given lemma.

Returns:



10
11
12
# File 'lib/zenlish/lex/lexicon.rb', line 10

def lemma2entry
  @lemma2entry
end

#name2terminalObject (readonly)

Returns the value of attribute name2terminal.



16
17
18
# File 'lib/zenlish/lex/lexicon.rb', line 16

def name2terminal
  @name2terminal
end

#terminalsObject (readonly)

The list of terminal symbols. Examples of terminal symbols:

  • word classes,
  • punctuation signs,...


15
16
17
# File 'lib/zenlish/lex/lexicon.rb', line 15

def terminals
  @terminals
end

Instance Method Details

#add_entry(anEntry) ⇒ Object

Parameters:



65
66
67
68
69
70
# File 'lib/zenlish/lex/lexicon.rb', line 65

def add_entry(anEntry)
  entries << anEntry
  lemma = anEntry.lemma

  update_mapping(lemma2entry, lemma, anEntry)
end

#add_terminal(aTerminal) ⇒ Object

Parameters:

  • aTerminal (Rley::Syntax::Terminal)


59
60
61
62
# File 'lib/zenlish/lex/lexicon.rb', line 59

def add_terminal(aTerminal)
  terminals << aTerminal
  name2terminal[aTerminal.name] = aTerminal
end

#get_lexeme(aLemma, aWordClass = nil) ⇒ Lex::Lexeme

Parameters:

  • aLemma (String)

    retrieve the lexeme form the given "head word".

  • aWordClass (WordClasses::WordClass, NilClass) (defaults to: nil)

    the word class of the lexeme.

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zenlish/lex/lexicon.rb', line 29

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