Skip to content

Instantly share code, notes, and snippets.

@tomz
Created November 25, 2010 15:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomz/715535 to your computer and use it in GitHub Desktop.
Save tomz/715535 to your computer and use it in GitHub Desktop.
Example usage of TweetSentiments.com API
require 'rubygems'
require 'json'
require 'net/http'
# Sentiments on tweets
# http://data.tweetsentiments.com:8080/api/search.json?topic=<topic to analyze>
url = "http://data.tweetsentiments.com:8080/api/analyze.json?q=i%20am%20happy"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
result = JSON.parse(data)
result["sentiment"]["name"]
result["sentiment"]["value"]
# Sentiments on topics
# http://data.tweetsentiments.com:8080/api/search.json?topic=<topic to analyze>
url = "http://data.tweetsentiments.com:8080/api/search.json?topic=verizon"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
result = JSON.parse(data)
result["sentiment_index"] # sentiment index for the topic
result["positive"] # number of positive tweets
result["negative"] # number of negative tweets
result["neutral"] # number of neutral tweets
result["results"] # tweets with sentiments
result["results"][0]["text"] # tweet text of the first analyzed tweet
result["results"][0]["sentiment"] # tweet sentiment of the first analyzed tweet
url = "http://data.tweetsentiments.com:8080/api/search.json?topic=comcast"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
result = JSON.parse(data)
result["sentiment_index"]
result["positive"]
result["negative"]
result["neutral"]
# Sentiments on users
# http://data.tweetsentiments.com:8080/api/search.json?user=<user to analyze>
url = "http://data.tweetsentiments.com:8080/api/search.json?user=dhh"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
result = JSON.parse(data)
result["sentiment_index"]
result["positive"]
result["negative"]
result["neutral"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment