stealth-luis 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +107 -0
- data/README.md +314 -0
- data/lib/stealth-luis.rb +3 -0
- data/lib/stealth/nlp/luis.rb +4 -0
- data/lib/stealth/nlp/luis/client.rb +58 -0
- data/lib/stealth/nlp/luis/result.rb +114 -0
- data/spec/spec_helper.rb +84 -0
- data/stealth-luis.gemspec +17 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f836e0dc962e9ecc58fb346cdd532c374afd2e88a5bbd360dfce71dd32b9678c
|
4
|
+
data.tar.gz: ffb78ed4055c36b83710127b9262146b19fe6361261eb23f9811d8902f158cff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 30064fe7c524dc67e9eac2251fa5b6c8e97af6fb14944d154a3de24c034e17f32d56682636614b08f72620d0130375b03f34630241eeeeb9301c26aa9907e0bd
|
7
|
+
data.tar.gz: a67c8549d67593f47ef605908369436e480f2f1287be63025d0c37df42948744dc20e5202622236b1ef444094613061642f38d5dcabf00b7951c0c1bb719326d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/hellostealth/stealth
|
3
|
+
revision: 36a7adea03b933e2c33c3d3f05e1770bb5fa1b96
|
4
|
+
branch: master
|
5
|
+
specs:
|
6
|
+
stealth (2.0.0)
|
7
|
+
activesupport (~> 6.0)
|
8
|
+
multi_json (~> 1.12)
|
9
|
+
puma (>= 4.2, < 5.0)
|
10
|
+
sidekiq (~> 6.0)
|
11
|
+
sinatra (~> 2.0)
|
12
|
+
thor (~> 0.20)
|
13
|
+
|
14
|
+
PATH
|
15
|
+
remote: .
|
16
|
+
specs:
|
17
|
+
stealth-luis (0.9.0)
|
18
|
+
http (~> 4)
|
19
|
+
stealth (~> 2.0)
|
20
|
+
|
21
|
+
GEM
|
22
|
+
remote: https://rubygems.org/
|
23
|
+
specs:
|
24
|
+
activesupport (6.0.1)
|
25
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
26
|
+
i18n (>= 0.7, < 2)
|
27
|
+
minitest (~> 5.1)
|
28
|
+
tzinfo (~> 1.1)
|
29
|
+
zeitwerk (~> 2.2)
|
30
|
+
addressable (2.7.0)
|
31
|
+
public_suffix (>= 2.0.2, < 5.0)
|
32
|
+
concurrent-ruby (1.1.5)
|
33
|
+
connection_pool (2.2.2)
|
34
|
+
diff-lcs (1.3)
|
35
|
+
domain_name (0.5.20190701)
|
36
|
+
unf (>= 0.0.5, < 1.0.0)
|
37
|
+
ffi (1.11.2)
|
38
|
+
ffi-compiler (1.0.1)
|
39
|
+
ffi (>= 1.0.0)
|
40
|
+
rake
|
41
|
+
http (4.2.0)
|
42
|
+
addressable (~> 2.3)
|
43
|
+
http-cookie (~> 1.0)
|
44
|
+
http-form_data (~> 2.0)
|
45
|
+
http-parser (~> 1.2.0)
|
46
|
+
http-cookie (1.0.3)
|
47
|
+
domain_name (~> 0.5)
|
48
|
+
http-form_data (2.1.1)
|
49
|
+
http-parser (1.2.1)
|
50
|
+
ffi-compiler (>= 1.0, < 2.0)
|
51
|
+
i18n (1.7.0)
|
52
|
+
concurrent-ruby (~> 1.0)
|
53
|
+
minitest (5.13.0)
|
54
|
+
multi_json (1.14.1)
|
55
|
+
mustermann (1.0.3)
|
56
|
+
nio4r (2.5.2)
|
57
|
+
public_suffix (4.0.1)
|
58
|
+
puma (4.3.0)
|
59
|
+
nio4r (~> 2.0)
|
60
|
+
rack (2.0.7)
|
61
|
+
rack-protection (2.0.7)
|
62
|
+
rack
|
63
|
+
rake (13.0.1)
|
64
|
+
redis (4.1.3)
|
65
|
+
rspec (3.8.0)
|
66
|
+
rspec-core (~> 3.8.0)
|
67
|
+
rspec-expectations (~> 3.8.0)
|
68
|
+
rspec-mocks (~> 3.8.0)
|
69
|
+
rspec-core (3.8.0)
|
70
|
+
rspec-support (~> 3.8.0)
|
71
|
+
rspec-expectations (3.8.2)
|
72
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
73
|
+
rspec-support (~> 3.8.0)
|
74
|
+
rspec-mocks (3.8.0)
|
75
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
76
|
+
rspec-support (~> 3.8.0)
|
77
|
+
rspec-support (3.8.0)
|
78
|
+
sidekiq (6.0.3)
|
79
|
+
connection_pool (>= 2.2.2)
|
80
|
+
rack (>= 2.0.0)
|
81
|
+
rack-protection (>= 2.0.0)
|
82
|
+
redis (>= 4.1.0)
|
83
|
+
sinatra (2.0.7)
|
84
|
+
mustermann (~> 1.0)
|
85
|
+
rack (~> 2.0)
|
86
|
+
rack-protection (= 2.0.7)
|
87
|
+
tilt (~> 2.0)
|
88
|
+
thor (0.20.3)
|
89
|
+
thread_safe (0.3.6)
|
90
|
+
tilt (2.0.10)
|
91
|
+
tzinfo (1.2.5)
|
92
|
+
thread_safe (~> 0.1)
|
93
|
+
unf (0.1.4)
|
94
|
+
unf_ext
|
95
|
+
unf_ext (0.0.7.6)
|
96
|
+
zeitwerk (2.2.1)
|
97
|
+
|
98
|
+
PLATFORMS
|
99
|
+
ruby
|
100
|
+
|
101
|
+
DEPENDENCIES
|
102
|
+
rspec (= 3.8.0)
|
103
|
+
stealth!
|
104
|
+
stealth-luis!
|
105
|
+
|
106
|
+
BUNDLED WITH
|
107
|
+
2.1.2
|
data/README.md
ADDED
@@ -0,0 +1,314 @@
|
|
1
|
+
# Stealth LUIS
|
2
|
+
|
3
|
+
This integration implements the [Microsoft LUIS](https://luis.ai) Language Understanding service. It utilizes the built-in NLP features part of Stealth 2.x. If you are still using Stealth 1.x, you will first need to upgrade to Stealth 2.x before you can use this integration.
|
4
|
+
|
5
|
+
## Configuration
|
6
|
+
|
7
|
+
For instructions on how to configure your Azure account signup for LUIS, please reference their docs. You won't have to set this anywhere, but this gem does utilize the latest `v3` LUIS API version.
|
8
|
+
|
9
|
+
Once your account is setup, these are the configuration settings you will need to add to you `services.yml` file:
|
10
|
+
|
11
|
+
```yaml
|
12
|
+
default: &default
|
13
|
+
luis:
|
14
|
+
endpoint: westus.api.cognitive.microsoft.com
|
15
|
+
app_id: 9434fbd8-420b-6d75-8a6f-b6c9a0ac5ec0
|
16
|
+
subscription_key: 1b69a4b9db669805b4fcba5f1f2f87bb
|
17
|
+
tz_offset: 0
|
18
|
+
|
19
|
+
production:
|
20
|
+
<<: *default
|
21
|
+
development:
|
22
|
+
<<: *default
|
23
|
+
test:
|
24
|
+
<<: *default
|
25
|
+
```
|
26
|
+
|
27
|
+
Next, inside of an initializer in your bot (`config/initializers/settings.rb`), you need to tell Stealth that `LUIS` will be your default NLP integration:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Stealth.config.nlp_integration = :luis
|
31
|
+
```
|
32
|
+
|
33
|
+
Stealth will automatically use your `staging` LUIS slot in development and staging environments and will use the `production` slot for your production Stealth environment.
|
34
|
+
|
35
|
+
That's it! Stealth will now automatically use LUIS for intent detection and entity extraction automatically via `handle_response` and `get_match`.
|
36
|
+
|
37
|
+
## Intents
|
38
|
+
|
39
|
+
We recommend you name your intents using snake case (`snake_case`). This is because this integration will automatically convert your intent names to Ruby symbols.
|
40
|
+
|
41
|
+
So for example, if you have a `handle_response` defined like this:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
handle_response(
|
45
|
+
'Maybe' => proc { step_to state: :say_maybe },
|
46
|
+
:yes => proc { step_to state: :say_yes },
|
47
|
+
:no => proc { step_to state: :say_no }
|
48
|
+
)
|
49
|
+
```
|
50
|
+
|
51
|
+
If your user responds with a variation of the string `maybe`, then they will be taken to the state `say_maybe`.
|
52
|
+
|
53
|
+
Otherwise, the intent named `yes` and the intent named `no` will attempt to be matched. So if you had named your intent `YES` for example, you'd have to use `:YES` here which doesn't match Ruby syntax conventions.
|
54
|
+
|
55
|
+
For more info about how intents are matched, please see the [Stealth NLP documentation](https://github.com/hellostealth/stealth/wiki/NLP).
|
56
|
+
|
57
|
+
## Entities
|
58
|
+
|
59
|
+
The entity types listed below are named using their corresponding Stealth type. The equivalent type used by Microsoft LUIS is also listed. For each code sample, the sample query is first provided followed by the array of entities extracted from the queries (for the given type).
|
60
|
+
|
61
|
+
It's possible, and even likely, that a query matches more than one entity type. For example, a `currency` type will also match a `number` type. For more info about how to utilize these types, please see the [Stealth NLP documentation](https://github.com/hellostealth/stealth/wiki/NLP).
|
62
|
+
|
63
|
+
### number
|
64
|
+
|
65
|
+
LUIS prebuilt entity: `number`
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
"I think it was something like 63 or maybe 764"
|
69
|
+
|
70
|
+
[
|
71
|
+
63,
|
72
|
+
764
|
73
|
+
]
|
74
|
+
```
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
"It was almost 15k"
|
78
|
+
|
79
|
+
[
|
80
|
+
15000
|
81
|
+
]
|
82
|
+
```
|
83
|
+
|
84
|
+
For more info about these values, please reference the [number entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-number?tabs=V3).
|
85
|
+
|
86
|
+
### currency
|
87
|
+
|
88
|
+
LUIS prebuilt entity: `money`
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
"send me $87 or 48 cents"
|
92
|
+
|
93
|
+
[
|
94
|
+
{ 'number' => 87, 'units' => 'Dollar' },
|
95
|
+
{ 'number' => 48, 'units': 'Cent' }
|
96
|
+
]
|
97
|
+
```
|
98
|
+
|
99
|
+
For more info about these values, please reference the [money entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-currency?tabs=V3).
|
100
|
+
|
101
|
+
### email
|
102
|
+
|
103
|
+
LUIS prebuilt entity: `email`
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
"you can contact me at john@email.none"
|
107
|
+
|
108
|
+
[
|
109
|
+
"john@email.none"
|
110
|
+
]
|
111
|
+
```
|
112
|
+
|
113
|
+
For more info about these values, please reference the [email entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-email?tabs=V3).
|
114
|
+
|
115
|
+
### phone
|
116
|
+
|
117
|
+
LUIS prebuilt entity: `phonenumber`
|
118
|
+
|
119
|
+
Note: LUIS does not parse nor attempts to clean up phone number.
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
"You can reach me at 313-555-1212"
|
123
|
+
|
124
|
+
[
|
125
|
+
"313-555-1212"
|
126
|
+
]
|
127
|
+
```
|
128
|
+
|
129
|
+
For more info about these values, please reference the [phonenumber entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-phonenumber?tabs=V3).
|
130
|
+
|
131
|
+
### percentage
|
132
|
+
|
133
|
+
LUIS prebuilt entity: `percentage`
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
"The stock is up 8.9% today"
|
137
|
+
|
138
|
+
[
|
139
|
+
8.9
|
140
|
+
]
|
141
|
+
```
|
142
|
+
|
143
|
+
For more info about these values, please reference the [percentage entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-percentage?tabs=V3).
|
144
|
+
|
145
|
+
### age
|
146
|
+
|
147
|
+
LUIS prebuilt entity: `age`
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
"81 years old"
|
151
|
+
|
152
|
+
[
|
153
|
+
{ 'number' => 81, 'units' => 'Year' }
|
154
|
+
]
|
155
|
+
```
|
156
|
+
|
157
|
+
For more info about these values, please reference the [age entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-age?tabs=V3).
|
158
|
+
|
159
|
+
### url
|
160
|
+
|
161
|
+
LUIS prebuilt entity: `url`
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
"please visit google.com or https://google.com"
|
165
|
+
|
166
|
+
[
|
167
|
+
"google.com",
|
168
|
+
"https://google.com"
|
169
|
+
]
|
170
|
+
|
171
|
+
```
|
172
|
+
|
173
|
+
For more info about these values, please reference the [url entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-url?tabs=V3).
|
174
|
+
|
175
|
+
### ordinal
|
176
|
+
|
177
|
+
LUIS prebuilt entity: `ordinalV2`
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
"they finished 2nd and 5th"
|
181
|
+
|
182
|
+
[
|
183
|
+
{ 'offset' => 2, 'relativeTo' => 'start' },
|
184
|
+
{ 'offset' => 5, 'relativeTo' => 'start' }
|
185
|
+
]
|
186
|
+
```
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
"she finished last"
|
190
|
+
|
191
|
+
[
|
192
|
+
{ 'offset' => 0, 'relativeTo' => 'end' }
|
193
|
+
]
|
194
|
+
```
|
195
|
+
|
196
|
+
For more info about these values, please reference the [ordinalV2 entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-ordinal-v2?tabs=V3).
|
197
|
+
|
198
|
+
### geo
|
199
|
+
|
200
|
+
LUIS prebuilt entity: `geographyV2`
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
"She moved to paris, france"
|
204
|
+
|
205
|
+
[
|
206
|
+
{ 'value' => 'paris', 'type' => 'city' },
|
207
|
+
{ 'value' => 'france', 'type' => 'countryRegion' }
|
208
|
+
]
|
209
|
+
```
|
210
|
+
|
211
|
+
For more info about these values, please reference the [geographyV2 entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-geographyv2?tabs=V3).
|
212
|
+
|
213
|
+
### dimension
|
214
|
+
|
215
|
+
LUIS prebuilt entity: `dimension`
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
"it's about 4 inches wide"
|
219
|
+
|
220
|
+
[
|
221
|
+
{ "number": 4, "units": "Inch" }
|
222
|
+
]
|
223
|
+
```
|
224
|
+
|
225
|
+
For more info about these values, please reference the [dimension entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-dimension?tabs=V3).
|
226
|
+
|
227
|
+
### temp
|
228
|
+
|
229
|
+
LUIS prebuilt entity: `temperature`
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
"it feels like 98 degrees"
|
233
|
+
|
234
|
+
[
|
235
|
+
{ 'number' => 98, 'units' => 'Degree' }
|
236
|
+
]
|
237
|
+
```
|
238
|
+
|
239
|
+
For more info about these values, please reference the [temperature entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-temperature?tabs=V3).
|
240
|
+
|
241
|
+
### datetime
|
242
|
+
|
243
|
+
LUIS prebuilt entity: `datetimeV2`
|
244
|
+
|
245
|
+
This one is the most complicated one to work with. The values are nested pretty deeply. This integration exposes the values at such a high level because there is a chance that LUIS will return results for more than one date type. For example, below we have just one result of type `date`, but LUIS could return more than one object of subtype `daterange`, `time`, `timerange`, etc. See the docs for more info about these subtypes.
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
"How about Mar 12?"
|
249
|
+
|
250
|
+
[
|
251
|
+
{
|
252
|
+
"type": "date",
|
253
|
+
"values": [
|
254
|
+
{
|
255
|
+
"timex": "XXXX-03-12",
|
256
|
+
"resolution": [
|
257
|
+
{
|
258
|
+
"value": "2019-03-12"
|
259
|
+
},
|
260
|
+
{
|
261
|
+
"value": "2020-03-12"
|
262
|
+
}
|
263
|
+
]
|
264
|
+
}
|
265
|
+
]
|
266
|
+
}
|
267
|
+
]
|
268
|
+
```
|
269
|
+
|
270
|
+
For more info about these values, please reference the [datetimeV2 entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-datetimev2?tabs=1-1%2C2-1%2C3-1%2C4-1%2C5-1%2C6-1#types-of-datetimev2).
|
271
|
+
|
272
|
+
### duration
|
273
|
+
|
274
|
+
LUIS prebuilt domain entity: `Calendar.Duration`
|
275
|
+
|
276
|
+
```ruby
|
277
|
+
"it will be between 15 minutes and 3 hours"
|
278
|
+
|
279
|
+
[
|
280
|
+
"15 minutes",
|
281
|
+
"3 hours"
|
282
|
+
]
|
283
|
+
```
|
284
|
+
|
285
|
+
_Additional docs for this prebuilt domain entitiy is not available_
|
286
|
+
|
287
|
+
### key_phrase
|
288
|
+
|
289
|
+
LUIS prebuilt entity: `keyPhrase`
|
290
|
+
|
291
|
+
```ruby
|
292
|
+
"I need to find the instructional materials for the course"
|
293
|
+
|
294
|
+
[
|
295
|
+
"instructional materials",
|
296
|
+
"course"
|
297
|
+
]
|
298
|
+
```
|
299
|
+
|
300
|
+
For more info about these values, please reference the [keyPhrase entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-keyphrase?tabs=V3).
|
301
|
+
|
302
|
+
### name
|
303
|
+
|
304
|
+
LUIS prebuilt entity: `personName`
|
305
|
+
|
306
|
+
```ruby
|
307
|
+
"Little Cindy-Lou Who who was not more than two"
|
308
|
+
|
309
|
+
[
|
310
|
+
"Little Cindy-Lou"
|
311
|
+
]
|
312
|
+
```
|
313
|
+
|
314
|
+
For more info about these values, please reference the [personName entity LUIS documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-person?tabs=V3).
|
data/lib/stealth-luis.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Stealth
|
4
|
+
module Nlp
|
5
|
+
module Luis
|
6
|
+
class Client < Stealth::Nlp::Client
|
7
|
+
|
8
|
+
ENDPOINT = ENV['LUIS_ENDPOINT'] || 'westus.api.cognitive.microsoft.com'
|
9
|
+
|
10
|
+
def initialize(subscription_key: nil, app_id: nil, tz_offset: 0)
|
11
|
+
begin
|
12
|
+
@subscription_key = subscription_key || Stealth.config.luis.subscription_key
|
13
|
+
@app_id = app_id || Stealth.config.luis.app_id
|
14
|
+
@tz_offset = tz_offset || Stealth.config.luis.tz_offset
|
15
|
+
@slot = Stealth.env.development? ? 'staging' : 'production'
|
16
|
+
rescue NoMethodError
|
17
|
+
raise(
|
18
|
+
Stealth::Errors::ConfigurationError,
|
19
|
+
'A `luis` configuration key must be specified directly or in `services.yml`'
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def endpoint
|
25
|
+
"https://#{ENDPOINT}/luis/prediction/v3.0/apps/#{@app_id}/slots/#{@slot}/predict"
|
26
|
+
end
|
27
|
+
|
28
|
+
def client
|
29
|
+
@client ||= begin
|
30
|
+
headers = {
|
31
|
+
'Content-Type' => 'application/json'
|
32
|
+
}
|
33
|
+
HTTP.timeout(connect: 15, read: 60).headers(headers)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def understand(query:)
|
38
|
+
params = {
|
39
|
+
'datetimeReference' => @tz_offset,
|
40
|
+
'subscription-key' => @subscription_key,
|
41
|
+
'query' => query
|
42
|
+
}
|
43
|
+
|
44
|
+
Stealth::Logger.l(
|
45
|
+
topic: :nlp,
|
46
|
+
message: 'Performing NLP lookup via Microsoft LUIS'
|
47
|
+
)
|
48
|
+
result = client.get(endpoint, params: params)
|
49
|
+
Result.new(result: result)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
ENTITY_TYPES = %i(number currency email percentage phone age
|
58
|
+
url ordinal geo dimension temp datetime duration)
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Stealth
|
4
|
+
module Nlp
|
5
|
+
module Luis
|
6
|
+
class Result < Stealth::Nlp::Result
|
7
|
+
|
8
|
+
ENTITY_MAP = {
|
9
|
+
'money' => :currency, 'number' => :number, 'email' => :email,
|
10
|
+
'percentage' => :percentage, 'Calendar.Duration' => :duration,
|
11
|
+
'geographyV2' => :geo, 'age' => :age, 'phonenumber' => :phone,
|
12
|
+
'ordinalV2' => :ordinal, 'url' => :url, 'dimension' => :dimension,
|
13
|
+
'temperature' => :temp, 'keyPhrase' => :key_phrase, 'name' => :name,
|
14
|
+
'datetimeV2' => :datetime
|
15
|
+
}
|
16
|
+
|
17
|
+
def initialize(result:)
|
18
|
+
@result = result
|
19
|
+
if result.status.success?
|
20
|
+
Stealth::Logger.l(
|
21
|
+
topic: :nlp,
|
22
|
+
message: 'NLP lookup successful'
|
23
|
+
)
|
24
|
+
parsed_result
|
25
|
+
else
|
26
|
+
Stealth::Logger.l(
|
27
|
+
topic: :nlp,
|
28
|
+
message: "NLP lookup FAILED: (#{result.status.code}) #{result.body.to_s}"
|
29
|
+
)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Sample JSON result:
|
34
|
+
# {
|
35
|
+
# "query": "I make between $5400 and $9600 per month",
|
36
|
+
# "prediction": {
|
37
|
+
# "topIntent": "None",
|
38
|
+
# "intents": {
|
39
|
+
# "None": {
|
40
|
+
# "score": 0.5345857
|
41
|
+
# }
|
42
|
+
# },
|
43
|
+
# "entities": {
|
44
|
+
# "money": [
|
45
|
+
# {
|
46
|
+
# "number": 5400,
|
47
|
+
# "units": "Dollar"
|
48
|
+
# },
|
49
|
+
# {
|
50
|
+
# "number": 9600,
|
51
|
+
# "units": "Dollar"
|
52
|
+
# }
|
53
|
+
# ],
|
54
|
+
# "number": [
|
55
|
+
# 5400,
|
56
|
+
# 9600
|
57
|
+
# ]
|
58
|
+
# },
|
59
|
+
# "sentiment": {
|
60
|
+
# "label": "positive",
|
61
|
+
# "score": 0.7805586
|
62
|
+
# }
|
63
|
+
# }
|
64
|
+
# }
|
65
|
+
def parsed_result
|
66
|
+
@parsed_result ||= MultiJson.load(result.body.to_s)
|
67
|
+
end
|
68
|
+
|
69
|
+
def intent
|
70
|
+
top_intent&.to_sym
|
71
|
+
end
|
72
|
+
|
73
|
+
def intent_score
|
74
|
+
parsed_result&.dig('prediction', 'intents', top_intent)
|
75
|
+
end
|
76
|
+
|
77
|
+
def raw_entities
|
78
|
+
parsed_result&.dig('prediction', 'entities')
|
79
|
+
end
|
80
|
+
|
81
|
+
def entities
|
82
|
+
return {} if raw_entities.blank?
|
83
|
+
_entities = {}
|
84
|
+
|
85
|
+
raw_entities.each do |type, values|
|
86
|
+
if ENTITY_MAP[type]
|
87
|
+
_entities[ENTITY_MAP[type]] = values
|
88
|
+
else
|
89
|
+
# A custom entity
|
90
|
+
_entities[type.to_sym] = values
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
_entities
|
95
|
+
end
|
96
|
+
|
97
|
+
def sentiment_score
|
98
|
+
parsed_result&.dig('prediction', 'sentiment', 'score')
|
99
|
+
end
|
100
|
+
|
101
|
+
def sentiment
|
102
|
+
parsed_result&.dig('prediction', 'sentiment', 'label')&.to_sym
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def top_intent
|
108
|
+
parsed_result&.dig('prediction', 'topIntent')
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'stealth-luis'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.expect_with :rspec do |expectations|
|
9
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
10
|
+
# and `failure_message` of custom matchers include text for helper methods
|
11
|
+
# defined using `chain`, e.g.:
|
12
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
13
|
+
# # => "be bigger than 2 and smaller than 4"
|
14
|
+
# ...rather than:
|
15
|
+
# # => "be bigger than 2"
|
16
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
17
|
+
end
|
18
|
+
|
19
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
20
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
21
|
+
config.mock_with :rspec do |mocks|
|
22
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
23
|
+
# a real object. This is generally recommended, and will default to
|
24
|
+
# `true` in RSpec 4.
|
25
|
+
mocks.verify_partial_doubles = true
|
26
|
+
end
|
27
|
+
|
28
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
29
|
+
# have no way to turn it off -- the option exists only for backwards
|
30
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
31
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
32
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
33
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
34
|
+
|
35
|
+
# The settings below are suggested to provide a good initial experience
|
36
|
+
# with RSpec, but feel free to customize to your heart's content.
|
37
|
+
=begin
|
38
|
+
# This allows you to limit a spec run to individual examples or groups
|
39
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
40
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
41
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
42
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
43
|
+
config.filter_run_when_matching :focus
|
44
|
+
|
45
|
+
# Allows RSpec to persist some state between runs in order to support
|
46
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
47
|
+
# you configure your source control system to ignore this file.
|
48
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
49
|
+
|
50
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
51
|
+
# recommended. For more details, see:
|
52
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
53
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
54
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
55
|
+
config.disable_monkey_patching!
|
56
|
+
|
57
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
58
|
+
# file, and it's useful to allow more verbose output when running an
|
59
|
+
# individual spec file.
|
60
|
+
if config.files_to_run.one?
|
61
|
+
# Use the documentation formatter for detailed output,
|
62
|
+
# unless a formatter has already been configured
|
63
|
+
# (e.g. via a command-line flag).
|
64
|
+
config.default_formatter = 'doc'
|
65
|
+
end
|
66
|
+
|
67
|
+
# Print the 10 slowest examples and example groups at the
|
68
|
+
# end of the spec run, to help surface which specs are running
|
69
|
+
# particularly slow.
|
70
|
+
config.profile_examples = 10
|
71
|
+
|
72
|
+
# Run specs in random order to surface order dependencies. If you find an
|
73
|
+
# order dependency and want to debug it, you can fix the order by providing
|
74
|
+
# the seed, which is printed after each run.
|
75
|
+
# --seed 1234
|
76
|
+
config.order = :random
|
77
|
+
|
78
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
79
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
80
|
+
# test failures related to randomization by passing the same `--seed` value
|
81
|
+
# as the one that triggered the failure.
|
82
|
+
Kernel.srand config.seed
|
83
|
+
=end
|
84
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'stealth-luis'
|
3
|
+
s.version = '0.9.0'
|
4
|
+
s.summary = "Stealth LUIS"
|
5
|
+
s.description = "Built-in NLP for Stealth bots via Microsoft's Language Understanding (LUIS)."
|
6
|
+
s.authors = ["Mauricio Gomes"]
|
7
|
+
s.email = 'mauricio@edge14.com'
|
8
|
+
s.files = `git ls-files`.split("\n")
|
9
|
+
s.homepage = 'http://github.com/hellostealth/stealth-luis'
|
10
|
+
s.license = 'MIT'
|
11
|
+
|
12
|
+
s.add_dependency 'stealth', '~> 2.0'
|
13
|
+
s.add_dependency 'http', '~> 4'
|
14
|
+
|
15
|
+
s.add_development_dependency "rspec", "= 3.8.0"
|
16
|
+
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stealth-luis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mauricio Gomes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: stealth
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: http
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.8.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.8.0
|
55
|
+
description: Built-in NLP for Stealth bots via Microsoft's Language Understanding
|
56
|
+
(LUIS).
|
57
|
+
email: mauricio@edge14.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- README.md
|
67
|
+
- lib/stealth-luis.rb
|
68
|
+
- lib/stealth/nlp/luis.rb
|
69
|
+
- lib/stealth/nlp/luis/client.rb
|
70
|
+
- lib/stealth/nlp/luis/result.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- stealth-luis.gemspec
|
73
|
+
homepage: http://github.com/hellostealth/stealth-luis
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubygems_version: 3.0.6
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Stealth LUIS
|
96
|
+
test_files: []
|