Class: RecastAI::Conversation
- Inherits:
-
Object
- Object
- RecastAI::Conversation
- Defined in:
- lib/recastai/apis/request/models/conversation.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#conversation_token ⇒ Object
readonly
Returns the value of attribute conversation_token.
-
#entities ⇒ Object
readonly
Returns the value of attribute entities.
-
#intents ⇒ Object
readonly
Returns the value of attribute intents.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#memory ⇒ Object
readonly
Returns the value of attribute memory.
-
#next_actions ⇒ Object
readonly
Returns the value of attribute next_actions.
-
#processing_language ⇒ Object
readonly
Returns the value of attribute processing_language.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#replies ⇒ Object
readonly
Returns the value of attribute replies.
-
#sentiment ⇒ Object
readonly
Returns the value of attribute sentiment.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #get_memory(key = nil) ⇒ Object
-
#initialize(response, token) ⇒ Conversation
constructor
A new instance of Conversation.
- #intent ⇒ Object
- #joined_replies(sep = ' ') ⇒ Object
- #negative? ⇒ Boolean
- #neutral? ⇒ Boolean
- #next_action ⇒ Object
- #positive? ⇒ Boolean
- #reply ⇒ Object
- #reset_conversation ⇒ Object
- #reset_memory(name = nil) ⇒ Object
- #set_memory(memory) ⇒ Object
- #vnegative? ⇒ Boolean
- #vpositive? ⇒ Boolean
Constructor Details
#initialize(response, token) ⇒ Conversation
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 13 def initialize(response, token) @token = token @raw = response response = JSON.parse(response) response = response['results'] @uuid = response['uuid'] @source = response['source'] @replies = response['replies'] @action = response['action'] ? Action.new(response['action']) : nil @next_actions = response['next_actions'].map{ |i| Action.new(i) } @sentiment = response['sentiment'] @memory = response['memory'].reject{ |_, e| e.nil? }.map{ |n, e| Entity.new(n, e) } @entities = response['entities'].flat_map{ |n, e| e.map{ |ee| Entity.new(n, ee) } } @intents = response['intents'].map{ |i| Intent.new(i) } @conversation_token = response['conversation_token'] @language = response['language'] @processing_language = response['processing_language'] @version = response['version'] = response['timestamp'] @status = response['status'] end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def action @action end |
#conversation_token ⇒ Object (readonly)
Returns the value of attribute conversation_token.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def conversation_token @conversation_token end |
#entities ⇒ Object (readonly)
Returns the value of attribute entities.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def entities @entities end |
#intents ⇒ Object (readonly)
Returns the value of attribute intents.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def intents @intents end |
#language ⇒ Object (readonly)
Returns the value of attribute language.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def language @language end |
#memory ⇒ Object (readonly)
Returns the value of attribute memory.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def memory @memory end |
#next_actions ⇒ Object (readonly)
Returns the value of attribute next_actions.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def next_actions @next_actions end |
#processing_language ⇒ Object (readonly)
Returns the value of attribute processing_language.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def processing_language @processing_language end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def raw @raw end |
#replies ⇒ Object (readonly)
Returns the value of attribute replies.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def replies @replies end |
#sentiment ⇒ Object (readonly)
Returns the value of attribute sentiment.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def sentiment @sentiment end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def source @source end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def status @status end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def uuid @uuid end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
10 11 12 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 10 def version @version end |
Instance Method Details
#get_memory(key = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 50 def get_memory(key = nil) return @memory if key.nil? @memory.each do |entity| return entity if entity.name.casecmp(key.to_s).zero? end nil end |
#intent ⇒ Object
60 61 62 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 60 def intent @intents.any? ? @intents.first : nil end |
#joined_replies(sep = ' ') ⇒ Object
46 47 48 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 46 def joined_replies(sep = ' ') @replies.join(sep) end |
#negative? ⇒ Boolean
76 77 78 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 76 def negative? @sentiment == Utils::SENTIMENT_NEGATIVE end |
#neutral? ⇒ Boolean
72 73 74 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 72 def neutral? @sentiment == Utils::SENTIMENT_NEUTRAL end |
#next_action ⇒ Object
42 43 44 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 42 def next_action @next_actions.any? ? @next_actions.first : nil end |
#positive? ⇒ Boolean
68 69 70 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 68 def positive? @sentiment == Utils::SENTIMENT_POSITIVE end |
#reply ⇒ Object
38 39 40 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 38 def reply @replies.any? ? @replies.first : nil end |
#reset_conversation ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 114 def reset_conversation body = { conversation_token: @conversation_token } response = HTTParty.delete( Utils::CONVERSE_ENDPOINT, body: body, headers: { 'Authorization' => "Token #{@token}" } ) raise RecastError.new(JSON.parse(response.body)['message']) if response.code != 200 response = JSON.parse(response.body) response = response['results'] response['memory'].reject{ |_, e| e.nil? }.map{ |n, e| Entity.new(n, e) } end |
#reset_memory(name = nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 98 def reset_memory(name = nil) body = { conversation_token: @conversation_token } body[:memory] = { name => nil } unless name.nil? response = HTTParty.put( Utils::CONVERSE_ENDPOINT, body: body, headers: { 'Authorization' => "Token #{@token}" } ) raise RecastError.new(JSON.parse(response.body)['message']) if response.code != 200 response = JSON.parse(response.body) response = response['results'] response['memory'].reject{ |_, e| e.nil? }.map{ |n, e| Entity.new(n, e) } end |
#set_memory(memory) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 84 def set_memory(memory) body = { conversation_token: @conversation_token, memory: memory } response = HTTParty.put( Utils::CONVERSE_ENDPOINT, body: body, headers: { 'Authorization' => "Token #{@token}" } ) raise RecastError.new(JSON.parse(response.body)['message']) if response.code != 200 response = JSON.parse(response.body) response = response['results'] response['memory'].reject{ |_, e| e.nil? }.map{ |n, e| Entity.new(n, e) } end |
#vnegative? ⇒ Boolean
80 81 82 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 80 def vnegative? @sentiment == Utils::SENTIMENT_VNEGATIVE end |
#vpositive? ⇒ Boolean
64 65 66 |
# File 'lib/recastai/apis/request/models/conversation.rb', line 64 def vpositive? @sentiment == Utils::SENTIMENT_VPOSITIVE end |