Skip to Content

How to do sentiment/polarity analysis of twitter data using tm.plugin.sentiment [closed]

Hi I have a working code(as shown below) using sentiment package,
but as this package is no more available only alternative is using tm.plugin.sentiment package from r-forge.My system is R,Rstudio,Windows 7.
Can anybody guide me how to do all these using tm.plugin.sentiment ?

library(twitteR)
library(sentiment)
library(tm)
library(Snowball)
library(wordcloud)
###### search in twitter #######
# send R to go collect twitter data on latest Stalone movie "Bullet to the Head"
rev = searchTwitter("#Bullet to the Head", n=500, lang="en",
                    since="2013-01-10", until="2013-03-01")
#shows first 5 tweets
rev[1:5]
# changes tweets to data frame 
rev.df = twListToDF(rev) 
#save data 
# write.table(as.matrix(rev.df[ ,1]), file.choose())
write.table(rev.df, file = "D:/R Projects/twitterdata.csv", sep = ",", col.names = NA,
            qmethod = "double")
# Standard text mining stuff
x=read.csv("D:/R Projects/twitterdata.csv",row.names = 1)
# x = readLines(file.choose())
x1 = Corpus(VectorSource(x))
# standardize the text - remove blanks, uppercase #
# punctuation, English connectors etc. #
x1 = tm_map(x1, stripWhitespace)
x1 = tm_map(x1, tolower)
x1 = tm_map(x1, removePunctuation)
x1 = tm_map(x1, removeNumbers)
# removing it from stopwords
myStopwords <- stopwords('english')
x1 = tm_map(x1, removeWords, myStopwords)
x1 = tm_map(x1, stemDocument)
# make the doc-term matrix #
myDtm = DocumentTermMatrix(x1,control = list(minWordLength = 1))
inspect(myDtm[266:270,21:30])
# Frequent Terms and Associations
findFreqTerms(myDtm, lowfreq=10,highfreq = Inf)
# which words are associated with "Stalone"?
findAssocs(myDtm, "Stalone", 0.05)
# Word Cloud
# After building a document-term matrix, we can show 
# the importance of words with a word cloud (also kown as a tag cloud) . 
m <- as.matrix(myDtm)
# calculate the frequency of words
v <- sort(rowSums(m), decreasing=TRUE)
myNames <- names(v)
d <- data.frame(word=myNames, freq=v)
wordcloud(d$word, d$freq, min.freq=3)
# Invoke sentiment analysis. Classify the tweets by emotion, 
# find the polarity (or which emotion pole - pos or neg - 
# dominates a text output) using simple functions.
## --- inspect only those tweets which 
## got a clear sentiment orientation ---
# a1=classify_emotion(x1)
# which tweets had clear polarity
a2=x[(!is.na(a1[,7]))] 
a2[1:10]
# what is the polarity score of each tweet? #
# that is, what's the ratio of pos to neg content? #
b1=classify_polarity(x1)
dim(b1)
# build polarities table
# view a few rows
b1[1:5,]
##--- changing the a1 thing to reg data frame 
a1a=data.matrix(as.numeric(a1))
a1b=matrix(a1a,nrow(a1),ncol(a1))
# build sentiment type-score matrix
a1b[1:4,] # view few rows
# recover and remove the mode values
mode1 <- function(x){names(sort(-table(x)))[1]}
for (i1 in 1:6){ # for the 6 primary emotion dimensions
  mode11=as.numeric(mode1(a1b[,i1]))
  a1b[,i1] = a1b[,i1]-mode11 }
summary(a1b)
a1c = a1b[,1:6]
colnames(a1c) <- c("Anger", "Disgust", "fear", "joy", "sadness", "surprise")
a1c[1:10,] # view a few rows
## -- see top 10 tweets in "Joy" (for example) 
a1c=as.data.frame(a1c);attach(a1c)
test = x[(joy != 0)]; test[1:10]
# for the top few tweets in "Anger" 
test = x[(Anger != 0)]; test[1:10]
test = x[(sadness != 0)]; test[1:10]