-
Notifications
You must be signed in to change notification settings - Fork 1
/
got_finale_wordcloud & Sentiment Analysis .R
259 lines (174 loc) · 6.95 KB
/
got_finale_wordcloud & Sentiment Analysis .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
# Word cloud, Sentiment Analysis and Network graphs for GOT Final episode fan reviews
# Author - Abhirami A
#install.packages("wordcloud2")
# Load
library("tm")
library("SnowballC")
library("wordcloud")
library("wordcloud2")
library("RColorBrewer")
library("stringr")
getwd()
setwd("H:/DATA SCIENCE/DS & ML")
got_file_loc <- ("H:/DATA SCIENCE/DS & ML/Word Cloud & Sentiment analysis Projects/GOT_finale_fan_reactions/got_finale_fan_review.txt")
got_text <- readLines(got_file_loc)
got_text <- str_replace_all(got_text ,'[^ a-zA-Z]'," ")
class(got_text)
length(got_text)
head(got_text)
head(got_df)
x## Load the data as Corpus
got_docs <- Corpus(VectorSource(got_text))
class(got_docs)
# View the content of the
inspect(got_docs)
toSpace <- content_transformer(function (x , pattern ) gsub(pattern, " ",
x))
got_docs <- tm_map(got_docs, toSpace, "/")
got_docs <- tm_map(got_docs, toSpace, "@")
got_docs <- tm_map(got_docs, toSpace, "\\|")
got_docs <- tm_map(got_docs, toSpace, "\\-")
# Convert the text to lower case
got_docs <- tm_map(got_docs, content_transformer(tolower))
# Remove numbers
got_docs <- tm_map(got_docs, removeNumbers)
# Remove english common stopwords
got_docs <- tm_map(got_docs, removeWords, stopwords("english"))
# Remove your own stop word
# specify your stopwords as a character vector
got_docs <- tm_map(got_docs, removeWords, c("game of thrones","one","two","thrones","last","ending","got","snow",
"three","four","five","six","season","show",
"seven","eight","fans","game","said","shows","final","series","now","getting","also","didn","though","couln","can","episodes","around","episodes","don","first",
"can","made","saw","shows","finale","episode","episodes"))
# Remove punctuations
got_docs <- tm_map(got_docs, removePunctuation)
# Eliminate extra white spaces
got_docs <- tm_map(got_docs, stripWhitespace)
# Text stemming
#stemDocument(c("episodes","episode))
got_dtm <- TermDocumentMatrix(got_docs)
m <- as.matrix(got_dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v,stringsAsFactors = F)
head(d, 10)
glimpse(d)
set.seed(1234)
windows() ## create window to plot your file
wordcloud2(d , size=.7)
#wordcloud2(d,shape = 'cardioid')
#wordcloud(words = d$word, freq = d$freq, min.freq = 1,
# max.words=200, random.order=FALSE, rot.per=0.35,
# colors=brewer.pal(8, "Dark2"))
# __________ SENTIMENT ANALYSIS________________________
#nrc sentiment lexicon
require(tidytext)
require(dplyr)
get_sentiments("nrc")
get_sentiments("nrc") %>% group_by(sentiment) %>% summarise( n() )
#bing sentiment lexicon
get_sentiments("bing")
get_sentiments("bing") %>% group_by(sentiment) %>% summarise( n() )
#afinn sentiment lexicon
get_sentiments("afinn")
get_sentiments("afinn") %>% mutate(Sentiment = ifelse(score < 0 , 'Negative',
'Positive')) %>%
group_by(Sentiment) %>% summarise( n() , sum(score))
#Loughran sentiment lexicon
get_sentiments("loughran")
get_sentiments("loughran") %>% group_by(sentiment) %>% summarise( n() )
#GOT finale fan reviews sentiment analysis
# nrc lexicon
got_finale_sentiment_nrc <- d %>%
right_join(get_sentiments("nrc")) %>%
filter(!is.na(sentiment)) %>%
count(sentiment, sort = TRUE)
Sentiment_Colors <- c("red","green","grey","pink",
"blue","violet","yellow","springgreen",
"orange","magenta" )
barplot(got_finale_sentiment_nrc$n,
names.arg=got_finale_sentiment_nrc$sentiment,
xlab="Sentiment (nrc)",ylab="Count",
col=Sentiment_Colors,
main="GOT Finale Fan Reviews Sentiment Analysis",
las=2)
# bing Lexicon
got_finale_sentiment_bing <- d %>%
right_join(get_sentiments("bing")) %>%
filter(!is.na(sentiment)) %>%
count(sentiment, sort = TRUE)
barplot(got_finale_sentiment_bing$n,
names.arg=got_finale_sentiment_bing$sentiment,
xlab="Sentiment(bing)",ylab="Count",
col=c("red","green"),
main="GOT Finale Fan Reviews Sentiment Analysis",
las=2)
barplot(got_finale_sentiment_bing$n,
names.arg=got_finale_sentiment_bing$sentiment,
xlab="Sentiment",ylab="Count",
col=c("red","green"),
main="Sentiment chart",
las=2,
horiz = T)
#pie chart
slices <- got_finale_sentiment_bing$n
lbls <- got_finale_sentiment_bing$sentiment
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
pie(slices,labels = lbls, col = rainbow(length(lbls)),
main = "GOT Finale Fan Review Sentiment Analysis(bing)")
# 3D Exploded Pie Chart
#install.packages("plotrix", dependencies = T)
library(plotrix)
pie3D(slices,labels=lbls,explode=0.1,
main="GOT Finale Fan Review Sentiment Analysis(bing)")
## Wordcloud depicting the sentiment
#----------------------------------
require(reshape2)
windows()
got_finale_sentiment_nrc %>%
inner_join(get_sentiments("nrc")) %>%
count(word, sentiment, sort = TRUE) %>%
acast(word ~ sentiment, value.var = "n", fill = 0) %>%
comparison.cloud(colors = c("#F8766D", "#00BFC4"))
# Tokenizing by n-gram
#------------------------
require(tidytext)
bigram <- tibble()
got_df <- as.data.frame(got_text, stringsAsFactors = F)
class(got_df)
glimpse(got_df)
got_finale_Bi_Gram <- got_df %>% unnest_tokens(bigram,got_df,token = "ngrams", n = 2,n_min =2)
#Now , We can examine the most common bigrams
#------------------------------------------------
got_finale_Bi_Gram %>% count(bigram, sort = TRUE)
#Applying Stop Words and removing common un-intersting words
#----------------------------------------------------------------
require(tidyr)
# C.1) Separate the Bigram into individual words
bigrams_separated <- got_finale_Bi_Gram %>%
separate(bigram, c("word1", "word2"), sep = " ")
bigrams_filtered <- bigrams_separated %>%
filter(!word1 %in% stop_words$word) %>%
filter(!word2 %in% stop_words$word)
# new bigram counts:
bigram_counts <- bigrams_filtered %>%
count(word1, word2, sort = TRUE)
bigram_counts
# Unite the separated words of Bigram
bigrams_united <- bigrams_filtered %>%
unite(bigram, word1, word2, sep = " ")
bigrams_united
bigrams_united %>% count(bigram, sort = TRUE)
#______________________________________________
## Visualizing a network of bigrams with ggraph
require(igraph)
require(ggraph)
# filter for only relatively common combinations
bigram_graph <- bigram_counts %>%
filter(n > 600) %>%
graph_from_data_frame()
set.seed(2019)
ggraph(bigram_graph, layout = "fr") +
geom_edge_link() +
geom_node_point() +
geom_node_text(aes(label = name), vjust = 1, hjust = 1)