-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_mining_exp_ngram_Q12.R
208 lines (170 loc) · 7.77 KB
/
text_mining_exp_ngram_Q12.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
# Code from: http://www.sthda.com/english/wiki/text-mining-and-word-cloud-fundamentals-in-r-5-simple-steps-you-should-know
# Also from: https://www.tidytextmining.com/ngrams.html
## Install
# install.packages("tm") # for text mining
# install.packages("SnowballC") # for text stemming
# install.packages("wordcloud") # word-cloud generator
# install.packages("RColorBrewer") # color palettes
# install.packages("qdap")
## install.packages("rJava")
## install.packages("RWeka") # to make n-grams
# install.packages("ngram") # to make n-grams
#install.packages("tidytext")
# install.packages("janeaustenr")
# install.packages("igraph") # plots with edge/vertex attributes (a gRaPh)
# install.packages("ggraph") # analogous to ggplot but for igraph objects
#### Load libraries ####
library(tidyverse)
library(modelr)
library(readxl)
library(sandwich)
library(lmtest)
library(utils)
library(openxlsx)
library("tm")
library("SnowballC")
library("wordcloud")
library("RColorBrewer")
library(qdap)
library(rJava)
#library(RWeka)
library(janeaustenr)
library(tidytext) # for sentiments
library(textdata) # for AFINN
library(igraph) # masks decompose, spectrum (from stats), union (from base)
library(ggraph) #
library(reshape2) # to be able to use 'acast' function
#library(webshot) # to save wordclouds as png
#library(htmlwidgets) # to help save wordclouds as png
library(scales)
#### Auxiliary functions ####
visualize_bigrams <- function(bigrams) {
set.seed(2016)
a <- grid::arrow(type = "closed", length = unit(.15, "inches"))
bigrams %>%
graph_from_data_frame() %>%
ggraph(layout = "fr") +
geom_edge_link(aes(edge_alpha = freq), show.legend = FALSE, arrow = a) +
geom_node_point(color = "lightblue", size = 5) +
geom_node_text(aes(label = name), vjust = 1, hjust = 1) +
theme_void()
}
filePath <- "input/analysis_dataset_20210201.xlsx"
#text <- read_excel(path=filePath,range="L1:L52295")
text <- read.xlsx(filePath)
text <- text %>% rename(Q12 = "Q12..Is.there.anything.you.would.like.to.do.with.the.iPad.that.you.can't.at.the.moment?" )
text_c <- text %>% select(Q12) %>% filter(Q12 !="-")
#### Load the data as a corpus ####
docs <- VCorpus(VectorSource(text_c))
docs_f <- VCorpus(VectorSource(text_c))
#### Text transformation #####
toSpace <- content_transformer(function (x , pattern ) gsub(pattern, " ", x))
docs <- tm_map(docs, toSpace, "/")
docs <- tm_map(docs, toSpace, "@")
docs <- tm_map(docs, toSpace, "\\|")
docs_f <- tm_map(docs_f, toSpace, "/")
docs_f <- tm_map(docs_f, toSpace, "@")
docs_f <- tm_map(docs_f, toSpace, "\\|")
# Convert the text to lower case
docs <- tm_map(docs, content_transformer(tolower))
# Remove numbers
docs <- tm_map(docs, removeNumbers)
# Remove english common stopwords
docs <- tm_map(docs, removeWords, stopwords("english"))
# Remove your own stop word
# specify your stopwords as a character vector
docs <- tm_map(docs, removeWords, c("no", "n/a"))
# Remove punctuations
docs <- tm_map(docs, removePunctuation)
# Eliminate extra white spaces
docs <- tm_map(docs, stripWhitespace)
# Text stemming
#docs <- tm_map(docs, stemDocument)
# Convert the text to lower case
docs_f <- tm_map(docs_f, content_transformer(tolower))
# Remove numbers
docs_f <- tm_map(docs_f, removeNumbers)
# Remove english common stopwords
docs_f <- tm_map(docs_f, removeWords, stopwords("english"))
# Remove your own stop word
# specify your stopwords as a character vector
docs_f <- tm_map(docs_f, removeWords, c("no", "n/a"))
# Remove punctuations
docs_f <- tm_map(docs_f, removePunctuation)
# Eliminate extra white spaces
docs_f <- tm_map(docs_f, stripWhitespace)
# Text stemming
#docs_f <- tm_map(docs_f, stemDocument)
#### Tokenizer / N-Grammer (pairs) ####
token_size <- 2
NLP_tokenizer <- function(x) {
unlist(lapply(ngrams(words(x), token_size), paste, collapse = " "), use.names = FALSE)
}
control_list_ngram = list(tokenize = NLP_tokenizer)
#### Build a term-document matrix ####
dtm <- TermDocumentMatrix(docs,control_list_ngram)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v) %>% filter(!grepl("na",word))
head(d, 40)
#### Plot word frequencies ####
d$word <- factor(d$word,rev(unique(d$word))) # to make sure "Words" are plotted in correct order rather than alphabetically
# mutate(word = reorder(word, n)) %>% ## try this as well to reorder!
ggplot(data=d[1:20,],mapping=aes(x=word,y=freq)) + geom_col(fill="deepskyblue") +
labs(y="Word frequencies",x="Words",title="Q12 - Most frequent pairs of words") + theme(axis.text.x=element_text(angle=60,hjust=1)) +
coord_flip()+ labs(caption="Frequency that two words appear next to each other in a reply\n (excluding connectors, common stopwords, punctuation etc)")+
geom_text(aes(label=freq),position = position_dodge(width = 1), hjust = 0, size = 5)+
theme(text=element_text(size=18))
ggsave("./output/Q12_wordpairfreq.png",width = 24, height = 20, dpi=300,units ="cm")
#### Generate the Word Cloud ####
set.seed(1234)
my_cloud<-wordcloud(words = d$word, freq = d$freq, min.freq = 1,
max.words=30, random.order=FALSE, rot.per=0,
colors=brewer.pal(8, "Dark2"))
#saveWidget(my_cloud,"tmp.html",selfcontained = F)
#### Create a graph for bigrams ####
att_sep <- ifelse(token_size==3,list(c("word1","word2","word3")),list(c("word1", "word2"))) %>% .[[1]]
dd <- d %>% filter(!grepl("na",word)) %>% separate(word, att_sep, sep = " ")
dd %>% filter(freq>5) %>% visualize_bigrams()
##### Explore frequent terms and their associations / correlations ####
#findFreqTerms(dtm, lowfreq = 1000)
#ass <- findAssocs(dtm, terms = "ipad", corlimit = 0.01)
#list_vect2df(ass$ipad)
#### Tokenizer / N-Grammer (triplet) ####
token_size <- 3
NLP_tokenizer <- function(x) {
unlist(lapply(ngrams(words(x), token_size), paste, collapse = " "), use.names = FALSE)
}
control_list_ngram = list(tokenize = NLP_tokenizer)
#### Build a term-document matrix ####
dtm <- TermDocumentMatrix(docs,control_list_ngram)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v) %>% filter(!grepl("na",word))
head(d, 40)
#### Plot word frequencies ####
d$word <- factor(d$word,rev(unique(d$word))) # to make sure "Words" are plotted in correct order rather than alphabetically
# mutate(word = reorder(word, n)) %>% ## try this as well to reorder!
ggplot(data=d[1:15,],mapping=aes(x=word,y=freq)) + geom_col(fill="deepskyblue") +
labs(y="Word frequencies",x="Words",title="Q12 - Most frequent triplets of words") + theme(axis.text.x=element_text(angle=60,hjust=1)) +
coord_flip()+ labs(caption="Frequency that three words appear next to each other in a reply\n (excluding connectors, common stopwords, punctuation etc)")+
geom_text(aes(label=freq),position = position_dodge(width = 1), hjust = 0, size = 5)+
theme(text=element_text(size=18))
ggsave("./output/Q12_wordtripfreq.png",width = 24, height = 20, dpi=300,units ="cm")
#### Single word ####
dtm_w <- TermDocumentMatrix(docs)
m_w <- as.matrix(dtm_w)
v_w <- sort(rowSums(m_w),decreasing=TRUE)
d_w <- data.frame(word = names(v_w),freq=v_w)
head(d_w, 40)
d_w$word <- factor(d_w$word,rev(unique(d_w$word)))
ggplot(data=d_w[1:20,],mapping=aes(x=word,y=freq)) + geom_col(fill="deepskyblue") +
labs(y="Word frequencies",x="Words",title="Q12 - Most frequent words") + theme(axis.text.x=element_text(angle=60,hjust=1)) +
coord_flip()+ labs(caption="Frequency that a word appears\n (top is excluding connectors, common stopwords, punctuation etc)")+
geom_text(aes(label=freq),position = position_dodge(width = 1), hjust = 0, size = 5)+
theme(text=element_text(size=18))
ggsave("./output/Q12_wordsinglefreq.png",width = 24, height = 20, dpi=300,units ="cm")
set.seed(1234)
my_cloud<-wordcloud(words = d_w$word, freq = d_w$freq, min.freq = 1,
max.words=40, random.order=FALSE, rot.per=0,
colors=brewer.pal(8, "Dark2"))