nlu_adapter 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +1 -0
- data/CHANGELOG.md +8 -2
- data/README.md +1 -0
- data/docs/dialogflow.md +7 -1
- data/docs/lex.md +8 -0
- data/docs/watson_assistant.md +64 -0
- data/lib/nlu_adapter.rb +2 -1
- data/lib/nlu_adapter/version.rb +1 -1
- data/lib/nlu_adapter/watson_assistant.rb +157 -0
- data/nlu_adapter.gemspec +4 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25db10d98a5439cbbc18327adba57ed9422211b2088644d950d9172cb8ed210f
|
4
|
+
data.tar.gz: 4ccf19d8a3633de04e0764fa74c374d185e3182dece8750cb3d9a7931a07167a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6184e011cda087ec1e98cb5a81da9f3e01a982e05fdb7d75946bc2a3e083573f61a1992139533f2d73f6b4a42c4b1de73f36fcf30c68189e1b45347e7a3eee6
|
7
|
+
data.tar.gz: 136be99c729217c0e7af0e24c7fc5401dad0f311dc60045b58b8490b48ee160d4a871d72fc156f61cb2e5ed823ca23c97e3b507e07ce3b4fea82d3effb9b4a0e
|
data/.yardopts
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,14 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5 (22/Feb/2019)
|
2
|
+
---
|
3
|
+
* Added IBM Watson Assistant
|
4
|
+
* Updated documentation
|
5
|
+
|
6
|
+
0.1.4 (19/Jan/2019)
|
2
7
|
---
|
3
8
|
* Fixed a bad push
|
9
|
+
* Note/Update: It was not bad! www.rubydoc.info was not refreshing for some strange reason
|
4
10
|
|
5
|
-
0.1.3
|
11
|
+
0.1.3 (19/Jan/2019)
|
6
12
|
---
|
7
13
|
* New functionalities:
|
8
14
|
* bulk_parse - parse multiple texts
|
data/README.md
CHANGED
@@ -31,6 +31,7 @@ NLU Service | Intent | Intet Collection | Intent identification
|
|
31
31
|
----------- | ------ | ---------------- | ---------------------
|
32
32
|
[Aws Lex](https://aws.amazon.com/lex/) ([doc](docs/lex.md)) | C,R,U | C,R,U | Yes
|
33
33
|
[Google Dialogflow](https://dialogflow.com/) ([doc](docs/dialogflow.md)) | C,R,U | To-Do | Yes
|
34
|
+
[IBM Watson Assistant](https://cloud.ibm.com/catalog/services/watson-assistant) ([doc](docs/watson_assistant.md)) | C,R,U | To-Do | Yes
|
34
35
|
|
35
36
|
## Classification metrics
|
36
37
|
### Supported intent classification metrics
|
data/docs/dialogflow.md
CHANGED
@@ -1,13 +1,17 @@
|
|
1
|
+
# @markup markdown
|
2
|
+
|
1
3
|
# NluAdapter::Dialogflow
|
2
4
|
|
3
5
|
Adapter for Google Dialogflow
|
4
6
|
|
5
7
|
## Setup
|
6
8
|
|
9
|
+
Please check the [documentation](https://dialogflow.com/).
|
7
10
|
|
8
11
|
## Examples
|
9
12
|
|
10
|
-
1. Parse a text and identify intent from an existing
|
13
|
+
1. Parse a text and identify intent from an existing Dialogflow bot
|
14
|
+
|
11
15
|
```ruby
|
12
16
|
require 'nlu_adapter'
|
13
17
|
|
@@ -20,6 +24,7 @@ puts d.parse('I want to book a hotel')
|
|
20
24
|
{:intent_name=>"BookHotel"}
|
21
25
|
```
|
22
26
|
2. Create an intent
|
27
|
+
|
23
28
|
```ruby
|
24
29
|
require 'nlu_adapter'
|
25
30
|
|
@@ -35,6 +40,7 @@ d.create_intent(i)
|
|
35
40
|
Create an Agent from Dialogflow console.
|
36
41
|
|
37
42
|
## Running examples
|
43
|
+
|
38
44
|
```bash
|
39
45
|
$ cat test-df-1.rb
|
40
46
|
require 'nlu_adapter'
|
data/docs/lex.md
CHANGED
@@ -1,12 +1,17 @@
|
|
1
|
+
# @markup markdown
|
2
|
+
|
1
3
|
# NluAdapter::Lex
|
2
4
|
|
3
5
|
Adapter for Aws Lex
|
4
6
|
|
5
7
|
## Setup
|
6
8
|
|
9
|
+
Please check the [documentation](https://aws.amazon.com/lex/).
|
10
|
+
|
7
11
|
## Examples
|
8
12
|
|
9
13
|
1. Parse a text and identify intent from an existing Lex bot
|
14
|
+
|
10
15
|
```ruby
|
11
16
|
require 'nlu_adapter'
|
12
17
|
|
@@ -19,6 +24,7 @@ puts l.parse('I want to book a hotel')
|
|
19
24
|
{:intent_name=>"BookHotel"}
|
20
25
|
```
|
21
26
|
2. Create an intent
|
27
|
+
|
22
28
|
```ruby
|
23
29
|
require 'nlu_adapter'
|
24
30
|
|
@@ -30,6 +36,7 @@ l.create_intent(i)
|
|
30
36
|
```
|
31
37
|
|
32
38
|
3. Create an intent collection
|
39
|
+
|
33
40
|
```ruby
|
34
41
|
|
35
42
|
require 'nlu_adapter'
|
@@ -45,6 +52,7 @@ l.create_intent_collection(ic)
|
|
45
52
|
```
|
46
53
|
|
47
54
|
## Running examples
|
55
|
+
|
48
56
|
```bash
|
49
57
|
$ cat test-lex-1.rb
|
50
58
|
require 'nlu_adapter'
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# @markup markdown
|
2
|
+
|
3
|
+
# NluAdapter::WatsonAssistant
|
4
|
+
|
5
|
+
Adapter for IBM Watson Assistant
|
6
|
+
|
7
|
+
## Setup
|
8
|
+
|
9
|
+
Please check the [Getting started tutorial](https://cloud.ibm.com/docs/services/assistant/getting-started.html#getting-started)
|
10
|
+
|
11
|
+
* To get the API details
|
12
|
+
* Go to [resources list](https://cloud.ibm.com/resources)
|
13
|
+
* Select the desired service from the Services section
|
14
|
+
* Download / Copy the API Key & URL
|
15
|
+
* Click on the Launch tool to go to IBM Watson Assistant
|
16
|
+
* Select the Skills tab
|
17
|
+
* Click on the vertical ... icon for the desired skill
|
18
|
+
* Then click on View API Details from the menu
|
19
|
+
* Copy the Workspace ID
|
20
|
+
|
21
|
+
* To create intents
|
22
|
+
* In the above step instead of clicking on the vertical ... icon, click the skill name
|
23
|
+
|
24
|
+
## Examples
|
25
|
+
|
26
|
+
1. Parse a text and identify intent from an existing skill
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'nlu_adapter'
|
30
|
+
|
31
|
+
wa = NluAdapter.new(:WatsonAssistant, {url: 'https://gateway-lon.watsonplatform.net/assistant/api', version: '2018-09-20'})
|
32
|
+
|
33
|
+
puts wa.parse('I want to book a hotel')
|
34
|
+
|
35
|
+
```
|
36
|
+
```
|
37
|
+
{:intent_name=>"BookHotel"}
|
38
|
+
```
|
39
|
+
2. Create an intent
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'nlu_adapter'
|
43
|
+
|
44
|
+
wa = NluAdapter.new(:WatsonAssistant, {url: 'https://gateway-lon.watsonplatform.net/assistant/api', version: '2018-09-20'})
|
45
|
+
i = wa.new_intent('BookHotel', ['please book a hotel', 'I want to book a hotel'])
|
46
|
+
|
47
|
+
wa.create_intent(i)
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
## Running examples
|
52
|
+
|
53
|
+
```bash
|
54
|
+
$ cat test-wa-1.rb
|
55
|
+
require 'nlu_adapter'
|
56
|
+
|
57
|
+
wa = NluAdapter.new(:WatsonAssistant, {url: 'https://gateway-lon.watsonplatform.net/assistant/api', version: '2018-09-20'})
|
58
|
+
|
59
|
+
puts wa.parse('I want to book a hotel')
|
60
|
+
|
61
|
+
$ WATSON_API_KEY='XXX' WATSON_WORKSPACE_ID='YYY' ruby ./test-wa-1.rb
|
62
|
+
{:intent_name=>"BookHotel"}
|
63
|
+
|
64
|
+
```
|
data/lib/nlu_adapter.rb
CHANGED
@@ -6,8 +6,9 @@ module NluAdapter
|
|
6
6
|
autoload :ParseHelper, 'nlu_adapter/parse_helper'
|
7
7
|
autoload :Metrics, 'nlu_adapter/metrics'
|
8
8
|
module Adapters
|
9
|
-
autoload :Lex,
|
9
|
+
autoload :Lex, 'nlu_adapter/lex'
|
10
10
|
autoload :Dialogflow, 'nlu_adapter/dialogflow'
|
11
|
+
autoload :WatsonAssistant, 'nlu_adapter/watson_assistant'
|
11
12
|
end
|
12
13
|
|
13
14
|
def self.new(name, options = {})
|
data/lib/nlu_adapter/version.rb
CHANGED
@@ -0,0 +1,157 @@
|
|
1
|
+
require "ibm_watson/assistant_v1"
|
2
|
+
|
3
|
+
|
4
|
+
module NluAdapter
|
5
|
+
module Adapters
|
6
|
+
# IBM Watson Assistant wrapper class
|
7
|
+
#
|
8
|
+
class WatsonAssistant
|
9
|
+
include ParseHelper
|
10
|
+
|
11
|
+
include IBMWatson
|
12
|
+
|
13
|
+
# Constructor
|
14
|
+
def initialize(options = {})
|
15
|
+
@url = options[:url] || "https://gateway-lon.watsonplatform.net/assistant/api"
|
16
|
+
@version = options[:version] || "2018-09-20"
|
17
|
+
@workspace_id = ENV["WATSON_WORKSPACE_ID"]
|
18
|
+
begin
|
19
|
+
@assistant = AssistantV1.new(
|
20
|
+
iam_apikey: ENV["WATSON_API_KEY"],
|
21
|
+
version: @version,
|
22
|
+
url: @url
|
23
|
+
)
|
24
|
+
rescue WatsonApiException => ex
|
25
|
+
puts "Error: #{ex.inspect}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Understand a given text
|
30
|
+
#
|
31
|
+
# @param text [String] a text to parse using the NLU provider
|
32
|
+
# @return [Json] return the identified intent name
|
33
|
+
#
|
34
|
+
def parse(text)
|
35
|
+
intent_name = nil
|
36
|
+
begin
|
37
|
+
response = @assistant.message(
|
38
|
+
workspace_id: @workspace_id,
|
39
|
+
input: {
|
40
|
+
text: text
|
41
|
+
}
|
42
|
+
)
|
43
|
+
|
44
|
+
intent_name = response.result["intents"][0]["intent"]
|
45
|
+
rescue WatsonApiException => ex
|
46
|
+
puts "Error: #{ex.inspect}"
|
47
|
+
end
|
48
|
+
return { intent_name: intent_name}
|
49
|
+
end
|
50
|
+
|
51
|
+
# Get an instance of Intent, if it exists else nil
|
52
|
+
#
|
53
|
+
# @param name [String] name of the intent
|
54
|
+
# @return [Intent] intent object
|
55
|
+
#
|
56
|
+
def get_intent(name)
|
57
|
+
begin
|
58
|
+
response = @assistant.get_intent(
|
59
|
+
workspace_id: @workspace_id,
|
60
|
+
intent: name,
|
61
|
+
export: true
|
62
|
+
)
|
63
|
+
|
64
|
+
return Intent.new({id: response.result['intent'], #for is update check
|
65
|
+
name: response.result['intent'],
|
66
|
+
description: response.result['description'],
|
67
|
+
utterences: response.result['examples'].map { |e| e['text']} })
|
68
|
+
|
69
|
+
rescue WatsonApiException => ex
|
70
|
+
puts "Error: #{ex.inspect}"
|
71
|
+
end
|
72
|
+
return nil
|
73
|
+
end
|
74
|
+
|
75
|
+
# Get a new instance of Intent
|
76
|
+
#
|
77
|
+
# @param name [String] name of the intent
|
78
|
+
# @param utterences [Array] phrases for training
|
79
|
+
# @return [Intent] Intent object
|
80
|
+
#
|
81
|
+
def new_intent(name, description, utterences = [])
|
82
|
+
i = get_intent(name)
|
83
|
+
|
84
|
+
i = Intent.new if !i
|
85
|
+
i.name = name
|
86
|
+
i.description = description
|
87
|
+
i.utterences = utterences
|
88
|
+
|
89
|
+
return i
|
90
|
+
end
|
91
|
+
|
92
|
+
# Given an Intent object, create/update it in Dialogflow
|
93
|
+
#
|
94
|
+
# @param intent [Intent] Intent object
|
95
|
+
# @return [Intent] Intent object
|
96
|
+
#
|
97
|
+
# @todo convert response -> Intent
|
98
|
+
#
|
99
|
+
def create_intent(intent)
|
100
|
+
#check: to create / update
|
101
|
+
i = intent.to_h
|
102
|
+
if !intent.id
|
103
|
+
response = @assistant.create_intent(
|
104
|
+
workspace_id: @workspace_id,
|
105
|
+
intent: i[:name],
|
106
|
+
description: i[:description],
|
107
|
+
examples: i[:examples]
|
108
|
+
)
|
109
|
+
else
|
110
|
+
response = @assistant.update_intent(
|
111
|
+
workspace_id: @workspace_id,
|
112
|
+
intent: intent.id,
|
113
|
+
new_examples: i[:examples],
|
114
|
+
new_description: i[:description],
|
115
|
+
new_intent: i[:name]
|
116
|
+
)
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
# Class represents Intent in an IntentCollection
|
123
|
+
class Intent
|
124
|
+
include NluAdapterIntent
|
125
|
+
attr_accessor :id, :description
|
126
|
+
|
127
|
+
# Constructor
|
128
|
+
def initialize(options = {})
|
129
|
+
@id = options[:id] #check for is update
|
130
|
+
@name = options[:name]
|
131
|
+
@description = options[:description]
|
132
|
+
@utterences = options[:utterences]
|
133
|
+
end
|
134
|
+
|
135
|
+
# Convert self to Hash
|
136
|
+
# @return [Hash] ruby hash
|
137
|
+
#
|
138
|
+
def to_h
|
139
|
+
{
|
140
|
+
name: @name,
|
141
|
+
description: @description,
|
142
|
+
examples: @utterences.map {|u| {"text" => u } }
|
143
|
+
}
|
144
|
+
end
|
145
|
+
|
146
|
+
# convert self to json
|
147
|
+
# @return [json] json
|
148
|
+
#
|
149
|
+
def to_json
|
150
|
+
to_h.to_json
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
data/nlu_adapter.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nlu_adapter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Girish Nair
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: 0.1.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: ibm_watson
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.12.0
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.12.0
|
111
125
|
description: An adapter for various NLU web services like Aws Lex, Google Dialogflow
|
112
126
|
etc.
|
113
127
|
email:
|
@@ -130,6 +144,7 @@ files:
|
|
130
144
|
- bin/setup
|
131
145
|
- docs/dialogflow.md
|
132
146
|
- docs/lex.md
|
147
|
+
- docs/watson_assistant.md
|
133
148
|
- entrypoint.sh
|
134
149
|
- lib/nlu_adapter.rb
|
135
150
|
- lib/nlu_adapter/dialogflow.rb
|
@@ -139,6 +154,7 @@ files:
|
|
139
154
|
- lib/nlu_adapter/metrics.rb
|
140
155
|
- lib/nlu_adapter/parse_helper.rb
|
141
156
|
- lib/nlu_adapter/version.rb
|
157
|
+
- lib/nlu_adapter/watson_assistant.rb
|
142
158
|
- nlu_adapter.gemspec
|
143
159
|
homepage: https://rubygems.org/gems/nlu_adapter
|
144
160
|
licenses:
|