Module: Raingrams::OpenVocabulary::OpenModel

Included in:
BigramModel, HexagramModel, Model, PentagramModel, QuadgramModel, TrigramModel
Defined in:
lib/raingrams/open_vocabulary/open_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lexiconObject (readonly)

The fixed lexicon of this model



8
9
10
# File 'lib/raingrams/open_vocabulary/open_model.rb', line 8

def lexicon
  @lexicon
end

Instance Method Details

#initialize(options = {}, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/raingrams/open_vocabulary/open_model.rb', line 10

def initialize(options={},&block)
  @lexicon = (options[:lexicon] || [])

  @lexicon.map! do |word|
    word.to_gram
  end

  super(options,&block)
end

#train_ngram(ngram) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/raingrams/open_vocabulary/open_model.rb', line 24

def train_ngram(ngram)
  ngram = ngram.map do |gram|
    if within_lexicon?(gram)
      gram
    else
      Tokens::Unknown
    end
  end

  return super(ngram)
end

#within_lexicon?(gram) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/raingrams/open_vocabulary/open_model.rb', line 20

def within_lexicon?(gram)
  @lexicon.include?(gram.to_gram)
end