Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add getSentiment.rb which uses the AlchemyAPI to retrieve sentiment
- Loading branch information
Showing
1 changed file
with
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\""] | ||
|
||
|
||
|
||
|