sentiment 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sentiment.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nikolai Manek
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # Sentiment
2
+
3
+ Sentiment is a simple gem which allows you to get the sentiment of a string. It returns 1 for positive words or sentences, -1 for negative ones and 0 if neutral.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sentiment'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sentiment
18
+
19
+ ## Basic usage
20
+
21
+ Sentiment Analysis
22
+
23
+ require 'sentiment'
24
+ client = Sentiment::Client.new("8w7d64oqweryiqweo87qnxtqwi47xqn3i4tisq7")
25
+
26
+ sentiment = client.sentiment("I love Ruby!","en")
27
+ `=> 1`
28
+
29
+ or
30
+ sentiment = client.sentiment("amore","it")
31
+
32
+ `=> 1`
33
+
34
+ At this time the API supports the following languages:
35
+ * English (en)
36
+ * German (de)
37
+ * Italian (it)
38
+ * Spanish (es)
39
+ * French (fr)
40
+ * Turkish (tr)
41
+
42
+
43
+ KNOWN ISSUE:
44
+ In case you get a `"NoMethodError: undefined method `stringify_keys' for #<HTTParty::Response:0x007fa9231ab1b0>"`
45
+ - It's telling you that the api key is invalid and therefore can't handle the server response. Please get a valid api key. The key in this document can be used for limited testing as well.
46
+
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'mash'
3
+ require 'mash'
4
+ gem 'httparty'
5
+ require 'httparty'
6
+
7
+
8
+ class APIKeyNotSet < StandardError; end
9
+
10
+ module Sentiment
11
+
12
+ # Get your API key from https://developer.apphera.com
13
+ def self.api_key
14
+ raise APIKeyNotSet if @api_key.nil?
15
+ @api_key
16
+ end
17
+
18
+ def self.api_key=(api_key)
19
+ @api_key = api_key
20
+ end
21
+
22
+ end
23
+
24
+ directory = File.expand_path(File.dirname(__FILE__))
25
+
26
+ require File.join(directory, 'sentiment', 'client')
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Sentiment
4
+ class Client
5
+ include HTTParty
6
+ base_uri 'https://api.apphera.com/1'
7
+ format :json
8
+
9
+ attr_reader :api_key
10
+
11
+ # Get a free api_key @ https://developer.apphera.com
12
+ def initialize(api_key=nil)
13
+ @api_key = api_key
14
+ @api_key ||= Sentiment.api_key
15
+ @api_path = ''
16
+ end
17
+
18
+ def sentiment(body, lang)
19
+ options = {:body => {:body => body, :lang => lang}, :query => self.default_options}
20
+ results = Mash.new(self.class.post('/sentiments', options))
21
+
22
+ sentiment = results.response.polarity
23
+ end
24
+
25
+
26
+ protected
27
+
28
+ def default_options
29
+ {:api_key => @api_key}
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Sentiment
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sentiment/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Nikolai Manek"]
6
+ gem.email = ["niko.manek@gmail.com"]
7
+ gem.description = %q{A very simple to use sentiment analysis tool}
8
+ gem.summary = %q{Sentiment analysis}
9
+ gem.homepage = "https://github.com/nikoma/sentiment"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "sentiment"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Sentiment::VERSION
17
+ gem.add_runtime_dependency "httparty"
18
+ gem.add_runtime_dependency "mash"
19
+ gem.add_development_dependency "test/unit"
20
+ gem.add_development_dependency "vcr"
21
+ gem.add_development_dependency "webmock"
22
+ end
@@ -0,0 +1,47 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'vcr'
6
+ require 'webmock'
7
+ require 'sentiment'
8
+
9
+ VCR.configure do |c|
10
+ c.cassette_library_dir = 'fixtures/vcr_cassettes'
11
+ c.hook_into :webmock
12
+
13
+ end
14
+
15
+ class VCRTest < Test::Unit::TestCase
16
+ $client = Sentiment::Client.new("8w7d64oqweryiqweo87qnxtqwi47xqn3i4tisq7")
17
+
18
+ def test_sentiment_positive
19
+ VCR.use_cassette('sentiment_positive') do
20
+ sample = "I love ruby"
21
+ sentiment = $client.sentiment(sample, "en")
22
+ assert_equal 1, sentiment
23
+ end
24
+ end
25
+ def test_sentiment_negative
26
+ VCR.use_cassette('sentiment_negative') do
27
+ sample = "I hate some random thing"
28
+ sentiment = $client.sentiment(sample, "en")
29
+ assert_equal -1, sentiment
30
+ end
31
+ end
32
+ def test_sentiment_neutral
33
+ VCR.use_cassette('sentiment_neutral') do
34
+ sample = "Today it's 20 degrees outside and that's it"
35
+ sentiment = $client.sentiment(sample, "en")
36
+ assert_equal 0, sentiment
37
+ end
38
+ end
39
+ def test_sentiment_positive_de_umlauts
40
+ VCR.use_cassette('sentiment_positive_de_umlauts') do
41
+ sample = "Wir freuen uns über Umlaute die Sätze ändern"
42
+ sentiment = $client.sentiment(sample, "de")
43
+ assert_equal 1, sentiment
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,44 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://api.apphera.com/1/sentiments?api_key=8w7d64oqweryiqweo87qnxtqwi47xqn3i4tisq7
6
+ body:
7
+ encoding: UTF-8
8
+ string: body=I%20hate%20some%20random%20thing&lang=en
9
+ headers: {}
10
+ response:
11
+ status:
12
+ code: 200
13
+ message: OK
14
+ headers:
15
+ Server:
16
+ - nginx/1.2.1
17
+ Date:
18
+ - Thu, 02 Aug 2012 16:38:53 GMT
19
+ Content-Type:
20
+ - application/json; charset=utf-8
21
+ Content-Length:
22
+ - '48'
23
+ Connection:
24
+ - keep-alive
25
+ Status:
26
+ - 200 OK
27
+ X-Ua-Compatible:
28
+ - IE=Edge,chrome=1
29
+ Etag:
30
+ - ! '"45fc0357c8bd5b70db4f8bce584b7df5"'
31
+ Cache-Control:
32
+ - max-age=0, private, must-revalidate
33
+ X-Request-Id:
34
+ - d4c7705b2052a740f711365f74dc22f6
35
+ X-Runtime:
36
+ - '0.005970'
37
+ X-Rack-Cache:
38
+ - invalidate, pass
39
+ body:
40
+ encoding: UTF-8
41
+ string: ! '{"response":{"polarity":-1,"sentiment":-0.0625}}'
42
+ http_version:
43
+ recorded_at: Thu, 02 Aug 2012 16:38:53 GMT
44
+ recorded_with: VCR 2.2.4
@@ -0,0 +1,44 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://api.apphera.com/1/sentiments?api_key=8w7d64oqweryiqweo87qnxtqwi47xqn3i4tisq7
6
+ body:
7
+ encoding: UTF-8
8
+ string: body=Today%20it's%2020%20degrees%20outside%20and%20that's%20it&lang=en
9
+ headers: {}
10
+ response:
11
+ status:
12
+ code: 200
13
+ message: OK
14
+ headers:
15
+ Server:
16
+ - nginx/1.2.1
17
+ Date:
18
+ - Thu, 02 Aug 2012 16:38:54 GMT
19
+ Content-Type:
20
+ - application/json; charset=utf-8
21
+ Content-Length:
22
+ - '43'
23
+ Connection:
24
+ - keep-alive
25
+ Status:
26
+ - 200 OK
27
+ X-Ua-Compatible:
28
+ - IE=Edge,chrome=1
29
+ Etag:
30
+ - ! '"3c1d01e46834ab4bec5b8aec9135ef7c"'
31
+ Cache-Control:
32
+ - max-age=0, private, must-revalidate
33
+ X-Request-Id:
34
+ - 4fc8089f40269153def66becb77b2b82
35
+ X-Runtime:
36
+ - '0.005184'
37
+ X-Rack-Cache:
38
+ - invalidate, pass
39
+ body:
40
+ encoding: UTF-8
41
+ string: ! '{"response":{"polarity":0,"sentiment":0.0}}'
42
+ http_version:
43
+ recorded_at: Thu, 02 Aug 2012 16:38:54 GMT
44
+ recorded_with: VCR 2.2.4
@@ -0,0 +1,44 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://api.apphera.com/1/sentiments?api_key=8w7d64oqweryiqweo87qnxtqwi47xqn3i4tisq7
6
+ body:
7
+ encoding: UTF-8
8
+ string: body=I%20love%20ruby&lang=en
9
+ headers: {}
10
+ response:
11
+ status:
12
+ code: 200
13
+ message: OK
14
+ headers:
15
+ Server:
16
+ - nginx/1.2.1
17
+ Date:
18
+ - Thu, 02 Aug 2012 16:36:49 GMT
19
+ Content-Type:
20
+ - application/json; charset=utf-8
21
+ Content-Length:
22
+ - '45'
23
+ Connection:
24
+ - keep-alive
25
+ Status:
26
+ - 200 OK
27
+ X-Ua-Compatible:
28
+ - IE=Edge,chrome=1
29
+ Etag:
30
+ - ! '"12c84abae49f81af5a943c98c44b2dc0"'
31
+ Cache-Control:
32
+ - max-age=0, private, must-revalidate
33
+ X-Request-Id:
34
+ - da82d22d527b08d4527998d5dae2e1e5
35
+ X-Runtime:
36
+ - '0.005299'
37
+ X-Rack-Cache:
38
+ - invalidate, pass
39
+ body:
40
+ encoding: UTF-8
41
+ string: ! '{"response":{"polarity":1,"sentiment":0.325}}'
42
+ http_version:
43
+ recorded_at: Thu, 02 Aug 2012 16:36:49 GMT
44
+ recorded_with: VCR 2.2.4
@@ -0,0 +1,44 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://api.apphera.com/1/sentiments?api_key=8w7d64oqweryiqweo87qnxtqwi47xqn3i4tisq7
6
+ body:
7
+ encoding: UTF-8
8
+ string: body=Wir%20freuen%20und%20%C3%BCber%20Umlaute%20die%20S%C3%A4tze%20%C3%A4ndern&lang=de
9
+ headers: {}
10
+ response:
11
+ status:
12
+ code: 200
13
+ message: OK
14
+ headers:
15
+ Server:
16
+ - nginx/1.2.1
17
+ Date:
18
+ - Thu, 02 Aug 2012 16:41:21 GMT
19
+ Content-Type:
20
+ - application/json; charset=utf-8
21
+ Content-Length:
22
+ - '46'
23
+ Connection:
24
+ - keep-alive
25
+ Status:
26
+ - 200 OK
27
+ X-Ua-Compatible:
28
+ - IE=Edge,chrome=1
29
+ Etag:
30
+ - ! '"16a9bceff8483260f21ce3bf113968bb"'
31
+ Cache-Control:
32
+ - max-age=0, private, must-revalidate
33
+ X-Request-Id:
34
+ - d178c5dcd8af6c2d624607c5b30e2942
35
+ X-Runtime:
36
+ - '0.005309'
37
+ X-Rack-Cache:
38
+ - invalidate, pass
39
+ body:
40
+ encoding: UTF-8
41
+ string: ! '{"response":{"polarity":1,"sentiment":0.2198}}'
42
+ http_version:
43
+ recorded_at: Thu, 02 Aug 2012 16:41:21 GMT
44
+ recorded_with: VCR 2.2.4
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sentiment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nikolai Manek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mash
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: test/unit
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: vcr
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: webmock
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: A very simple to use sentiment analysis tool
95
+ email:
96
+ - niko.manek@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE
104
+ - README.md
105
+ - Rakefile
106
+ - lib/sentiment.rb
107
+ - lib/sentiment/client.rb
108
+ - lib/sentiment/version.rb
109
+ - sentiment.gemspec
110
+ - test/client_test.rb
111
+ - test/fixtures/vcr_cassettes/sentiment_negative.yml
112
+ - test/fixtures/vcr_cassettes/sentiment_neutral.yml
113
+ - test/fixtures/vcr_cassettes/sentiment_positive.yml
114
+ - test/fixtures/vcr_cassettes/sentiment_positive_de_umlauts.yml
115
+ homepage: https://github.com/nikoma/sentiment
116
+ licenses: []
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 1.8.24
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: Sentiment analysis
139
+ test_files:
140
+ - test/client_test.rb
141
+ - test/fixtures/vcr_cassettes/sentiment_negative.yml
142
+ - test/fixtures/vcr_cassettes/sentiment_neutral.yml
143
+ - test/fixtures/vcr_cassettes/sentiment_positive.yml
144
+ - test/fixtures/vcr_cassettes/sentiment_positive_de_umlauts.yml