-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwittr_amzon_sentiments.R
305 lines (246 loc) · 8.97 KB
/
twittr_amzon_sentiments.R
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
## Twitter analysis for NPL
library("twitteR")
library("ROAuth")
library(base64enc)
library(httpuv)
library(tm)
library(slam)
install.packages("topicmodels")
library(topicmodels)
setup_twitter_oauth("eOtcKxxxxxxxxxxxxjjjjjssss",
"TedjjjjjjdhhhhhhiiikkkkH2qppgD7wxxxxxxx",
"896758586243653632-6RCrlxxxxxxxxxxxx", # Access token
"QYLajXrqdbMYFKwno9itPNoxxxxxxxxxxxxxxxxxxxxx") # Access token secret key
Tweets <- userTimeline('BarackObama', n = 1000) ## twitter account of barack obama
TweetsDF <- twListToDF(Tweets)
View(TweetsDF)
write.csv(TweetsDF, "Tweets_sarf.csv")
getwd() ## to look where the file save
obama_tweets <- obama_tweets[,2]
head(obama_tweets)
str(obama_tweets)
mydata.corpus <- Corpus(VectorSource(obama_tweets)) ##making or corpus
inspect(mydata.corpus)
corpus_clean <- tm_map(mydata.corpus, tolower) ## all words should be lowercase
inspect(corpus_clean)
corpus_clean <- tm_map(corpus_clean,removeNumbers) ## removing numbers
stopwords("stop")
corpus_clean <- tm_map(corpus_clean,removeWords,stopwords()) ## restricting words by stop txt
corpus_clean <- tm_map(corpus_clean,removePunctuation) ## remove punctuation
corpus_clean <- tm_map(corpus_clean,stripWhitespace)
str(corpus_clean)
mydata.tdm <- DocumentTermMatrix(corpus_clean)
dim(mydata.tdm) ## 484 2609
## getting top 10 terms
##dtm <- as.DocumentTermMatrix(mydata.)
rowTotals <- apply(mydata.tdm , 1, sum)
rowTotals
dtm.new <- mydata.tdm[rowTotals> 0, ]
library(NLP)
lda <- LDA(dtm.new, 10) # find 10 topics
term <- terms(lda, 10) # first 5 terms of every topic
term
####################### Emotion mining ##############################
install.packages("syuzhet")
library("syuzhet")
str(obama_tweets)
#my_example_text <- readLines(obama_tweets)
s_v <- get_sentences(obama_tweets)
class(s_v)
str(s_v)
(s_v)
sentiment_vector <- get_sentiment(s_v, method = "bing")
head(sentiment_vector)
sentiment_vector
afinn_s_v <- get_sentiment(s_v, method = "afinn")
head(afinn_s_v)
nrc_vector <- get_sentiment(s_v, method="nrc")
head(nrc_vector)
sentiment_vector
sum(sentiment_vector)
mean(sentiment_vector)
summary(sentiment_vector)
# plot
plot(sentiment_vector, type = "l", main = "Plot Trajectory",
xlab = "Narrative Time", ylab = "Emotional Valence")
abline(h = 0, col = "red")
# To extract the sentence with the most negative emotional valence
negative <- s_v[which.min(sentiment_vector)]
negative
# and to extract the most positive sentence
positive <- s_v[which.max(sentiment_vector)]
positive
# more depth
plot(
sentiment_vector,
type="h",
main="Example Plot Trajectory",
xlab = "Narrative Time",
ylab= "Emotional Valence"
)
# percentage based figures
percent_vals <- get_percentage_values(sentiment_vector)
plot(
percent_vals,
type="l",
main="Throw the ring in the volcano Using Percentage-Based Means",
xlab = "Narrative Time",
ylab= "Emotional Valence",
col="red"
)
ft_values <- get_transformed_values(
sentiment_vector,
low_pass_size = 3,
x_reverse_len = 100,
scale_vals = TRUE,
scale_range = FALSE
)
ft_values
plot(
ft_values,
type ="h",
main ="LOTR using Transformed Values",
xlab = "Narrative Time",
ylab = "Emotional Valence",
col = "red"
)
# categorize each sentence by eight emotions
nrc_data <- get_nrc_sentiment(s_v)
nrc_data
# subset
sad_items <- which(nrc_data$sadness > 0)
head(s_v[sad_items])
# To view the emotions as a barplot
barplot(sort(colSums(prop.table(nrc_data[, 1:8]))), horiz = T, cex.names = 0.7,
las = 1, main = "Emotions", xlab = "Percentage",
col = 1:8)
## Taking production from amazon site
library(rvest)
library(XML)
library(magrittr)
aurl <- "https://www.amazon.in/product-reviews/B086CGNG4T/ref=cm_cr_arp_d_show_all?ie=UTF8&reviewerType=all_reviews&pageNumber=1"
amazon_reviews <- NULL
for (i in 1:5){
murl <- read_html(as.character(paste(aurl,i,sep="="))) # Use html()
murl
rev <- murl %>%
html_nodes(".review-text") %>%
html_text()
amazon_reviews <- c(amazon_reviews,rev)
View (amazon_reviews)
}
write.table(amazon_reviews,"sonybravia108.txt",row.names = F)
raw_amazon <- readLines('path-to-file/textMining/amazon/sonybravia108.txt')
head(raw_amazon)
class(raw_amazon)
mydata1.corpus <- Corpus(VectorSource(raw_amazon)) ##making or corpus
inspect(mydata1.corpus)
corpus_clean1 <- tm_map(mydata1.corpus, tolower) ## all words should be lowercase
inspect(corpus_clean1)
corpus_clean1 <- tm_map(corpus_clean1,removeNumbers) ## removing numbers
stopwords('en')
?stopwords
corpus_clean1 <- tm_map(corpus_clean1,removeWords,stopwords()) ## restricting words by stop txt
corpus_clean1 <- tm_map(corpus_clean1,removePunctuation) ## remove punctuation
corpus_clean1 <- tm_map(corpus_clean1,stripWhitespace)
s_v1 <- get_sentences(raw_amazon)
class(s_v1)
str(s_v1)
(s_v1)
sentiment_vector1 <- get_sentiment(s_v1, method = "bing")
head(sentiment_vector1)
sentiment_vector1
sentiment_vector2 <- get_sentiment(s_v, method = "syuzhet")
head(sentiment_vector2)
sentiment_vector2
sum(sentiment_vector2)
mean(sentiment_vector2)
summary(sentiment_vector2)
# plot
plot(sentiment_vector2, type = "l", main = "Plot Trajectory",
xlab = "Narrative Time", ylab = "Emotional Valence",
abline(h = 0, col = "red"))
# To extract the sentence with the most negative emotional valence
negative1 <- s_v1[which.min(sentiment_vector2)]
negative1
# and to extract the most positive sentence
positive1 <- s_v1[which.max(sentiment_vector2)]
positive1
# percentage based figures
percent_vals <- get_percentage_values(sentiment_vector2)
plot(
percent_vals,
type="l",
main="Throw the ring in the volcano Using Percentage-Based Means",
xlab = "Narrative Time",
ylab= "Emotional Valence",
col="red"
)
# categorize each sentence by eight emotions
nrc_data1 <- get_nrc_sentiment(s_v1)
nrc_data1
# To view the emotions as a barplot
barplot(sort(colSums(prop.table(nrc_data1))), horiz = T, cex.names = 0.7,
las = 1, main = "Emotions", xlab = "Percentage",
col = 1:10)
## wordcloud on twitter and amazon
########################## twitter Word Cloud #################################3
twitter.tdm <- TermDocumentMatrix(corpus_clean,
control = list(minwordLength = c(1,Inf))
)
findFreqTerms(twitter.tdm,lowfreq = 10)
twitter.termFrequency <- rowSums(as.matrix(twitter.tdm))
twitter.termFrequency <- subset(twitter.termFrequency,twitter.termFrequency>=10)
library(ggplot2)
barplot(twitter.termFrequency,las = 2, col = rainbow(20))
library(wordcloud)
m <- as.matrix(twitter.tdm)
twitter.wordFrequency <- sort(rowSums(m),decreasing = T)
wordcloud(words = names(twitter.wordFrequency), freq = twitter.wordFrequency, min.freq = 10, random.order = F, colors = rainbow(20))
pos <- scan(file.choose(), what = "character", comment.char = ';')
neg <- scan(file.choose(), what = "character", comment.char = ';')
## positive words cloud
pos.twitter.match <- match(names(twitter.wordFrequency),pos)
pos.twitter.match <- !is.na(pos.twitter.match)
freq.pos.twitter <- twitter.wordFrequency[pos.twitter.match]
p.twiter.names <- names(freq.pos.twitter)
p.twiter.names
wordcloud(p.twiter.names, freq = twitter.wordFrequency, min.freq = 10, random.order = F,
colors = rainbow(20))
## negative words cloud
neg.twitter.match <- match(names(twitter.wordFrequency),neg)
neg.twitter.match <- !is.na(neg.twitter.match)
freq.neg.twitter <- twitter.wordFrequency[neg.twitter.match]
n.twiter.names <- names(freq.neg.twitter)
n.twiter.names
wordcloud(n.twiter.names, freq = twitter.wordFrequency, min.freq = 10, random.order = F,
colors = rainbow(20))
########################## Amazon Word Cloud #################################3
amazon.tdm <- TermDocumentMatrix(corpus_clean1,
control = list(minwordLength = c(1,Inf))
)
findFreqTerms(amazon.tdm,lowfreq = 10)
amazon.termFrequency <- rowSums(as.matrix(amazon.tdm))
amazon.termFrequency <- subset(amazon.termFrequency,amazon.termFrequency>=10)
library(ggplot2)
barplot(amazon.termFrequency,las = 2, col = rainbow(20))
library(wordcloud)
m1 <- as.matrix(amazon.tdm)
amazon.wordFrequency <- sort(rowSums(m1),decreasing = T)
wordcloud(words = names(amazon.wordFrequency), freq = amazon.wordFrequency, min.freq = 10, random.order = F, colors = rainbow(20))
## positive words cloud
pos.amazon.match <- match(names(amazon.wordFrequency),pos)
pos.amazon.match <- !is.na(pos.amazon.match)
freq.pos.amazon <- amazon.wordFrequency[pos.amazon.match]
p.amazon.names <- names(freq.pos.amazon)
p.amazon.names
wordcloud(p.amazon.names, freq = amazon.wordFrequency, min.freq = 10, random.order = F,
colors = rainbow(20))
## negative words cloud
neg.amazon.match <- match(names(amazon.wordFrequency),neg)
neg.amazon.match <- !is.na(neg.amazon.match)
freq.neg.amazon <- amazon.wordFrequency[neg.amazon.match]
n.amazon.names <- names(freq.neg.amazon)
n.amazon.names
wordcloud(n.amazon.names, freq = amazon.wordFrequency, min.freq = 10, random.order = F,
colors = rainbow(20))