Class: NLPBackpack::POS::BrillTagger

Inherits:
Object
  • Object
show all
Defined in:
lib/nlp_backpack/pos/brill_tagger.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBrillTagger

Place corpus into memory



17
18
19
20
21
22
23
# File 'lib/nlp_backpack/pos/brill_tagger.rb', line 17

def initialize
  @lexicons = Hash.new {|hash, k| hash[k] = []}
  File.open(lexicon_path, 'r').each do |line|
    line = line.split
    @lexicons[line.shift] = line
  end
end

Class Method Details

.analyze(sentence) ⇒ Object



11
12
13
# File 'lib/nlp_backpack/pos/brill_tagger.rb', line 11

def analyze(sentence)
  new.tag(sentence)
end

Instance Method Details

#tag(text) ⇒ Object

TODO: per sentence



26
27
28
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
57
58
# File 'lib/nlp_backpack/pos/brill_tagger.rb', line 26

def tag(text)
  @text = Tokenizer::Word.tokenize(text)

  @pos = []
  @text.each do |word|
    if @lexicons.key?(word) || @lexicons.key?(word.downcase)
      @pos << @lexicons[word][0]
    else
      @pos << "NN"
    end
  end

  # Apply Transformational rules
  @pos.each_index do |index|
    rule_one(index)
    rule_two(index)
    rule_three(index)
    rule_four(index)
    rule_five(index)
    rule_six(index)
    rule_seven(index)
    rule_eight(index)
    rule_nine(index)
  end

  # Organize [word, pos]
  results = POSArray.new
  @text.each_with_index do |word, i|
    results << [word, @pos[i]]
  end

  results
end