Skip to content

Instantly share code, notes, and snippets.

@tts
Last active December 19, 2015 15:08
Show Gist options
  • Save tts/5973930 to your computer and use it in GitHub Desktop.
Save tts/5973930 to your computer and use it in GitHub Desktop.
Yet another light-weight sentiment analysis of the last updates of my English-writing Twitter followers
########################################################################################################
#
# Making - yet another - light-weight sentiment analysis of the last updates
# of my English-writing Twitter followers.
#
# Algorithm by
# http://jeffreybreen.wordpress.com/2011/07/04/twitter-text-mining-r-slides/
# https://github.com/jeffreybreen/twitter-sentiment-analysis-tutorial-201107/blob/master/R/sentiment.R
#
# Word list from
# http://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html
# (Linux: unrar e opinion-lexicon-English.rar)
#
# Help with rate limits and lookupUsers by Mikael Rekola
#
# Tuija Sonkkila
# 11.7.2013
#
########################################################################################################
library(twitteR)
library(ROAuth)
library(cldr)
source("score.sentiment.R")
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
cKey <- "your consumer key"
cSecret <- "your consumer secret"
credentials <- OAuthFactory$new(consumerKey=cKey,
consumerSecret=cSecret,
requestURL=reqURL,
accessURL=accessURL,
authURL=authURL)
credentials$handshake()
registerTwitterOAuth(credentials)
save(credentials, file="twittercred.Rdata")
me <- getUser('ttso')
# My followers
followers <- me$getFollowers()
# Transform to a data frame
fDf <- twListToDF(followers)
# My friends
# friends <- me$getFriends()
# Fetch info about my followers
users <- lookupUsers(as.vector(fDf$screenName))
# All last statuses
s <- lapply(users, function(x) x$getLastStatus())
# Transform to a data frame
uDfS <- twListToDF(s)
# Word lists
pos <- scan("negative-words.txt", what="character", comment.char=";")
neg <- scan("positive-words.txt", what="character", comment.char=";")
# Add some
neg <- c(neg, 'wtf', 'epicfail')
pos <- c(pos, 'ftw')
# Which tweets are in English?
# http://stackoverflow.com/questions/8078604/detect-text-language-in-r
r <- detectLanguage(uDfS$text)
# Merge all info
all <- cbind(uDfS, r)
# Analyze
result <- score.sentiment(all[all$detectedLanguage == 'ENGLISH', ]$text, pos, neg)
# Group count by score
sc <- aggregate(result$score, by=list(result$score), FUN=length)
# - and this is how it looks like
# https://www.dropbox.com/s/pqybjxuve127tt8/twmood2score.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment