Class: Myaso::Tagger

Inherits:
Object
  • Object
show all
Defined in:
lib/myaso/tagger.rb

Overview

This class is an implementation of the Viterbi algorithm.

Defined Under Namespace

Classes: Model, TnT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Tagger

An instance of Tagger should be initialized with an instance of trained HMM.


11
12
13
# File 'lib/myaso/tagger.rb', line 11

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model


6
7
8
# File 'lib/myaso/tagger.rb', line 6

def model
  @model
end

Instance Method Details

#annotate(sentence) ⇒ Object

Viterbi algorithm itself. Return tags that input sentence should be annotated.


18
19
20
21
22
23
# File 'lib/myaso/tagger.rb', line 18

def annotate(sentence)
  return [] if sentence.size == 0
  sentence = sentence.map { |w| model.classify(w) }
  sentence.unshift(model.start_symbol, model.start_symbol)
  backward(sentence, *forward(sentence))
end