forked from zenkay/dandelionapi-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for sentiment analysis endpoint
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# encoding: UTF-8 | ||
|
||
require "faraday" | ||
require "faraday_middleware" | ||
require "json" | ||
|
||
module Dandelionapi | ||
|
||
class SentimentAnalysis < Base | ||
|
||
ENDPOINT = "/datatxt/sent/v1" | ||
|
||
attr_accessor :text, :url, :html, :html_fragment, :lang | ||
|
||
def analyze(options) | ||
|
||
raise MissingParameters.new("Please provide one of the following parameters: text, url, html or html_fragment") if ([:text, :url, :html, :html_fragment] & options.keys).empty? | ||
|
||
params = options | ||
call(ENDPOINT, params) | ||
end | ||
|
||
end | ||
|
||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# encoding: UTF-8 | ||
|
||
require 'spec_helper' | ||
|
||
vcr_options = { :cassette_name => "sentiment_analysis", :record => :new_episodes } | ||
|
||
describe Dandelionapi::SentimentAnalysis, vcr: vcr_options do | ||
|
||
before(:each) do | ||
Dandelionapi.configure do |c| | ||
# account: Mario Rossi <datatxt@mailinator.com> | ||
c.app_id = "e0bca290" | ||
c.app_key = "2294c676b8698383764514cc219fad92" | ||
c.endpoint = "https://api.dandelion.eu/" | ||
end | ||
end | ||
|
||
it "initialize a new nex request" do | ||
request = Dandelionapi::SentimentAnalysis.new | ||
expect(request).to be_an_instance_of(Dandelionapi::SentimentAnalysis) | ||
end | ||
|
||
it "make a request to sent using italian plain text" do | ||
element = Dandelionapi::SentimentAnalysis.new | ||
response = element.analyze( | ||
text: "Mio padre che mi spinge a mangiare e guai se non finisco mio padre che vuol farmi guidare mi frena con il fischio" | ||
) | ||
puts response.inspect | ||
expect(response).not_to be_empty | ||
expect(response["sentiment"]).not_to be_empty | ||
expect(response["sentiment"]["score"]).to be >= -1.0 | ||
expect(response["sentiment"]["score"]).to be <= 1.0 | ||
expect(["positive", "neutral", "negative"]).to include(response["sentiment"]["type"]) | ||
end | ||
|
||
it "raise BadResponse exception on wrong config parameters" do | ||
Dandelionapi.configure do |c| | ||
c.app_id = "bad-app-id" | ||
c.app_key = "bad-app-key" | ||
c.endpoint = "not-an-url-endpoint" | ||
end | ||
element = Dandelionapi::SentimentAnalysis.new | ||
expect { element.analyze(text: "test") }.to raise_error(Dandelionapi::BadResponse) | ||
end | ||
|
||
it "raise MissingParameters exception when required params are missing" do | ||
element = Dandelionapi::SentimentAnalysis.new | ||
expect { element.analyze(other: "test") }.to raise_error(Dandelionapi::MissingParameters) | ||
end | ||
end |