Created
November 25, 2010 15:32
-
-
Save tomz/715535 to your computer and use it in GitHub Desktop.
Example usage of TweetSentiments.com API
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
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