wit-ruby 0.0.1 → 0.0.2
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/README.md +77 -0
- data/lib/wit/version.rb +1 -1
- data/lib/wit/wit.rb +30 -24
- data/spec/wit/wit_spec.rb +2 -2
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac3ac77f6ced1ee1c8fd03093b35d4746c81c30e
|
4
|
+
data.tar.gz: 0cb58a138f3bb3d08ff07f51ae406fd332893c48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b69a88dd619449ba409d51905b584260c8362e322329c87085f9bec41cc003a018fed95f7681a0a8f2d2d56f501d03ee9f6daa8056a19ca3d8703f865004995
|
7
|
+
data.tar.gz: 7f32a52883faf74eaa789a93d27f626e9b1357c56df9b0c57522d396be8a10f167f8481fe2ab5dd42e0547572cdc1f22225b00381d8e9a50b52f9788c4a75eda
|
data/README.md
CHANGED
@@ -3,11 +3,88 @@ wit-ruby
|
|
3
3
|
|
4
4
|
[](http://badge.fury.io/rb/wit-ruby)
|
5
5
|
|
6
|
+
Easy interface for interacting with the [Wit.ai](http://wit.ai) natural language parsing API.
|
7
|
+
|
8
|
+
This project will expand as the Wit.ai API expands, but as it stands there's a single endpoint. You can hit this
|
9
|
+
endpoint easily with `Wit.message([your message])`, which uses Wit.ai to convert that phrase or sentence into an object
|
10
|
+
with an intent, and entities if any are available.
|
11
|
+
|
12
|
+
You will need to create a Wit.ai account and begin training it.
|
13
|
+
|
6
14
|
## Installation
|
7
15
|
|
16
|
+
```ruby
|
17
|
+
gem 'wit-ruby', '>= 0.0.2'
|
18
|
+
```
|
19
|
+
|
20
|
+
You'll need to set an environment variable named `WIT_TOKEN` or specify your token with `Wit.token = [your token]`.
|
21
|
+
|
22
|
+
```shell
|
23
|
+
export WIT_TOKEN=[your token]
|
24
|
+
```
|
25
|
+
|
8
26
|
|
9
27
|
## Usage
|
10
28
|
|
29
|
+
```ruby
|
30
|
+
result = Wit.message('Hi')
|
31
|
+
result.intent # will be Hello with the default Wit instance.
|
32
|
+
result.confidence # will be relatively low initially.
|
33
|
+
result.entities # will be {}, but if there are entities the can accessed as [:name] or .name
|
34
|
+
```
|
35
|
+
|
36
|
+
### Result properties/methods
|
37
|
+
|
38
|
+
<dl>
|
39
|
+
|
40
|
+
<dt> raw </dt><dd>
|
41
|
+
Raw response hash (parsed JSON)
|
42
|
+
</dd>
|
43
|
+
|
44
|
+
<dt> msgId </dt><dd>
|
45
|
+
The unique message id provided back from Wit.
|
46
|
+
</dd>
|
47
|
+
|
48
|
+
<dt> msgBody </dt><dd>
|
49
|
+
The original message sent.
|
50
|
+
</dd>
|
51
|
+
|
52
|
+
<dt> intent </dt><dd>
|
53
|
+
The intent, as determined by Wit.
|
54
|
+
</dd>
|
55
|
+
|
56
|
+
<dt> confidence </dt><dd>
|
57
|
+
The confidence level that Wit determined.
|
58
|
+
</dd>
|
59
|
+
|
60
|
+
<dt> entities </dt><dd>
|
61
|
+
Hash of entities, which contain the value, and the start/end position within msgBody.
|
62
|
+
</dd>
|
63
|
+
|
64
|
+
</dl>
|
65
|
+
|
66
|
+
### Entity properties/methods
|
67
|
+
|
68
|
+
<dl>
|
69
|
+
|
70
|
+
<dt> value </dt><dd>
|
71
|
+
The value as determined by Wit (might not be the same as body).
|
72
|
+
</dd>
|
73
|
+
|
74
|
+
<dt> start </dt><dd>
|
75
|
+
The start position index from msgBody.
|
76
|
+
</dd>
|
77
|
+
|
78
|
+
<dt> end </dt><dd>
|
79
|
+
The end position index from msgBody.
|
80
|
+
</dd>
|
81
|
+
|
82
|
+
<dt> body </dt><dd>
|
83
|
+
The actual value as specified in msgBody.
|
84
|
+
</dd>
|
85
|
+
|
86
|
+
</dl>
|
87
|
+
|
11
88
|
|
12
89
|
## License
|
13
90
|
|
data/lib/wit/version.rb
CHANGED
data/lib/wit/wit.rb
CHANGED
@@ -1,30 +1,37 @@
|
|
1
|
-
require '
|
1
|
+
require 'faraday'
|
2
2
|
require 'json'
|
3
|
+
require 'ostruct'
|
3
4
|
|
4
5
|
module Wit
|
5
6
|
|
6
|
-
|
7
|
+
class << self
|
8
|
+
attr_accessor :token
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
http = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
11
|
-
req = Net::HTTP::Get.new(uri)
|
12
|
-
req['Authorization'] = "Bearer #{TOKEN}"
|
13
|
-
http.request(req)
|
10
|
+
def token
|
11
|
+
@token || ENV['WIT_TOKEN']
|
14
12
|
end
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
15
|
+
def self.message(message = '')
|
16
|
+
response = connection.get do |req|
|
17
|
+
req.headers['Authorization'] = "Bearer #{token}"
|
18
|
+
req.url '/message', q: message
|
18
19
|
end
|
19
20
|
|
20
|
-
|
21
|
-
|
21
|
+
case response.status
|
22
|
+
when 200 then return Result.new JSON.parse(response.body)
|
23
|
+
when 401 then raise Unauthorized, "incorrect token set for Wit.token set an env for WIT_TOKEN or set Wit::TOKEN manually"
|
24
|
+
else raise BadResponse, "response code: #{response.status}"
|
22
25
|
end
|
26
|
+
end
|
23
27
|
|
24
|
-
|
28
|
+
def self.connection
|
29
|
+
@connection ||= Faraday.new url: 'https://api.wit.ai' do |faraday|
|
30
|
+
faraday.adapter Faraday.default_adapter
|
31
|
+
end
|
25
32
|
end
|
26
33
|
|
27
|
-
class
|
34
|
+
class Result
|
28
35
|
|
29
36
|
attr_reader :raw, :msg_id, :msg_body, :intent, :confidence, :entities
|
30
37
|
|
@@ -34,26 +41,25 @@ module Wit
|
|
34
41
|
@msg_body = hash['msg_body']
|
35
42
|
@intent = hash['outcome']['intent']
|
36
43
|
@confidence = hash['outcome']['confidence']
|
37
|
-
@entities =
|
44
|
+
@entities = EntityCollection.new
|
38
45
|
hash['outcome']['entities'].each do |name, entity|
|
39
|
-
@entities
|
46
|
+
@entities.send(:"#{name}=", Entity.new(entity))
|
40
47
|
end
|
41
48
|
end
|
42
49
|
|
43
50
|
end
|
44
51
|
|
45
|
-
class
|
52
|
+
class EntityCollection < OpenStruct
|
46
53
|
|
47
|
-
|
48
|
-
|
49
|
-
def initialize(hash)
|
50
|
-
@start = hash['start']
|
51
|
-
@end = hash['end']
|
52
|
-
@value = hash['value']
|
53
|
-
@body = hash['body']
|
54
|
+
def [](name)
|
55
|
+
self.send(name)
|
54
56
|
end
|
55
57
|
|
56
58
|
end
|
57
59
|
|
60
|
+
class Entity < OpenStruct
|
61
|
+
end
|
62
|
+
|
58
63
|
class Unauthorized < Exception; end
|
64
|
+
class BadResponse < Exception; end
|
59
65
|
end
|
data/spec/wit/wit_spec.rb
CHANGED
@@ -6,10 +6,10 @@ describe Wit do
|
|
6
6
|
|
7
7
|
it "returns the expected output" do
|
8
8
|
res = Wit.message('deploy master foo to bar')
|
9
|
-
expect( res ).to be_a Wit::
|
9
|
+
expect( res ).to be_a Wit::Result
|
10
10
|
expect( res.msg_body ).to eq 'deploy master foo to bar'
|
11
11
|
expect( res.intent ).to eq 'deploy'
|
12
|
-
expect( res.entities
|
12
|
+
expect( res.entities.branch ).to be_a Wit::Entity
|
13
13
|
expect( res.entities[:branch].value ).to eq 'master'
|
14
14
|
end
|
15
15
|
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wit-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Jackson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
12
|
-
dependencies:
|
11
|
+
date: 2013-10-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.8
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.8
|
13
27
|
description: A simple library for interacting with wit.ai -- will expand as the api
|
14
28
|
does
|
15
29
|
email:
|