Skip to content

Commit

Permalink
add getSentiment.rb which uses the AlchemyAPI to retrieve sentiment
Browse files Browse the repository at this point in the history
  • Loading branch information
rtanglao committed Jul 3, 2012
1 parent 86c836e commit 43fab10
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions getSentiment.rb
@@ -0,0 +1,55 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'time'
require 'date'
require 'mongo'
require '../AlchemyAPI.rb'
require 'pp'

MONGO_HOST = ENV["MONGO_HOST"]
raise(StandardError,"Set Mongo hostname in ENV: 'MONGO_HOST'") if !MONGO_HOST
MONGO_PORT = ENV["MONGO_PORT"]
raise(StandardError,"Set Mongo port in ENV: 'MONGO_PORT'") if !MONGO_PORT
MONGO_USER = ENV["MONGO_USER"]
raise(StandardError,"Set Mongo user in ENV: 'MONGO_USER'") if !MONGO_USER
MONGO_PASSWORD = ENV["MONGO_PASSWORD"]
raise(StandardError,"Set Mongo user in ENV: 'MONGO_PASSWORD'") if !MONGO_PASSWORD

db = Mongo::Connection.new(MONGO_HOST, MONGO_PORT.to_i).db("gs")
raise(StandardError, "Couldn't authenticate, exiting") if !db.authenticate(MONGO_USER, MONGO_PASSWORD)

topicsColl = db.collection("topics")

# Create an AlchemyAPI object.
alchemyObj = AlchemyAPI.new();

# Load the API key from disk.
alchemyObj.loadAPIKey("api_key.txt");

url = id = ARGV[0]
# Get sentiment for a web URL.
# result = alchemyObj.URLGetTextSentiment(url);
# puts result

# # Retrieve targeted sentiment
# result = alchemyObj.URLGetTargetedSentiment(url, "Walmart", AlchemyAPI::OutputMode::XML, tsparamObj);
# puts result

# Create a parameters object.
kparamObj = AlchemyAPI_KeywordParams.new();

# Enable keyword-targeted sentiment.
kparamObj.setSentiment(1);

# Retrieve keywords with keyword-targeted sentiment.
result = alchemyObj.URLGetRankedKeywords(url, AlchemyAPI::OutputMode::JSON, kparamObj);
pp result
printf("Just keywords\n");
pp result["status"]
print result
pp result["\r\n \"status\""]




0 comments on commit 43fab10

Please sign in to comment.