Module: Attentive

Extended by:
Config
Defined in:
lib/attentive.rb,
lib/attentive/trie.rb,
lib/attentive/match.rb,
lib/attentive/token.rb,
lib/attentive/config.rb,
lib/attentive/cursor.rb,
lib/attentive/entity.rb,
lib/attentive/errors.rb,
lib/attentive/phrase.rb,
lib/attentive/tokens.rb,
lib/attentive/matcher.rb,
lib/attentive/message.rb,
lib/attentive/version.rb,
lib/attentive/duration.rb,
lib/attentive/listener.rb,
lib/attentive/tokenizer.rb,
lib/attentive/tokens/word.rb,
lib/attentive/tokens/emoji.rb,
lib/attentive/substitutions.rb,
lib/attentive/tokens/any_of.rb,
lib/attentive/tokens/regexp.rb,
lib/attentive/composite_entity.rb,
lib/attentive/tokens/invocation.rb,
lib/attentive/tokens/whitespace.rb,
lib/attentive/tokens/punctuation.rb,
lib/attentive/listener_collection.rb

Defined Under Namespace

Modules: Config, Tokens Classes: CompositeEntity, Cursor, Duration, Entity, Listener, ListenerCollection, Match, Matcher, Message, Phrase, StringToken, Token, Tokenizer, Trie, UndefinedEntityError

Constant Summary collapse

VERSION =
"0.3.9"
SUBSTITUTIONS =
{"ain't"=>["am not"], "aren't"=>["are not"], "can't"=>["can not"], "cannot"=>["can not"], "could've"=>["could have"], "couldn't"=>["could not"], "didn't"=>["did not"], "doesn't"=>["does not"], "don't"=>["do not"], "hadn't"=>["had not"], "hasn't"=>["has not"], "haven't"=>["have not"], "he'd"=>["he had", "he would"], "he'll"=>["he will", "he shall"], "he's"=>["he is", "he has"], "he'sn't"=>["he is not", "he has not"], "how'd"=>["how did", "how would"], "how'll"=>["how will"], "how's"=>["how is", "how has", "how does"], "i'd"=>["i would", "i had"], "i'll"=>["i shall", "i will"], "i'm"=>["i am"], "i've"=>["i have"], "i'ven't"=>["i have not"], "isn't"=>["is not"], "it'd"=>["it would", "it had"], "it'll"=>["it will", "it shall"], "it's"=>["it is", "it has"], "it'sn't"=>["it is not", "it has not"], "let's"=>["let us"], "ma'am"=>["madam"], "mightn't"=>["might not"], "might've"=>["might have"], "mustn't"=>["must not"], "must've"=>["must have"], "needn't"=>["need not"], "not've"=>["not have"], "o'clock"=>["of the clock"], "oughtn't"=>["ought not"], "shan't"=>["shall not"], "she'd"=>["she had", "she would"], "she'll"=>["she shall", "she will"], "she's"=>["she is", "she has"], "she'sn't"=>["she is not", "she has not"], "should've"=>["should have"], "shouldn't"=>["should not"], "somebody'd"=>["somebody had", "somebody would"], "somebody'dn't've"=>["somebody would not have"], "somebody'll"=>["somebody shall", "somebody will"], "somebody's"=>["somebody is", "somebody has"], "someone'd"=>["someone had", "someone would"], "someone'll"=>["someone shall", "someone will"], "someone's"=>["someone is", "someone has"], "something'd"=>["something had", "something would"], "something'll"=>["something shall", "something will"], "something's"=>["something is", "something has"], "that'll"=>["that will"], "that's"=>["that is", "that has"], "there'd"=>["there had", "there would"], "there're"=>["there are"], "there's"=>["there is", "there has"], "they'd"=>["they would", "they had"], "they'dn't"=>["they would not"], "they'll"=>["they shall", "they will"], "they'lln't've"=>["they will not have"], "they're"=>["they are"], "they've"=>["they have"], "they'ven't"=>["they have not"], "'tis"=>["it is"], "'twas"=>["it was"], "wasn't"=>["was not"], "we'd"=>["we had", "we would"], "we'dn't've"=>["we would not have"], "we'll"=>["we will"], "we'lln't've"=>["we will not have"], "we're"=>["we are"], "we've"=>["we have"], "weren't"=>["were not"], "what'll"=>["what shall", "what will"], "what're"=>["what are"], "what's"=>["what is", "what does", "what has"], "what've"=>["what have"], "when's"=>["when is", "when has"], "where'd"=>["where did"], "where's"=>["where is", "where does", "where has"], "where've"=>["where have"], "who'd"=>["who would", "who had"], "who'll"=>["who shall", "who will"], "who're"=>["who are"], "who's"=>["who is", "who has"], "who've"=>["who have"], "why'll"=>["why will"], "why're"=>["why are"], "why's"=>["why is", "why has"], "won't"=>["will not"], "would've"=>["would have"], "wouldn't"=>["would not"], "y'all"=>["you all"], "you'd"=>["you had", "you would"], "you'll"=>["you shall", "you will"], "you're"=>["you are"], "you'ren't"=>["you are not"], "you've"=>["you have"], "you'ven't"=>["you have not"], "bye"=>["goodbye"], "gonna"=>["going to"], "hi"=>["hello"], "ol'"=>["old"], "'sup"=>["what is up"], "thanks"=>["thank you"], "wanna"=>["want to"], "w/o"=>["without"], "mon"=>["monday"], "tue"=>["tuesday"], "tues"=>["tuesday"], "wed"=>["wednesday"], "thu"=>["thursday"], "thur"=>["thursday"], "thurs"=>["thursday"], "fri"=>["friday"], "sat"=>["saturday"], "sun"=>["sunday"], "jan"=>["january"], "feb"=>["february"], "mar"=>["march"], "apr"=>["april"], "jun"=>["june"], "jul"=>["july"], "aug"=>["august"], "sep"=>["september"], "sept"=>["september"], "oct"=>["october"], "nov"=>["november"], "dec"=>["december"]}.each_with_object({}) do |(key, values), new_hash|
  tokens = Attentive.tokenize(key, substitutions: false)
  possibilities = values.map { |value| Attentive.tokenize(value, substitutions: false) }
  value = possibilities.length == 1 ? possibilities[0] : Attentive::Phrase.new([Attentive::Tokens::AnyOf.new(key, possibilities, 0)])
  new_hash[tokens] = value
end.freeze

Instance Attribute Summary

Attributes included from Config

#default_prohibited_contexts, #default_required_contexts, #invocations

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config

substitutions

Class Method Details

.abstract(message) ⇒ Object

Recognizes entities in a phrase



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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 = {}) ⇒ Object

Shorthand for tokenizer



46
47
48
# File 'lib/attentive.rb', line 46

def self.tokenize(message, options={})
  Attentive::Tokenizer.tokenize(message, options)
end

Instance Method Details

#hear(message, params = {}) ⇒ Object

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



64
65
66
67
# 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 = {}) ⇒ Object

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



71
72
73
74
75
76
# 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) ⇒ Object



58
59
60
# File 'lib/attentive.rb', line 58

def listen_for(*args, &block)
  listeners.listen_for(*args, &block)
end

#listenersObject

Attentive DSL



54
55
56
# File 'lib/attentive.rb', line 54

def listeners
  @listeners ||= Attentive::ListenerCollection.new
end