watson-client 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75eb37d001cf07bb49a4719cd6ef33760dd51ef7
4
- data.tar.gz: f23f072ec1f00124c73618f1eabd1a775b53997e
3
+ metadata.gz: 708f63fa2b0fc8d8401632cba3851fcea62ec86e
4
+ data.tar.gz: c939cdec985dbb758214bdd07c9db71c5c3cb9f1
5
5
  SHA512:
6
- metadata.gz: bf10d1598cf074e77b59cb8ee78eaf9f9f42f50917d428ae5be724b336fce03bf6763c37d569c8fdee7203301b04038a1591e29e518c043139bde440e9b974d2
7
- data.tar.gz: 9402bdc34541986e5070c79b6881a5634582a46f08e1da434fcc9fbcd5cb4a75a743933e88ca4d82dd780282f88c92435b9b4771011990efeb70f708363d9209
6
+ metadata.gz: 17d81c47cd0818d1f5a122ca2977be8f5cbad8bb9302e217bbca69b3901d0506d11021cc1a5b7ffcebbdb2b62215ef935cccf98fdf2d596d21aff0af36d381e4
7
+ data.tar.gz: 6dedc5fe97f496b03616d8c149b2458f8b1a670c9dc4281c994a8fd08c4c64d899f7b11497c8457d624c4b89fd55db96775be7e1d223309bc69881663c0da7b5
data/README.md CHANGED
@@ -18,7 +18,7 @@ Watson::Conversation.configure(
18
18
  **Example:**
19
19
  A simple response from your dialog model
20
20
  ```ruby
21
- require 'watson-client'
21
+ require 'watson'
22
22
 
23
23
  client = Watson::Conversation.new
24
24
  client.send_message('message one')
@@ -29,7 +29,7 @@ client.send_message('message two')
29
29
 
30
30
  Try something different
31
31
  ```ruby
32
- require 'watson-client'
32
+ require 'watson'
33
33
 
34
34
  puts 'Say Hi!'
35
35
  user_input = nil
@@ -37,7 +37,8 @@ until user_input == 'q' || user_input == 'exit'
37
37
  print '> '
38
38
  user_input = gets.chomp
39
39
  return if user_input == 'q' || user_input == 'exit'
40
- client.send_message(user_input)
40
+ r = client.send_message(user_input)
41
+ puts r
41
42
  sleep 0.5
42
43
  end
43
44
  ```
@@ -59,3 +60,21 @@ responses = Watson::RetrieveAndRank.send_message('message')
59
60
  ```
60
61
 
61
62
  The response is returned as a JSON array.
63
+
64
+ ### Conversation Enhanced
65
+ Combination of Conversation API and Retrieve & Rank API. In order to get this class working you need to set both both configurations variables.
66
+ If the `Conversation` class detects an unknown message, we call `RetrieveAndRank` with the intent name of that unknown message, so we need to set that intent name in the constructor of `ConversationEnhanced` class.
67
+
68
+ ```ruby
69
+ client = Watson::ConversationEnhanced.new('INTENT_NAME')
70
+ user_input = nil
71
+ until user_input == 'q' || user_input == 'exit'
72
+ print '> '
73
+ user_input = gets.chomp
74
+ return if user_input == 'q' || user_input == 'exit'
75
+ r = client.send_message(user_input)
76
+ puts r
77
+ sleep 0.5
78
+ end
79
+
80
+ ```
@@ -12,9 +12,10 @@ module Watson
12
12
  end
13
13
 
14
14
  def send_message(message = '')
15
- params = { input: { text: message }, alternate_intents: true }
15
+ return if message.empty?
16
+ @message = message
17
+ params = { input: { text: @message }, alternate_intents: true }
16
18
  params.merge!(@context) unless @context.nil?
17
-
18
19
  response = Excon.post(url,
19
20
  body: params.to_json,
20
21
  headers: {
@@ -24,9 +25,13 @@ module Watson
24
25
  user: @@username,
25
26
  password: @@password)
26
27
  result = JSON.parse(response.body)
27
-
28
28
  @context = { context: result['context'] }
29
- puts result['output']['text']
29
+ build_response(result)
30
+ end
31
+
32
+ def build_response(result)
33
+ { source: 'conversation', intent: result['intents'][0]['intent'],
34
+ message: @message, tittle: @message, body: result['output']['text'] }
30
35
  end
31
36
 
32
37
  def url
@@ -0,0 +1,26 @@
1
+ module Watson
2
+ class ConversationEnhanced < Conversation
3
+
4
+ def initialize(intent = '')
5
+ @intent = intent
6
+ end
7
+
8
+ def send_message(message = '')
9
+ return if message.empty?
10
+ response = super
11
+ if must_retrieve?(response[:intent])
12
+ retrieve_and_rank(message)
13
+ else
14
+ response
15
+ end
16
+ end
17
+
18
+ def must_retrieve?(result)
19
+ result == @intent
20
+ end
21
+
22
+ def retrieve_and_rank(message)
23
+ RetrieveAndRank.send_message(message)
24
+ end
25
+ end
26
+ end
@@ -9,7 +9,9 @@ module Watson
9
9
  end
10
10
 
11
11
  def self.send_message(message = '')
12
- query = URI.encode(message)
12
+ return if message.empty?
13
+ @message = message
14
+ query = URI.encode(@message)
13
15
  response = Excon.post(url(query),
14
16
  headers: {
15
17
  'Content-Type' => 'application/json',
@@ -22,7 +24,10 @@ module Watson
22
24
  end
23
25
 
24
26
  def self.answers(data)
25
- data.each { |d| puts d['body'] }
27
+ data.collect do |d|
28
+ { source: 'retrieveandrank', intent: '',
29
+ message: @message, tittle: d['title'], body: d['body'] }
30
+ end
26
31
  end
27
32
 
28
33
  def self.url(query)
data/lib/watson.rb CHANGED
@@ -3,3 +3,4 @@ require 'json'
3
3
  require 'uri'
4
4
  require_relative '../lib/watson/conversation'
5
5
  require_relative '../lib/watson/retrieve_and_rank'
6
+ require_relative '../lib/watson/conversation_enhanced'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watson-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Andres Torres Cruz
@@ -24,7 +24,8 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Send and receive messages to Conversation or Retrieve and Rank services
27
+ description: REST API client for IBM Watson™ services, Conversation, Retrieve & Rank,
28
+ Conversation enhanced services are available
28
29
  email: carlosandrestorres28@gmail.com
29
30
  executables: []
30
31
  extensions: []
@@ -32,8 +33,8 @@ extra_rdoc_files: []
32
33
  files:
33
34
  - README.md
34
35
  - lib/watson.rb
35
- - lib/watson/cluster.rb
36
36
  - lib/watson/conversation.rb
37
+ - lib/watson/conversation_enhanced.rb
37
38
  - lib/watson/retrieve_and_rank.rb
38
39
  homepage: https://github.com/katorres02/w-ruby
39
40
  licenses:
@@ -1,13 +0,0 @@
1
- module Watson
2
- class Cluster
3
- attr_reader :solr_cluster_id, :cluster_name, :cluster_size,
4
- :solr_cluster_status
5
-
6
- def initialize(options = {})
7
- @solr_cluster_id = options['solr_cluster_id']
8
- @cluster_name = options['cluster_name']
9
- @cluster_size = options['cluster_size']
10
- @solr_cluster_status = options['solr_cluster_status']
11
- end
12
- end
13
- end