Skip to content

Commit

Permalink
Added support for sentiment analysis endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
zenkay committed Jul 6, 2015
1 parent 211add5 commit 0118287
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/dandelionapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "dandelionapi/entity_extraction"
require "dandelionapi/text_similarity"
require "dandelionapi/language_detection"
require "dandelionapi/sentiment_analysis"

module Dandelionapi

Expand Down
25 changes: 25 additions & 0 deletions lib/dandelionapi/sentiment_analysis.rb
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
46 changes: 46 additions & 0 deletions spec/cassettes/sentiment_analysis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions spec/sentiment_analysis_spec.rb
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

0 comments on commit 0118287

Please sign in to comment.