applied 0.0.2 → 0.1.0

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: 14b98e71134bfd46b00b8fc161382faa3e680708
4
- data.tar.gz: 4e1f85664c0eeb6e68427c43a8457c170ccab40f
3
+ metadata.gz: 8a3cf39a6d9282c7914f90ecf9205e8e42c4eb5f
4
+ data.tar.gz: 257bf3953365209c281b6012d2ef2ab79a2938f9
5
5
  SHA512:
6
- metadata.gz: d5262e492fd0e3837593aa979e0147fb5074c9535ad04413222cda826a9d9083a35d7a2083c806c935922d44de6458df9289944d668edf3ee62247d140cfc24f
7
- data.tar.gz: 563b4113e79ed379682fd4101b928694a8c7e3c5a2b088025b834a694c6e7c0e8132584f10226a09026e1d010a72a95d6743ed8fd18981f275683406b41c110b
6
+ metadata.gz: 7df7c4cbde9618000c100924b973267e6eba93512a7d041189371dabfb3303760d9a8e5ff999a1936b17ead84b31e018554a3c06fbafc1d8229761de64eb0b45
7
+ data.tar.gz: d6944efa7baf8f07bb0bd3e35143d439e983da78c9d9bf3f71ca11ecc6f97e8b74dc6dce30ab1eb86dacb38b00f17d5a2a992717e8b7da5f857124c76ed4cbc3
data/README.md CHANGED
@@ -1,29 +1,75 @@
1
- # Applied
1
+ # [![Ruby Gem Icon](https://raw.githubusercontent.com/zenkay/ai-applied-ruby/master/rubygem.png)](https://rubygems.org/gems/applied) AI Applied Ruby Gem
2
2
 
