library(twitteR)
library(sentiment)
library(plyr)
library(ggplot2)
library(wordcloud)
library(RColorBrewer)
some_tweets = searchTwitter(“patanjali”, n=1500, lang=“en”)
some_txt = sapply(some_tweets, function(x) x$getText())
some_txt = gsub(“(RT|via)((?:\b\W*@\w+)+)“,”“, some_txt)
some_txt = gsub(“@\w+”, “”, some_txt)
some_txt = gsub(“[[:punct:]]”, “”, some_txt)
some_txt = gsub(“[[:digit:]]”, “”, some_txt)
some_txt = gsub(“http\w+”, “”, some_txt)
some_txt = gsub(“[ ]{2,}”, “”, some_txt) some_txt = gsub(“^\s+|\s+$”, “”, some_txt)
try.error = function(x)
{
# create missing value
y = NA
# tryCatch error
try_error = tryCatch(tolower(x), error=function(e) e)
# if not an error
if (!inherits(try_error, “error”))
y = tolower(x)
# result
return(y)
}
some_txt = sapply(some_txt, try.error)
some_txt = some_txt[!is.na(some_txt)] names(some_txt) = NULL
class_emo = classify_emotion(some_txt, algorithm=“bayes”, prior=1.0)
emotion = class_emo[,7]
emotion[is.na(emotion)] = “unknown”
class_pol = classify_polarity(some_txt, algorithm=“bayes”)
polarity = class_pol[,4]
sent_df = data.frame(text=some_txt, emotion=emotion,
polarity=polarity, stringsAsFactors=FALSE)
sent_df = within(sent_df, emotion <- factor(emotion, levels=names(sort(table(emotion), decreasing=TRUE))))
counts <- table(sent_df$emotion) barplot(counts, main=“Sentiment Analysis of Tweets about Starbucksby emotion”, xlab=“emotion”,col=c(“pink”,“yellow”,“orange”,“red”,“black”,“purple”,“green”),legend = rownames(counts))
counts <- table(sent_df$polarity) barplot(counts, main=“Sentiment Analysis of Tweets about Starbucksby polarity”, xlab=“emotion”,col=c(“pink”,“yellow”,“purple”),legend = rownames(counts))
separating text by emotion
emos = levels(factor(sent_df$emotion)) nemo = length(emos) emo.docs = rep(“”, nemo) for (i in 1:nemo)
{
tmp = some_txt[emotion == emos[i]]
emo.docs[i] = paste(tmp, collapse=" “)
}
emo.docs = removeWords(emo.docs, stopwords(“english”))
corpus = Corpus(VectorSource(emo.docs)) tdm = TermDocumentMatrix(corpus) tdm = as.matrix(tdm) colnames(tdm) = emos
comparison.cloud(tdm, colors = brewer.pal(nemo, “Dark2”),scale = c(3,.5), random.order = FALSE, title.size = 1.5)