Class: Myaso::Lexicon

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/myaso/lexicon.rb

Overview

A pretty useful representation of a lexicon in the following form: `word_prefix -> word -> tags`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLexicon

An instance of a n-gram storage is initialized by zero counts.


15
16
17
18
19
# File 'lib/myaso/lexicon.rb', line 15

def initialize
  @table = Hash.new do |h, k|
    h[k] = Hash.new { |h, k| h[k] = Hash.new(0) }
  end
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table


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

def table
  @table
end

Instance Method Details

#==(other) ⇒ Object

Two lexicons are equal iff they tables are equal.


45
46
47
# File 'lib/myaso/lexicon.rb', line 45

def == other
  self.table == other.table
end

#[](word, tag = nil) ⇒ Object

Obtain the count of the specified word and tag.


23
24
25
26
27
# File 'lib/myaso/lexicon.rb', line 23

def [] word, tag = nil
  return 0 unless table.include? prefix(word)
  return 0 unless table[prefix(word)].include? word
  table[prefix(word)][word][tag]
end

#[]=(word, tag = nil, count) ⇒ Object

Assign the count to the specified word and tag.


31
32
33
34
# File 'lib/myaso/lexicon.rb', line 31

def []= word, tag = nil, count
  @tags = nil
  table[prefix(word)][word][tag] = count
end

#tags(word = nil) ⇒ Object

Retrieve global tags or tags of the given word.


38
39
40
41
# File 'lib/myaso/lexicon.rb', line 38

def tags(word = nil)
  return lazy_aggregated_tags unless word
  table[prefix(word)][word].keys.compact
end