-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWebSummitSentimentAnalysis.R
More file actions
187 lines (141 loc) · 5.46 KB
/
WebSummitSentimentAnalysis.R
File metadata and controls
187 lines (141 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#https://sites.google.com/site/miningtwitter/questions/sentiment/sentiment
#http://stackoverflow.com/questions/15194436/is-there-any-other-package-other-than-sentiment-to-do-sentiment-analysis-in-r
options(download.file.method = "wininet")
install.packages("/Volumes/work/data/others/Rstem_0.4-1.zip", repos = NULL, type="source")
install.packages("Rstem", repos = "http://www.omegahat.org/R", type="source")
download.file("http://cran.r-project.org/src/contrib/Archive/sentiment/sentiment_0.2.tar.gz", "sentiment.tar.gz")
install.packages("sentiment.tar.gz", repos=NULL, type="source")
install.packages("devtools")
library(devtools)
library(sentiment)
install.packages(c("rjson", "bit64", "httr"))
library(httr)
install_github("geoffjentry/twitteR", username="geoffjentry")
library(twitteR)
library(plyr)
library(ggplot2)
library(wordcloud)
library(RColorBrewer)
library(stringr)
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
apiKey <- ""
apiSecret <- ""
access_token <- ""
access_token_secret <- ""
#setup_twitter_oauth(apiKey,apiSecret,access_token,access_token_secret)
setup_twitter_oauth("MIgAEnO0XHTPKdMv3qiGKr6nu","CMYO2quM7fUzcVuvx8JjALiKjC9cnpXeJFqQLtv2pnECJCCZKz")
#fetch tweets with word
trendword = searchTwitter("#flyhackfly", n=2000)
# get the text
trendword = sapply(trendword, function(x) x$getText())
#Avoid the non utf-8 characters
trendword=str_replace_all(trendword,"[^[:graph:]]", " ")
#Copy paste of direct cleaning of String
#based on general
# remove retweet entities
trendword = gsub("(RT|via)((?:\\b\\W*@\\w+)+)", "", some_txt)
# remove at people
trendword = gsub("@\\w+", "", some_txt)
# remove punctuation
trendword = gsub("[[:punct:]]", "", trendword)
# remove numbers
trendword = gsub("[[:digit:]]", "", trendword)
# remove html links
trendword = gsub("http\\w+", "", trendword)
# remove unnecessary spaces
trendword = gsub("[ \t]{2,}", "", trendword)
trendword = gsub("^\\s+|\\s+$", "", trendword)
# define "tolower error handling" function
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)
}
# lower case using try.error with sapply
trendword = sapply(trendword, try.error)
# remove NAs in some_txt
trendword = trendword[!is.na(trendword)]
names(trendword) = NULL
# classify emotion
class_emo = classify_emotion(trendword, algorithm="bayes", prior=1.0)
# get emotion best fit
emotion = class_emo[,7]
# substitute NA's by "unknown"
emotion[is.na(emotion)] = "unknown"
# classify polarity
class_pol = classify_polarity(trendword, algorithm="bayes")
# get polarity best fit
polarity = class_pol[,4]
# data frame with results
sent_df = data.frame(text=trendword, emotion=emotion,
polarity=polarity, stringsAsFactors=FALSE)
# sort data frame
sent_df = within(sent_df,
emotion <- factor(emotion, levels=names(sort(table(emotion), decreasing=TRUE))))
# plot distribution of emotions
ggplot(sent_df, aes(x=emotion)) +
geom_bar(aes(y=..count.., fill=emotion)) +
scale_fill_brewer(palette="Dark2") +
labs(x="Twitter Sentiments", y="number of tweets (4000) Latest") +
ggtitle("Tweets on #flyhackfly \n(classification by emotion)"
)
# plot distribution of polarity
ggplot(sent_df, aes(x=polarity)) +
geom_bar(aes(y=..count.., fill=polarity)) +
scale_fill_brewer(palette="RdGy") +
labs(x="polarity categories", y="number of tweets(4000)") +
ggtitle("Sentiment Analysis of Tweets about #flyhackfly")
# separating text by emotion
emos = levels(factor(sent_df$emotion))
nemo = length(emos)
emo.docs = rep("", nemo)
for (i in 1:nemo)
{
tmp = trendword[emotion == emos[i]]
emo.docs[i] = paste(tmp, collapse=" ")
}
common <- read.csv("/Volumes/work/project/github/machine-learning-R/common.csv",header=FALSE)
# remove generic and custom stopwords
my_stopwords <- c(stopwords('english'), common)
# remove stopwords
emo.docs = removeWords(emo.docs, my_stopwords)
# create corpus
corpus = Corpus(VectorSource(emo.docs))
tdm = TermDocumentMatrix(corpus)
tdm = as.matrix(tdm)
colnames(tdm) = emos
# comparison word cloud
comparison.cloud(tdm, colors = brewer.pal(nemo, "Dark2"),
scale = c(3,.5), random.order = FALSE, title.size = 1.5)
dev.off()
#######################################retweets###########################################
#http://stackoverflow.com/questions/13649019/with-r-split-time-series-data-into-time-intervals-say-an-hour-and-then-plot-t
#http://stackoverflow.com/questions/10317470/simple-analog-for-plotting-a-line-from-a-table-object-in-ggplot2
#http://blog.ouseful.info/2012/02/17/visualising-twitter-user-timeline-activity-in-r/
#find number of retweets
library(ggplot2)
rdmTweets <- searchTwitter('#trendword', n=500)
#Create a dataframe based around the results
df <- do.call("rbind", lapply(trendword, as.data.frame))
#Here are the columns
names(df)
#And some example content
head(df,3)
counts=table(df$retweetCount)
barplot(counts)
dev.off()
#find retweets maximum than 30
retweetSubset =subset(df,retweetCount > 50)
qplot(screenName, data=retweetSubset, geom="bar",weight=retweetCount,fill=screenName)
#We can also generate barplots showing the distribution of tweet count over time:
MyDatesTable <- table(cut(df$created, breaks="10 mins"))
ggplot(df,aes(x=cut(df$created, breaks="4 mins")))+geom_bar(aes(y = (..count..)))