RecastAI 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/Gemfile.lock +1 -1
- data/README.md +93 -7
- data/lib/recastai/client.rb +12 -3
- data/lib/recastai/response.rb +22 -3
- data/lib/recastai/sentence.rb +32 -0
- data/lib/recastai/utils.rb +2 -2
- data/misc/logo-inline.png +0 -0
- data/recastai.gemspec +2 -2
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8fc4edaf1b82d336f4b13e82dd68b45bacc60bd
|
4
|
+
data.tar.gz: 031d2d8409dc6abd4679cfa69b9c92199205bead
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97f198079573ac33474efdc4bdfd4d0c287d30eca3d1f861ce598811bc89ea3bd86793debcf7ef63271841c854505659a7f38932accd01ae94a512abec5b4086
|
7
|
+
data.tar.gz: e26c419f1c4762c9fc446494ba7f9407ea1f769ce784bed6827c3ce2b534430e8312c29448c8132c2ca83d5636e62807ce9f843531336ec9c2bf15ce060ebb31
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
# SDK
|
1
|
+
# RecastAI - Ruby SDK
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/RecastAI)
|
4
|
+
|
5
|
+

|
2
6
|
|
3
7
|
Recast.AI official SDK in Ruby.
|
4
8
|
|
@@ -8,6 +12,11 @@ Recast.AI official SDK in Ruby.
|
|
8
12
|
This gem is a pure Ruby interface to the [Recast.AI](https://recast.ai) API. It allows you to make requests to your bots.
|
9
13
|
|
10
14
|
|
15
|
+
## Requirements
|
16
|
+
|
17
|
+
* Ruby 2.2+
|
18
|
+
|
19
|
+
|
11
20
|
## Installation
|
12
21
|
|
13
22
|
### Via Gemfile
|
@@ -48,7 +57,7 @@ gem install RecastAI-*.gem
|
|
48
57
|
```ruby
|
49
58
|
require 'recastai'
|
50
59
|
|
51
|
-
client = RecastAI::Client.new(YOUR_TOKEN)
|
60
|
+
client = RecastAI::Client.new(YOUR_TOKEN, YOUR_LANGUAGE)
|
52
61
|
response = client.text_request(YOUR_TEXT)
|
53
62
|
#response = client.file_request(File.new(File.join(File.dirname(__FILE__), YOUR_FILE)))
|
54
63
|
|
@@ -57,8 +66,6 @@ if response.intent == YOUR_EXPECTED_INTENT
|
|
57
66
|
end
|
58
67
|
```
|
59
68
|
|
60
|
-
You can find more examples in the `misc/` folder.
|
61
|
-
|
62
69
|
### CLI
|
63
70
|
|
64
71
|
```bash
|
@@ -87,12 +94,44 @@ Don't hesitate to dive into the code, it's commented ;)
|
|
87
94
|
|
88
95
|
### RecastAI::Client
|
89
96
|
|
90
|
-
The Recast.AI Client can be instanciated with a token and provides the two following methods:
|
97
|
+
The Recast.AI Client can be instanciated with a token (optional) and a language (optional) and provides the two following methods:
|
91
98
|
|
92
99
|
* text_request(text, options = {}) *Performs a text request*
|
93
100
|
* file_request(file, options = {}) *Performs a voice file request*
|
94
101
|
|
95
|
-
|
102
|
+
On each call to `text_request` or `file_request` your can override either your token or your language by passing a hash of options.
|
103
|
+
|
104
|
+
If no language is provided in the request, Recast.AI does the following:
|
105
|
+
|
106
|
+
* text_request: the language of the text is detected and is used for processing if your bot has expressions for it, else your bot's primary language is used for processing.
|
107
|
+
* voice_request: your bot's primary language is used for processing as we do not provide language detection for speech.
|
108
|
+
|
109
|
+
If a language is provided, Recast.AI does the following:
|
110
|
+
|
111
|
+
* text_request: the language you've given is used for processing if your bot has expressions for it, else your bot's primary language is used.
|
112
|
+
* voice_request: the language you've given is used for processing if your bot has expressions for it, else your bot's primary language is used
|
113
|
+
|
114
|
+
*Accepted options are :token, :language, to override the defaults provided at initialization*
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
require 'recastai'
|
118
|
+
|
119
|
+
client = RecastAI::Client.new(YOUR_TOKEN, YOUR LANGUAGE)
|
120
|
+
|
121
|
+
# Performs a text request on Recast.AI
|
122
|
+
response = client.text_request(YOUR_TEXT, { token: YOUR_TOKEN, language: YOUR_LANGUAGE })
|
123
|
+
|
124
|
+
if response.intent == YOUR_EXPECTED_INTENT
|
125
|
+
# Do your code...
|
126
|
+
end
|
127
|
+
|
128
|
+
# Performs a voice file request on Recast.AI
|
129
|
+
response = client.file_request(File.new(File.join(File.dirname(__FILE__),YOUR_FILE)), { token: YOUR_TOKEN, language: YOUR_LANGUAGE })
|
130
|
+
|
131
|
+
if response.intent == YOUR_EXPECTED_INTENT
|
132
|
+
# Do your code...
|
133
|
+
end
|
134
|
+
```
|
96
135
|
|
97
136
|
### RecastAI::Response
|
98
137
|
|
@@ -106,10 +145,26 @@ The Recast.AI Response is generated after a call with the two previous methods a
|
|
106
145
|
* sentence(\*) *Returns the first detected sentence*
|
107
146
|
* get(name) *Returns the first entity matching -name-*
|
108
147
|
* all(name) *Returns all the entities matching -name-*
|
148
|
+
* entities(\*) *Returns all the entities*
|
109
149
|
* version(\*) *Returns the version of the JSON*
|
110
150
|
* timestamp(\*) *Returns the timestamp at the end of the processing*
|
111
151
|
* status(\*) *Returns the status of the response*
|
112
152
|
|
153
|
+
```ruby
|
154
|
+
response = client.text_request('Give me some recipes with potatoes. And cheese.')
|
155
|
+
|
156
|
+
# Get the first sentence, aka 'Give me some recipes with potatoes'
|
157
|
+
first_sentence = response.sentence
|
158
|
+
|
159
|
+
# If the first intent matched 'recipe'...
|
160
|
+
if response.intent == 'recipe'
|
161
|
+
# ...get all the entities matching 'ingredient'
|
162
|
+
ingredients = response.all('ingredient')
|
163
|
+
|
164
|
+
puts "The request has been filled at #{response.timestamp}"
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
113
168
|
### RecastAI::Sentence
|
114
169
|
|
115
170
|
The Recast.AI Sentence is generated by the Recast.AI Response initializer and provides the following methods:
|
@@ -119,8 +174,20 @@ The Recast.AI Sentence is generated by the Recast.AI Response initializer and pr
|
|
119
174
|
* action(\*) *Returns the action of the sentence*
|
120
175
|
* agent(\*) *Returns the agent of the sentence*
|
121
176
|
* polarity(\*) *Returns the polarity (negation or not) of the sentence*
|
177
|
+
* get(name) *Returns the first entity matching -name-*
|
178
|
+
* all(name) *Returns all the entities matching -name-*
|
122
179
|
* entities(\*) *Returns all the entities detected in the sentence*
|
123
180
|
|
181
|
+
```ruby
|
182
|
+
response = client.text_request('Tell me a joke.')
|
183
|
+
# Get the first sentence
|
184
|
+
sentence = response.sentence
|
185
|
+
|
186
|
+
if sentence.action == 'tell' && sentence.polarity == 'positive'
|
187
|
+
# Tell a joke...
|
188
|
+
end
|
189
|
+
```
|
190
|
+
|
124
191
|
### RecastAI::Entity
|
125
192
|
|
126
193
|
The Recast.AI Entity is generated by the Recast.AI Sentence initializer and provides the following method:
|
@@ -145,17 +212,36 @@ In addition to this method, more attributes are generated depending of the natur
|
|
145
212
|
* grain(\*)
|
146
213
|
* order(\*)
|
147
214
|
|
215
|
+
```ruby
|
216
|
+
response = client.text_request('What can I cook with salmon ?')
|
217
|
+
|
218
|
+
if response.intent == 'recipe'
|
219
|
+
# Get the ingredient
|
220
|
+
ingredient = response.get('ingredient')
|
221
|
+
|
222
|
+
puts "You asked me for a recipe with #{ingredient.value}"
|
223
|
+
end
|
224
|
+
|
225
|
+
```
|
226
|
+
|
148
227
|
### RecastAI::RecastError
|
149
228
|
|
150
229
|
The Recast.AI Error is thrown when receiving an non-200 response from Recast.AI.
|
151
230
|
|
152
|
-
As it inherits from ::Exception, it implements the default
|
231
|
+
As it inherits from ::Exception, it implements the default exception methods.
|
153
232
|
|
154
233
|
## More
|
155
234
|
|
156
235
|
You can view the whole API reference at [man.recast.ai](https://man.recast.ai).
|
157
236
|
|
158
237
|
|
238
|
+
## Author
|
239
|
+
|
240
|
+
Paul Renvoisé, paul.renvoise@recast.ai, [@paulrenvoise](https://twitter.com/paulrenvoise)
|
241
|
+
|
242
|
+
You can follow us on Twitter at [@recastai](https://twitter.com/recastai) for updates and releases.
|
243
|
+
|
244
|
+
|
159
245
|
## License
|
160
246
|
|
161
247
|
Copyright (c) [2016] [Recast.AI](https://recast.ai)
|
data/lib/recastai/client.rb
CHANGED
@@ -2,8 +2,9 @@ module RecastAI
|
|
2
2
|
class Client
|
3
3
|
attr_accessor :token
|
4
4
|
|
5
|
-
def initialize(token = nil)
|
5
|
+
def initialize(token = nil, language = nil)
|
6
6
|
@token = token
|
7
|
+
@language = language
|
7
8
|
end
|
8
9
|
|
9
10
|
##
|
@@ -20,8 +21,12 @@ module RecastAI
|
|
20
21
|
token = options[:token] || @token
|
21
22
|
raise(RecastError.new('Token is missing')) if token.nil?
|
22
23
|
|
24
|
+
language = options[:language] || @language
|
25
|
+
|
26
|
+
body = { 'text' => text }
|
27
|
+
body['language'] = language unless language.nil?
|
23
28
|
response = HTTParty.post(Utils::API_ENDPOINT,
|
24
|
-
body:
|
29
|
+
body: body,
|
25
30
|
headers: { 'Authorization' => "Token #{token}" }
|
26
31
|
)
|
27
32
|
raise(RecastError.new(response.message)) if response.code != 200
|
@@ -43,8 +48,12 @@ module RecastAI
|
|
43
48
|
token = options[:token] || @token
|
44
49
|
raise(RecastError.new('Token is missing')) if token.nil?
|
45
50
|
|
51
|
+
language = options[:language] || @language
|
52
|
+
|
53
|
+
body = { 'voice' => File.new(file) }
|
54
|
+
body['language'] = language unless language.nil?
|
46
55
|
response = HTTMultiParty.post(Utils::API_ENDPOINT,
|
47
|
-
body:
|
56
|
+
body: body,
|
48
57
|
headers: { 'Authorization' => "Token #{token}" }
|
49
58
|
)
|
50
59
|
raise(RecastError.new(response.message)) if response.code != 200
|
data/lib/recastai/response.rb
CHANGED
@@ -4,6 +4,7 @@ module RecastAI
|
|
4
4
|
attr_reader :source
|
5
5
|
attr_reader :intents
|
6
6
|
attr_reader :sentences
|
7
|
+
attr_reader :language
|
7
8
|
attr_reader :version
|
8
9
|
attr_reader :timestamp
|
9
10
|
attr_reader :status
|
@@ -17,6 +18,7 @@ module RecastAI
|
|
17
18
|
@source = response['source']
|
18
19
|
@intents = response['intents']
|
19
20
|
@sentences = response['sentences'].map{ |s| Sentence.new(s) }
|
21
|
+
@language = response['language']
|
20
22
|
@version = response['version']
|
21
23
|
@timestamp = response['timestamp']
|
22
24
|
@status = response['status']
|
@@ -52,7 +54,7 @@ module RecastAI
|
|
52
54
|
def get(name)
|
53
55
|
@sentences.each do |sentence|
|
54
56
|
sentence.entities.each do |entity|
|
55
|
-
return entity if entity.name.
|
57
|
+
return entity if entity.name.casecmp(name.to_s) == 0
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
@@ -66,12 +68,29 @@ module RecastAI
|
|
66
68
|
# - +name+ - String, the entities' names
|
67
69
|
# * *Returns* :
|
68
70
|
# - An array of instances of Entity or an empty array
|
69
|
-
def all(name
|
71
|
+
def all(name)
|
70
72
|
entities = []
|
71
73
|
|
72
74
|
@sentences.each do |sentence|
|
73
75
|
sentence.entities.each do |entity|
|
74
|
-
entities << entity if entity.name.
|
76
|
+
entities << entity if entity.name.casecmp(name.to_s) == 0
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
entities
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Returns all entities
|
85
|
+
#
|
86
|
+
# * *Returns* :
|
87
|
+
# - An array of instances of Entity or an empty array
|
88
|
+
def entities
|
89
|
+
entities = []
|
90
|
+
|
91
|
+
@sentences.each do |sentence|
|
92
|
+
sentence.entities.each do |entity|
|
93
|
+
entities << entity
|
75
94
|
end
|
76
95
|
end
|
77
96
|
|
data/lib/recastai/sentence.rb
CHANGED
@@ -15,5 +15,37 @@ module RecastAI
|
|
15
15
|
@polarity = sentence['polarity']
|
16
16
|
@entities = sentence['entities'].flat_map{ |n, e| e.map{ |ee| Entity.new(n, ee) } }
|
17
17
|
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Returns the first entity whose name matches the parameter
|
21
|
+
#
|
22
|
+
# * *Args* :
|
23
|
+
# - +name+ - String, the entity's name
|
24
|
+
# * *Returns* :
|
25
|
+
# - An instance of Entity or nil
|
26
|
+
def get(name)
|
27
|
+
@entities.each do |entity|
|
28
|
+
return entity if entity.name.casecmp(name.to_s) == 0
|
29
|
+
end
|
30
|
+
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
##
|
35
|
+
# Returns all entities whose names matches the parameter
|
36
|
+
#
|
37
|
+
# * *Args* :
|
38
|
+
# - +name+ - String, the entities' names
|
39
|
+
# * *Returns* :
|
40
|
+
# - An array of instances of Entity or an empty array
|
41
|
+
def all(name)
|
42
|
+
entities = []
|
43
|
+
|
44
|
+
@entities.each do |entity|
|
45
|
+
entities << entity if entity.name.casecmp(name.to_s) == 0
|
46
|
+
end
|
47
|
+
|
48
|
+
entities
|
49
|
+
end
|
18
50
|
end
|
19
51
|
end
|
data/lib/recastai/utils.rb
CHANGED
Binary file
|
data/recastai.gemspec
CHANGED
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.name = 'RecastAI'
|
11
11
|
spec.version = RecastAI::Utils::VERSION
|
12
12
|
spec.date = Date.today
|
13
|
-
spec.summary = %q(Recast.AI official SDK
|
14
|
-
spec.description = %q(Recast.AI official SDK
|
13
|
+
spec.summary = %q(Recast.AI official SDK for Ruby)
|
14
|
+
spec.description = %q(Recast.AI official SDK for Ruby. Allows you to make requests to your bots.)
|
15
15
|
spec.homepage = 'https://github.com/RecastAI/SDK-ruby'
|
16
16
|
spec.license = 'MIT'
|
17
17
|
spec.authors = ['Paul Renvoisé']
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: RecastAI
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Renvoisé
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -94,7 +94,8 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.11'
|
97
|
-
description: Recast.AI official SDK
|
97
|
+
description: Recast.AI official SDK for Ruby. Allows you to make requests to your
|
98
|
+
bots.
|
98
99
|
email: paul.renvoise@recast.ai
|
99
100
|
executables:
|
100
101
|
- text
|
@@ -119,6 +120,7 @@ files:
|
|
119
120
|
- lib/recastai/sentence.rb
|
120
121
|
- lib/recastai/utils.rb
|
121
122
|
- misc/.gitkeep
|
123
|
+
- misc/logo-inline.png
|
122
124
|
- recastai.gemspec
|
123
125
|
homepage: https://github.com/RecastAI/SDK-ruby
|
124
126
|
licenses:
|
@@ -143,5 +145,5 @@ rubyforge_project:
|
|
143
145
|
rubygems_version: 2.5.1
|
144
146
|
signing_key:
|
145
147
|
specification_version: 4
|
146
|
-
summary: Recast.AI official SDK
|
148
|
+
summary: Recast.AI official SDK for Ruby
|
147
149
|
test_files: []
|