3
- TODO: Write a gem description
3
+ [![Code Climate](https://codeclimate.com/github/zenkay/ai-applied-ruby/badges/gpa.svg)](https://codeclimate.com/github/zenkay/ai-applied-ruby) [![Travis CI](https://travis-ci.org/zenkay/ai-applied-ruby.svg?branch=master)](https://travis-ci.org/zenkay/applied-ruby) [![Gem Version](https://badge.fury.io/rb/applied.svg)](http://badge.fury.io/rb/applied) [![Coverage Status](https://coveralls.io/repos/zenkay/ai-applied-ruby/badge.png?branch=master)](https://coveralls.io/r/zenkay/ai-applied-ruby?branch=master)
4
4
 
5
5
  ## Installation
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'applied'
9
+ ```
10
+ gem 'applied'
11
+ ```
10
12
 
11
13
  And then execute:
12
14
 
13
- $ bundle
15
+ ```
16
+ $ bundle install
17
+ ```
18
+ ## Setup
14
19
 
15
- Or install it yourself as:
20
+ Setup configuration parameters
16
21
 
17
- $ gem install applied
22
+ ```
23
+ Applied.configure do |c|
24
+ c.api_key = "your-api-key-for-applied-account"
25
+ c.endpoint = "http://api.ai-applied.nl/"
26
+ end
27
+ ```
18
28
 
19
29
  ## Usage
20
30
 
21
- TODO: Write usage instructions here
31
+ ### Sentiment API
22
32
 
23
- ## Contributing
33
+ - [Class Documentation](docs/sentiment_details.md)
34
+ - [Usage Examples](docs/sentiment_examples.md)
35
+
36
+ ```
37
+ element = Applied::Sentiment.new
38
+
39
+ options = {return_original: false, classifier: "default"}
40
+
41
+ data = [
42
+ {text: "Sono molto contento di quello che è successo", language_iso: "ita", id: 42},
43
+ {text: "Sono molto arrabbiato per quello che è successo", language_iso: "ita", id: 69},
44
+ {text: "Sono molto indifferente a quello che è successo", language_iso: "ita", id: 99}
45
+ ]
46
+
47
+ response = element.analyze(data, options)
48
+ ```
49
+
50
+ - _[Official documentation](http://ai-applied.nl/api-documentation/2013/10/5/sentiment-analysis-api-documentation)_
51
+
52
+ ### Text Analysis API
53
+
54
+ Coming Soon
55
+
56
+ ### Text Extract API
57
+
58
+ Coming Soon
59
+
60
+ ### Data Miner API
61
+
62
+ Coming Soon
63
+
64
+ ### Text Label API
65
+
66
+ Coming Soon
67
+
68
+ ### Demographics API
69
+
70
+ Coming Soon
71
+
72
+ ### Language Detection API
73
+
74
+ Coming Soon
24
75
 
25
- 1. Fork it ( https://github.com/[my-github-username]/applied/fork )
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create a new Pull Request
@@ -0,0 +1,95 @@
1
+ # Sentiment API Documentation
2
+
3
+ - [Setup](#setup)
4
+ - [Parameters](#parameters)
5
+ - [Response](#response)
6
+
7
+ ## Setup
8
+
9
+ First you need to configure your account adding your API key. Endpoint sohould be always the same but I keep it configurable.
10
+
11
+ ```
12
+ Applied.configure do |c|
13
+ c.api_key = "your-api-key-for-applied-account"
14
+ c.endpoint = "http://api.ai-applied.nl/"
15
+ end
16
+ ```
17
+
18
+ Then you need to create a new ```Sentiment``` object
19
+
20
+ ```
21
+ element = Applied::Sentiment.new
22
+ ```
23
+
24
+ ## Parameters
25
+
26
+ The ```analyze``` method takes two parameters: ```data``` and ```options```.
27
+
28
+ ### data
29
+
30
+ The ```data``` parameter contains text you need to analyze with additional informations. Here is an example item:
31
+
32
+ ```
33
+ {
34
+ text: "Sono molto contento di quello che è successo",
35
+ language_iso: "ita",
36
+ id: 42
37
+ }
38
+ ```
39
+
40
+ | Parameter | Mandatory | Description |
41
+ | --------- | --------- | ----------- |
42
+ | text | yes | The message text as a string |
43
+ | language_iso | yes | Specifying the language of this individual message (```eng``` for English, ```nld``` for Dutch, ```deu``` for German, ```fra``` for French, ```spa``` for Spanish, ```ita``` for Italian, ```rus``` for Russian) |
44
+ | id | yes | Unique message ID as a string or an integer you could use to identify your messages into response |
45
+
46
+ Data item should be placed into an Array.
47
+ You could send more than one message in a single call.
48
+
49
+ ```
50
+ data = [
51
+ {text: "Sono molto contento di quello che è successo", language_iso: "ita", id: 42},
52
+ {text: "Sono molto arrabbiato per quello che è successo", language_iso: "ita", id: 69},
53
+ {text: "Sono molto indifferente a quello che è successo", language_iso: "ita", id: 99}
54
+ ]
55
+ ```
56
+
57
+ ### options
58
+
59
+ The ```options``` parameter set configuration of the analysis.
60
+
61
+ | Parameter | Mandatory | Default | Description |
62
+ | --------- | --------- | ------- | ----------- |
63
+ | return_original | yes | false | Return full posted messages (true) together with the language annotation or only the message id's (false) annotated with language |
64
+ | classifier | yes | "default" | Specifies which classifier to use. Sentiment Analysis API offers two standard classifiers, "default" and "subjective". The "default" classifier provides a two-class classification ("positive"/"negative"), while the "subjective" classifier provides the "neutral" class as well. |
65
+
66
+ ```
67
+ options = {
68
+ return_original: false,
69
+ classifier: "default"
70
+ }
71
+ ```
72
+
73
+ ## Response
74
+
75
+ ```
76
+ response = element.analyze(data, options)
77
+ ```
78
+
79
+ Ai Applied return a JSON data that is parsed to a Ruby Hash.
80
+
81
+ ```
82
+ {
83
+ "status" => 1,
84
+ "id" => nil,
85
+ "response" => {
86
+ "data" => [
87
+ {"confidence_sentiment" => 0.735094833636812, "sentiment_class" => "positive", "id" => 42},
88
+ {"confidence_sentiment" => 0.9914339753287233, "sentiment_class" => "negative", "id" => 69},
89
+ {"confidence_sentiment" => 0.7537000605846166, "sentiment_class" => "negative", "id" => 99}
90
+ ],
91
+ "description" => "OK: Call processed.",
92
+ "success" => true
93
+ }
94
+ }
95
+ ```
@@ -0,0 +1 @@
1
+ # Sentiment API Examples
@@ -7,11 +7,49 @@ module Applied
7
7
  class Sentiment
8
8
 
9
9
  ENDPOINT = "/api/sentiment_api/"
10
+ PERMITTED_OPTIONS = [:return_original, :classifier]
11
+ CLASSIFIERS = ["default", "subjective"]
12
+ AVAILABLE_LANGUAGES = ["eng", "nld", "deu", "fra", "spa", "ita", "rus"]
13
+
14
+ DATA_MUST_BE_ARRAY = "Data must be and Array"
15
+ TEXT_MISSING = "Missing text parameter, it is mandatory"
16
+ ID_MISSING = "Missing ID parameter, it is mandatory"
17
+ LANGUAGE_MISSING = "Missing language parameter, it is mandatory"
18
+ BAD_RETURN = "Bad value for return_original parameter. Allowed values are: true, false"
19
+ BAD_CLASSIFIER = "Bad value for classifier parameter. Allowed values are: 'default', 'subjective'"
10
20
 
11
21
  attr_accessor :return_original, :classifier
12
22
 
13
- def analyze(data, options)
14
- params = options
23
+ def analyze(data, options = {})
24
+
25
+ # checks on data
26
+
27
+ raise Applied::BadData.new(DATA_MUST_BE_ARRAY) unless data.instance_of? Array
28
+
29
+ data.each do |d|
30
+ raise Applied::BadData.new(TEXT_MISSING) if d[:text].nil? or d[:text].empty?
31
+ raise Applied::BadData.new(ID_MISSING) unless [Fixnum, String].include? d[:id].class
32
+ raise Applied::BadData.new(LANGUAGE_MISSING) if d[:language_iso].nil? or d[:language_iso].empty? or not AVAILABLE_LANGUAGES.include? d[:language_iso]
33
+ end
34
+
35
+ # checks on options
36
+
37
+ unless options[:return_original].nil?
38
+ raise Applied::BadOptions.new(BAD_RETURN) unless [true, false].include? options[:return_original]
39
+ else
40
+ options[:return_original] = false
41
+ end
42
+
43
+ unless options[:classifier].nil?
44
+ raise Applied::BadOptions.new(BAD_CLASSIFIER) unless CLASSIFIERS.include? options[:classifier]
45
+ else
46
+ options[:classifier] = "default"
47
+ end
48
+
49
+ # options cleanup
50
+
51
+ options.delete_if {|o| not PERMITTED_OPTIONS.include? o}
52
+
15
53
  call(ENDPOINT, data, options)
16
54
  end
17
55
 
@@ -43,4 +81,9 @@ module Applied
43
81
 
44
82
  end
45
83
 
84
+ class BadOptions < Exception; end
85
+
86
+ class BadData < Exception; end
87
+
88
+
46
89
  end
@@ -1,3 +1,3 @@
1
1
  module Applied
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
Binary file
@@ -22,7 +22,26 @@ describe Applied::Sentiment, vcr: vcr_options do
22
22
  options = {return_original: false, classifier: "default"}
23
23
  data = [{text: "Sono molto contento di quello che è successo", language_iso: "ita", id: 42}]
24
24
  response = element.analyze(data, options)
25
+ expect(response).not_to be_empty
26
+ expect(response["status"]).to eq 1
27
+ expect(response["response"]).to be_a Hash
28
+ expect(response["response"]["data"]).to be_a Array
29
+ response["response"]["data"].each do |d|
30
+ expect(d["sentiment_class"]).to match /positive|negative|unknown/
31
+ expect(d["id"]).to be 42
32
+ expect(d["confidence_sentiment"]).to be_between(0.0, 1.0)
33
+ end
34
+ end
25
35
 
36
+ it "make multiple request sentiment of an italian sentence" do
37
+ element = Applied::Sentiment.new
38
+ options = {return_original: false, classifier: "default"}
39
+ data = [
40
+ {text: "Sono molto contento di quello che è successo", language_iso: "ita", id: 42},
41
+ {text: "Sono molto arrabbiato per quello che è successo", language_iso: "ita", id: 69},
42
+ {text: "Sono molto indifferente a quello che è successo", language_iso: "ita", id: 99}
43
+ ]
44
+ response = element.analyze(data, options)
26
45
  expect(response).not_to be_empty
27
46
  expect(response["status"]).to eq 1
28
47
  expect(response["response"]).to be_a Hash
@@ -34,13 +53,53 @@ describe Applied::Sentiment, vcr: vcr_options do
34
53
  end
35
54
  end
36
55
 
37
- it "raise exception on wrong config parameters" do
56
+ it "make a request sentiment of an italian sentence using default options" do
57
+ element = Applied::Sentiment.new
58
+ data = [{text: "Sono molto contento di quello che è successo", language_iso: "ita", id: 42}]
59
+ response = element.analyze(data)
60
+ expect(response).not_to be_empty
61
+ expect(response["status"]).to eq 1
62
+ expect(response["response"]).to be_a Hash
63
+ expect(response["response"]["data"]).to be_a Array
64
+ response["response"]["data"].each do |d|
65
+ expect(d["sentiment_class"]).to match /positive|negative|unknown/
66
+ expect(d["id"]).to be 42
67
+ expect(d["confidence_sentiment"]).to be_between(0.0, 1.0)
68
+ end
69
+ end
70
+
71
+ it "raise BadData on missing text" do
72
+ element = Applied::Sentiment.new
73
+ expect { element.analyze([{language_iso: "ita", id: 42}]) }.to raise_error(Applied::BadData)
74
+ end
75
+
76
+ it "raise BadData on missing language" do
77
+ element = Applied::Sentiment.new
78
+ expect { element.analyze([{text: "test", id: 42}]) }.to raise_error(Applied::BadData)
79
+ end
80
+
81
+ it "raise BadData on missing text" do
82
+ element = Applied::Sentiment.new
83
+ expect { element.analyze([{text: "test", language_iso: "ita"}]) }.to raise_error(Applied::BadData)
84
+ end
85
+
86
+ it "raise BadOptions on wrong return_original" do
87
+ element = Applied::Sentiment.new
88
+ expect { element.analyze([{text: "test", language_iso: "ita", id: 42}], {return_original: "maybe"}) }.to raise_error(Applied::BadOptions)
89
+ end
90
+
91
+ it "raise BadOptions on wrong classifier" do
92
+ element = Applied::Sentiment.new
93
+ expect { element.analyze([{text: "test", language_iso: "ita", id: 42}], {classifier: "cool"}) }.to raise_error(Applied::BadOptions)
94
+ end
95
+
96
+ it "raise BadResponse on wrong config parameters" do
38
97
  Applied.configure do |c|
39
98
  c.api_key = "bad-app-id"
40
99
  c.endpoint = "not-an-url-endpoint"
41
100
  end
42
101
  element = Applied::Sentiment.new
43
- expect { element.analyze({text: "test"}, []) }.to raise_error(Applied::BadResponse)
102
+ expect { element.analyze([{text: "test", language_iso: "ita", id: 42}]) }.to raise_error(Applied::BadResponse)
44
103
  end
45
104
 
46
105
  end
metadata CHANGED
@@ -1,151 +1,151 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: applied
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrea Mostosi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-26 00:00:00.000000000 Z
11
+ date: 2014-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0.9'
20
- - - '>='
20
+ - - ">="
21
21
  - !ruby/object:Gem::Version
22
22
  version: 0.9.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ~>
27
+ - - "~>"
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0.9'
30
- - - '>='
30
+ - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 0.9.0
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: faraday_middleware
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ~>
37
+ - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0.9'
40
- - - '>='
40
+ - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  version: 0.9.1
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - ~>
47
+ - - "~>"
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0.9'
50
- - - '>='
50
+ - - ">="
51
51
  - !ruby/object:Gem::Version
52
52
  version: 0.9.1
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: bundler
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - ~>
57
+ - - "~>"
58
58
  - !ruby/object:Gem::Version
59
59
  version: '1.3'
60
60
  type: :development
61
61
  prerelease: false
62
62
  version_requirements: !ruby/object:Gem::Requirement
63
63
  requirements:
64
- - - ~>
64
+ - - "~>"
65
65
  - !ruby/object:Gem::Version
66
66
  version: '1.3'
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: rspec
69
69
  requirement: !ruby/object:Gem::Requirement
70
70
  requirements:
71
- - - ~>
71
+ - - "~>"
72
72
  - !ruby/object:Gem::Version
73
73
  version: '2.12'
74
74
  type: :development
75
75
  prerelease: false
76
76
  version_requirements: !ruby/object:Gem::Requirement
77
77
  requirements:
78
- - - ~>
78
+ - - "~>"
79
79
  - !ruby/object:Gem::Version
80
80
  version: '2.12'
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rake
83
83
  requirement: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - ~>
85
+ - - "~>"
86
86
  - !ruby/object:Gem::Version
87
87
  version: '10.1'
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - ~>
92
+ - - "~>"
93
93
  - !ruby/object:Gem::Version
94
94
  version: '10.1'
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: vcr
97
97
  requirement: !ruby/object:Gem::Requirement
98
98
  requirements:
99
- - - ~>
99
+ - - "~>"
100
100
  - !ruby/object:Gem::Version
101
101
  version: '2.4'
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  requirements:
106
- - - ~>
106
+ - - "~>"
107
107
  - !ruby/object:Gem::Version
108
108
  version: '2.4'
109
109
  - !ruby/object:Gem::Dependency
110
110
  name: webmock
111
111
  requirement: !ruby/object:Gem::Requirement
112
112
  requirements:
113
- - - ~>
113
+ - - "~>"
114
114
  - !ruby/object:Gem::Version
115
115
  version: '1.17'
116
116
  type: :development
117
117
  prerelease: false
118
118
  version_requirements: !ruby/object:Gem::Requirement
119
119
  requirements:
120
- - - ~>
120
+ - - "~>"
121
121
  - !ruby/object:Gem::Version
122
122
  version: '1.17'
123
123
  - !ruby/object:Gem::Dependency
124
124
  name: simplecov
125
125
  requirement: !ruby/object:Gem::Requirement
126
126
  requirements:
127
- - - ~>
127
+ - - "~>"
128
128
  - !ruby/object:Gem::Version
129
129
  version: '0.8'
130
130
  type: :development
131
131
  prerelease: false
132
132
  version_requirements: !ruby/object:Gem::Requirement
133
133
  requirements:
134
- - - ~>
134
+ - - "~>"
135
135
  - !ruby/object:Gem::Version
136
136
  version: '0.8'
137
137
  - !ruby/object:Gem::Dependency
138
138
  name: coveralls
139
139
  requirement: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - ~>
141
+ - - "~>"
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0.7'
144
144
  type: :development
145
145
  prerelease: false
146
146
  version_requirements: !ruby/object:Gem::Requirement
147
147
  requirements:
148
- - - ~>
148
+ - - "~>"
149
149
  - !ruby/object:Gem::Version
150
150
  version: '0.7'
151
151
  description: Ruby Gem for AI Applied service (ai-applied.nl)
@@ -155,16 +155,19 @@ executables: []
155
155
  extensions: []
156
156
  extra_rdoc_files: []
157
157
  files:
158
- - .gitignore
159
- - .travis.yml
158
+ - ".gitignore"
159
+ - ".travis.yml"
160
160
  - Gemfile
161
161
  - LICENSE.txt
162
162
  - README.md
163
163
  - Rakefile
164
164
  - applied.gemspec
165
+ - docs/sentiment_details.md
166
+ - docs/sentiment_examples.md
165
167
  - lib/applied.rb
166
168
  - lib/applied/sentiment.rb
167
169
  - lib/applied/version.rb
170
+ - rubygem.png
168
171
  - spec/cassettes/applied_sentiment.yml
169
172
  - spec/sentiment_spec.rb
170
173
  - spec/spec_helper.rb
@@ -178,12 +181,12 @@ require_paths:
178
181
  - lib
179
182
  required_ruby_version: !ruby/object:Gem::Requirement
180
183
  requirements:
181
- - - ~>
184
+ - - "~>"
182
185
  - !ruby/object:Gem::Version
183
186
  version: '2.0'
184
187
  required_rubygems_version: !ruby/object:Gem::Requirement
185
188
  requirements:
186
- - - '>='
189
+ - - ">="
187
190
  - !ruby/object:Gem::Version
188
191
  version: '0'
189
192
  requirements: []
@@ -196,3 +199,4 @@ test_files:
196
199
  - spec/cassettes/applied_sentiment.yml
197
200
  - spec/sentiment_spec.rb
198
201
  - spec/spec_helper.rb
202
+ has_rdoc: