Class: Attentive::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/attentive/matcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phrase, message, params = {}) ⇒ Matcher

Returns a new instance of Matcher.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/attentive/matcher.rb', line 13

def initialize(phrase, message, params={})
  @phrase = phrase
  @match_start = message.pos
  @cursor = Cursor.new(phrase, params.fetch(:pos, 0))
  @message = message
  self.message.pop while self.message.peek.whitespace?
  @match_params = params.merge(message: message.message, match_start: message.pos)
  @match_data = {}
  @state = :matching

  cursor.pop while cursor.peek.whitespace?
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



5
6
7
# File 'lib/attentive/matcher.rb', line 5

def cursor
  @cursor
end

#messageObject (readonly)

Returns the value of attribute message.



5
6
7
# File 'lib/attentive/matcher.rb', line 5

def message
  @message
end

#phraseObject (readonly)

Returns the value of attribute phrase.



5
6
7
# File 'lib/attentive/matcher.rb', line 5

def phrase
  @phrase
end

Class Method Details

.match!(phrase, message) ⇒ Object



7
8
9
10
11
# File 'lib/attentive/matcher.rb', line 7

def self.match!(phrase, message)
  phrase = Attentive::Tokenizer.tokenize(phrase, entities: true) unless phrase.is_a?(Attentive::Phrase)
  message = Attentive::Message.new(message) unless message.is_a?(Attentive::Message)
  self.new(phrase, Attentive::Cursor.new(message)).match!
end

Instance Method Details

#match!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/attentive/matcher.rb', line 38

def match!
  until (token = message.peek).eof?
    if token.ambiguous?
      unless match_any!(token.possibilities)
        @state = :mismatch
        break
      end
      message.pop
      cursor.pop while cursor.peek.whitespace?

    elsif match_data = cursor.peek.matches?(message)
      @match_data.merge!(match_data) unless match_data == true
      cursor.pop
      cursor.pop while cursor.peek.whitespace?
      @state = :found

      # -> This is the one spot where we instantiate a Match
      return Attentive::Match.new(phrase, @match_params.merge(
        match_end: message.pos,
        match_data: @match_data)) if cursor.eof?

    elsif token.skippable?
      message.pop

    else
      @state = :mismatch
      break
    end

    message.pop while message.peek.whitespace?
  end

  nil
end

#match_any!(messages) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/attentive/matcher.rb', line 73

def match_any!(messages)
  messages.each do |message|
    matcher = Matcher.new(phrase[pos..-1], Cursor.new(message))
    matcher.match!
    unless matcher.mismatch?
      cursor.advance matcher.pos
      return true
    end
  end

  false
end

#matching?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/attentive/matcher.rb', line 30

def matching?
  @state == :matching
end

#mismatch?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/attentive/matcher.rb', line 34

def mismatch?
  @state == :mismatch
end

#posObject



26
27
28
# File 'lib/attentive/matcher.rb', line 26

def pos
  cursor.pos
end