module Attentive

Constants

SUBSTITUTIONS
VERSION

Public Class Methods

abstract(message) click to toggle source

Recognizes entities in a phrase

# File lib/attentive.rb, line 21
def self.abstract(message)
  message = Attentive::Message.new(message)
  entities = Attentive::Entity.entities.map { |entity| Attentive::Phrase.new([entity.new]) }
  i = 0
  while i < message.tokens.length
    abstractions = entities.each_with_object({}) do |entity, abstractions|
      match = Attentive::Matcher.new(entity, Cursor.new(message, i)).match!
      abstractions[entity[0].name.to_s] = { entity: entity, match: match } if match
    end

    # Pick the most abstract entity: if we match both
    # {{core.date}} and {{core.date.past}}, use {{core.date}}
    if abstractions.any?
      keys = abstractions.keys
      keys.reject! { |key| keys.any? { |other_key| key != other_key && key.start_with?(other_key) } }
      abstraction = abstractions[keys.first]
      i = abstraction[:match].replace_with(abstraction[:entity])
    end

    i += 1
  end
  message.tokens.to_s
end
tokenize(message, options={}) click to toggle source

Shorthand for tokenizer

# File lib/attentive.rb, line 46
def self.tokenize(message, options={})
  Attentive::Tokenizer.tokenize(message, options)
end

Public Instance Methods

hear(message, params={}) click to toggle source

Matches a message against all listeners and returns an array of matches

# File lib/attentive.rb, line 64
def hear(message, params={})
  message = Attentive::Message.new(message, params) unless message.is_a?(Attentive::Message)
  listeners.hear message
end
hear!(message, params={}) click to toggle source

Matches a message against all listeners and invokes the first listener that mathes

# File lib/attentive.rb, line 71
def hear!(message, params={})
  hear(message, params).each do |match|
    match.listener.call(match)
    return
  end
end
listen_for(*args, &block) click to toggle source
# File lib/attentive.rb, line 58
def listen_for(*args, &block)
  listeners.listen_for(*args, &block)
end
listeners() click to toggle source

Attentive DSL

# File lib/attentive.rb, line 54
def listeners
  @listeners ||= Attentive::ListenerCollection.new
end