forked from abhishek-ch/MachineLearning-using-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExploredSentimentAnalysis.R
More file actions
217 lines (169 loc) · 7.75 KB
/
ExploredSentimentAnalysis.R
File metadata and controls
217 lines (169 loc) · 7.75 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#https://sites.google.com/site/miningtwitter/questions/sentiment
#https://sites.google.com/site/miningtwitter/questions/sentiment/analysis
library(twitteR)
library(plyr)
library(stringr)
library(ggplot2)
#The sequence of column in CSV does matter the smoot_spline method to work properly
if(Sys.info()["user"] == 'achoudhary'){
path = "D:/Work/RWorkSpace/Git/"
}else{
path = "/Users/abhishekchoudhary/MachineLearning/"
}
# function score.sentiment
score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
# Parameters
# sentences: vector of text to score
# pos.words: vector of words of postive sentiment
# neg.words: vector of words of negative sentiment
# .progress: passed to laply() to control of progress bar
# create simple array of scores with laply
scores = laply(sentences,
function(sentence, pos.words, neg.words)
{
# remove punctuation
sentence = gsub("[[:punct:]]", "", sentence)
# remove control characters
sentence = gsub("[[:cntrl:]]", "", sentence)
# remove digits?
sentence = gsub('\\d+', '', sentence)
# remove html links
sentence = gsub("http\\w+", "", sentence)
# remove unnecessary spaces
sentence = gsub("[ \t]{2,}", "", sentence)
sentence = gsub("^\\s+|\\s+$", "", sentence)
# define error handling function when trying tolower
tryTolower = 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)
}
# use tryTolower with sapply
sentence = sapply(sentence, tryTolower)
# split sentence into words with str_split (stringr package)
word.list = str_split(sentence, "\\s+")
words = unlist(word.list)
# compare words to the dictionaries of positive & negative terms
pos.matches = match(words, pos.words)
neg.matches = match(words, neg.words)
# get the position of the matched term or NA
# we just want a TRUE/FALSE
pos.matches = !is.na(pos.matches)
neg.matches = !is.na(neg.matches)
# final score
score = sum(pos.matches) - sum(neg.matches)
return(score)
}, pos.words, neg.words, .progress=.progress )
# data frame with scores for each sentence
scores.df = data.frame(text=sentences, score=scores)
return(scores.df)
}
# import positive and negative words
posText = paste(path,"positive_words.txt", sep = "")
negText = paste(path,"negative_words.txt", sep = "")
# import positive and negative words
pos = readLines(posText)
neg = readLines(negText)
#Twitter API details
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
apiKey <- "MIgAEnO0XHTPKdMv3qiGKr6nu"
apiSecret <- "CMYO2quM7fUzcVuvx8JjALiKjC9cnpXeJFqQLtv2pnECJCCZKz"
access_token <- "69009666-XkI1bcxXtE4qXfOtbRYCgkiJJvpCfsmS0fq4OSq9d"
access_token_secret <- "w89WtxJDAwakPToMqoFtpQYJIfht6YS3a8136hpcyW7eG"
#setup_twitter_oauth(apiKey,apiSecret,access_token,access_token_secret)
setup_twitter_oauth("MIgAEnO0XHTPKdMv3qiGKr6nu","CMYO2quM7fUzcVuvx8JjALiKjC9cnpXeJFqQLtv2pnECJCCZKz")
#tweets for phone
iphone = searchTwitter("oneplus2", n=2000,lang="en")
nexus6 = searchTwitter("iphone6s", n=2000,lang="en")
samsung = searchTwitter("S6", n=2000,lang="en")
#total tweets of each item
totaltweets = c(length(iphone),length(nexus6),length(samsung))
#join all tweets
allcontents = c(iphone,nexus6,samsung)
allcontents = sapply(allcontents, function(x) x$getText())
#to avoid the error http://stackoverflow.com/questions/9637278/r-tm-package-invalid-input-in-utf8towcs
allcontents=str_replace_all(allcontents,"[^[:graph:]]", " ")
all_corpus = Corpus(VectorSource(allcontents))
# apply function score.sentiment
scores = score.sentiment(allcontents, pos, neg, .progress='text')
# add variables to data frame
scores$phones = factor(rep(c("iPhone", "Nexus6", "Samsung"), totaltweets))
scores$very.pos = as.numeric(scores$score >= 2)
scores$very.neg = as.numeric(scores$score <= -2)
# how many very positives and very negatives
numpos = sum(scores$very.pos)
numneg = sum(scores$very.neg)
# global score
global_score = round( 100 * numpos / (numpos + numneg) )
#http://stackoverflow.com/questions/15651084/ggplot2-mapping-variable-to-y-and-using-stat-bin?lq=1
# barplot of average score
meanscore = tapply(scores$score, scores$phones, mean)
df = data.frame(phones=names(meanscore), meanscore=meanscore)
df$drinks <- reorder(df$phones, df$meanscore)
g1<- ggplot(df, aes(y=meanscore)) +
geom_bar(data=df, aes(x=phones, fill=phones),stat = "identity") +
scale_fill_manual(values=cols[order(df$meanscore)]) +
ggtitle("Average Sentiment Score")
# barplot of average very positive
phone_pos = ddply(scores, .(phones), summarise, mean_pos=mean(very.pos))
phone_pos$phones <- reorder(phone_pos$phones, phone_pos$mean_pos)
g2<- ggplot(phone_pos, aes(y=mean_pos)) +
geom_bar(data=phone_pos, aes(x=phones, fill=phones),stat = "identity") +
scale_fill_manual(values=cols[order(phone_pos$mean_pos)]) +
ggtitle("Average Very Positive Sentiment Score")
# barplot of average very negative
phone_neg = ddply(scores, .(phones), summarise, mean_neg=mean(very.neg))
phone_neg$phones <- reorder(phone_neg$phones, phone_neg$mean_neg)
g3<- ggplot(phone_neg, aes(y=mean_neg)) +
geom_bar(data=phone_neg, aes(x=phones, fill=phones),stat = "identity") +
scale_fill_manual(values=cols[order(phone_neg$mean_neg)]) +
ggtitle("Average Very Negative Sentiment Score")
multiplot(g1, g2, g3, cols=2)
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}