Sha256: 533cea7e63c1901af21ba20ec76fcc75ea95f70517c9c5c2906ae833e6db0f2e

Contents?: true

Size: 804 Bytes

Versions: 7

Compression:

Stored size: 804 Bytes

Contents

require "set"
require "attentive/tokenizer"

module Attentive
  class Message
    attr_reader :contexts, :text, :original_message

    def initialize(original_message, params={})
      @original_message = original_message
      @text = original_message.to_s
      @contexts = Set.new(params.fetch(:contexts, []))
      @contexts << :conversation if tokens.grep(Attentive::Tokens::Invocation).any?
      @contexts += Array(original_message.contexts) if original_message.respond_to?(:contexts)
    end

    def tokens
      @tokens ||= Attentive::Tokenizer.tokenize(text)
    end

    def [](key)
      tokens[key]
    end

    def []=(key, value)
      tokens[key] = value
    end

    def length
      tokens.length
    end

    alias :to_s :text

    def inspect
      tokens.inspect
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

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