Class: UtteranceParser::Example

Inherits:
Utterance show all
Defined in:
lib/utterance_parser/example.rb

Overview

An example utterance for training the parser. Can be labeled with entities via XML tags: ‘Play some <category>rap</category>`.

Constant Summary

Constants inherited from Utterance

Utterance::TAG_RE

Instance Attribute Summary collapse

Attributes inherited from Utterance

#text

Instance Method Summary collapse

Methods inherited from Utterance

#pos_tokens

Constructor Details

#initialize(labeled_text, intent) ⇒ Example

Returns a new instance of Example.



7
8
9
10
11
# File 'lib/utterance_parser/example.rb', line 7

def initialize(labeled_text, intent)
  super(labeled_text.gsub(TAG_RE, '\2'))
  @labeled_text = labeled_text
  @intent = intent
end

Instance Attribute Details

#intentObject (readonly)

Returns the value of attribute intent.



5
6
7
# File 'lib/utterance_parser/example.rb', line 5

def intent
  @intent
end

#labeled_textObject (readonly)

Returns the value of attribute labeled_text.



5
6
7
# File 'lib/utterance_parser/example.rb', line 5

def labeled_text
  @labeled_text
end

Instance Method Details

#==(other) ⇒ Object



13
14
15
# File 'lib/utterance_parser/example.rb', line 13

def ==(other)
  other.class == self.class && other.labeled_text == @labeled_text
end

#labeled_tokensObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/utterance_parser/example.rb', line 17

def labeled_tokens
  labels = tags_with_position(@labeled_text)

  tags_with_position(PosTagger.add_tags(@text)).map do |tag, word, word_position|
    label = labels.detect do |name, content, label_position|
      # If the word position intersect with the label's, it's a match
      if label_position.include? word_position.begin
        break name
      end
    end
    [word, tag.upcase, label]
  end
end