Sha256: 868edce3c7207e346e4ecfb2d7505473266b8db36d3e3da96e1aa601dcc9aac3

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require "attentive/version"
require "attentive/config"


module Attentive
  extend Attentive::Config

  # Default configuration
  self.invocations = ["@me".freeze]

  # Default contexts that listeners will require
  # a message to be heard in.
  self.default_required_contexts = %i{conversation}

  # Default contexts in which listeners will ignore messages.
  self.default_prohibited_contexts = %i{quotation}



  # Recognizes entities in a phrase
  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
      entities.each do |entity|
        match = Attentive::Matcher.new(entity, Cursor.new(message, i)).match!
        next unless match

        i = match.replace_with(entity)
        break
      end
      i += 1
    end
    message.tokens.to_s
  end

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



  # Attentive DSL

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

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

  # Matches a message against all listeners
  # and returns an array of matches
  def hear(message, params={})
    message = Attentive::Message.new(message, params) unless message.is_a?(Attentive::Message)
    listeners.hear message
  end

  # Matches a message against all listeners
  # and invokes the first listener that mathes
  def hear!(message, params={})
    hear(message, params).each do |match|
      match.listener.call(match)
      return
    end
  end

end

require "attentive/listener_collection"
require "attentive/message"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attentive-0.3.0 lib/attentive.rb