Sha256: 30eef4b8e0df4a51ac948f564f24617732f58ecdee84c30564d88a0a668b98e5

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

require "attentive/tokenizer"
require "set"

module Attentive
  class Listener
    attr_reader :phrases

    def initialize(listeners, phrases, options, callback)
      context_options = options.fetch(:context, {})
      @required_contexts = context_options.fetch(:in, Attentive.default_required_contexts)
      @required_contexts = [] if @required_contexts == :any
      @required_contexts = Set[*@required_contexts]
      @prohibited_contexts = context_options.fetch(:not_in, Attentive.default_prohibited_contexts)
      @prohibited_contexts = Set[*@prohibited_contexts]

      @listeners = listeners
      @phrases = tokenize_phrases!(phrases)
      @callback = callback
    end

    def matches_context?(message)
      return false unless message.contexts.superset? @required_contexts
      return false unless message.contexts.disjoint? @prohibited_contexts
      true
    end

    def stop_listening!
      listeners.delete self
      self
    end

    def call(e)
      @callback.call(e)
    end

  private
    attr_reader :listeners

    def tokenize_phrases!(phrases)
      phrases.map do |phrase|
        tokenize_phrase!(phrase)
      end
    end

    def tokenize_phrase!(phrase)
      Attentive::Tokenizer.tokenize(phrase, entities: true, regexps: true)
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
attentive-0.3.9 lib/attentive/listener.rb
attentive-0.3.8 lib/attentive/listener.rb
attentive-0.3.7 lib/attentive/listener.rb
attentive-0.3.6 lib/attentive/listener.rb
attentive-0.3.5 lib/attentive/listener.rb
attentive-0.3.4 lib/attentive/listener.rb
attentive-0.3.3 lib/attentive/listener.rb
attentive-0.3.2 lib/attentive/listener.rb