-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7_0_LDA_en_associatedPress.Rmd
307 lines (229 loc) · 6.16 KB
/
7_0_LDA_en_associatedPress.Rmd
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
306
---
title: "Topic modeling on DCard Forum"
author: "Ji-Lung Hsieh"
output:
html_notebook:
number_sections: true
toc: true
toc_float:
collapsed: false
smooth_scroll: false
fig_width: 6
fig_height: 3
fig_caption: true
theme: united
highlight: tango
---
# Basic cases
## Importing library
```{r}
library(dplyr)
library(tidytext)
library(stringr)
library(ggplot2)
library(topicmodels)
```
## Loading data
* `AssociatedPress` is a Document-Term Matrix
```{r}
library(topicmodels)
data("AssociatedPress") # topicmodels::AssociatedPress
AssociatedPress
dim(AssociatedPress)
```
## Building LDA model
```{r}
ap_lda <- LDA(AssociatedPress, k = 2, control = list(seed = 1234))
```
## Convert back to tidy form
* Chapter 5 of the book introduced the `tidy()` method, originally from the `broom` package (Robinson 2017), for tidying model objects. The `tidytext` package provides this method for extracting the per-topic-per-word probabilities, called ββ (“beta”), from the model.
```{r}
ap_topics <- tidytext::tidy(ap_lda, matrix = "beta")
```
## Visualization
```{r}
ap_top_terms <- ap_topics %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
arrange(topic, -beta)
ap_top_terms %>%
mutate(term = reorder(term, beta)) %>%
ggplot(aes(term, beta, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
facet_wrap(~ topic, scales = "free") +
coord_flip()
```
## Comparing topic 1 and 2
```{r}
library(tidyr)
beta_spread <- ap_topics %>%
mutate(topic = paste0("topic", topic)) %>%
spread(topic, beta) %>%
filter(topic1 > .001 | topic2 > .001) %>%
mutate(log_ratio = log2(topic2 / topic1))
beta_spread %>%
group_by(direction = log_ratio > 0) %>%
top_n(10, abs(log_ratio)) %>%
ungroup() %>%
mutate(term = reorder(term, log_ratio)) %>%
ggplot(aes(term, log_ratio)) +
geom_col() +
labs(y = "Log2 ratio of beta in topic 2 / topic 1") +
coord_flip()
```
## Topic probability of Documents
```{r}
ap_documents <- tidy(ap_lda, matrix = "gamma")
ap_documents
```
## Sorting word of Documents
```{r}
tidy(AssociatedPress) %>%
filter(document == 6) %>%
arrange(desc(count))
```
# The example from the book
# Trump's tweets
```{r}
library(jsonlite)
tweets <- fromJSON("data/condensed_2017.json")
text_df <- data_frame(post=1:nrow(tweets), text=tweets$text)
data(stop_words)
reg <- "([^A-Za-z\\d#@']|'(?![A-Za-z\\d#@]))"
text_token <- text_df %>%
filter(!str_detect(text, '^"')) %>%
mutate(text = str_replace_all(text, "https://t.co/[A-Za-z\\d]+|&", "")) %>%
unnest_tokens(word, text, token = "regex", pattern = reg) %>%
# unnest_tokens(word, text) %>%
anti_join(stop_words) %>%
filter(str_detect(word, "[a-z]"))
dtm <- text_token %>%
count(post, word) %>%
cast_dtm(post, word, n)
dtm_lda <- LDA(dtm, k = 10, control = list(seed = 1234))
dtm_topics <- tidy(dtm_lda, matrix = "beta")
top_terms <- dtm_topics %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
arrange(topic, -beta)
top_terms %>%
mutate(term = reorder(term, beta)) %>%
ggplot(aes(term, beta, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
facet_wrap(~ topic, scales = "free") +
coord_flip()
```
# The New York Times
```{r}
library(RTextTools)
library(tm)
data(NYTimes)
data <- NYTimes[sample(1:3100, size=1000, replace=FALSE),]
```
## Creating matrix
```{r}
matrix <- create_matrix(cbind(as.vector(data$Title),
as.vector(data$Subject)),
language="english",
removeNumbers=TRUE,
stemWords=TRUE,
weighting=tm::weightTf)
```
## Modeling
```{r}
k <- length(unique(data$Topic.Code))
lda <- LDA(matrix, k)
terms(lda)
topics(lda)
```
# Dcard
# Loading data
```{r}
post.df <- readRDS("data/dcard_relationship.rds")
dim(post.df)
stopWords <- readRDS("data/stopWords.rds")
```
# doc filter
```{r}
doc_in <- "劈腿" # for observing target
doc_in.v <- unlist(str_split(doc_in, "\\|"))
doc_out <- "" # for filtering irrelevant posts
doc_out.v <- unlist(str_split(doc_out, "\\|"))
post.df <- post.df %>%
filter(str_detect(excerpt, doc_in))
```
## Calculating each document's topic composition
```{r}
ap_documents <- tidy(ap_lda, matrix = "gamma")
ap_documents
tidy(AssociatedPress) %>%
filter(document == 6) %>%
arrange(desc(count))
```
## Tokenized by jieba
```{r}
library(jiebaR)
cutter = worker()
segment_not <- c("前男友","前女友", "女生朋友", "男生朋友", "無接縫", "接軌", "女朋友", "男朋友", "在一起", "劈腿", "渣男", "筋夠軟", "很久", "又要到了", "也要到了", "走不到", "原po", "約炮", "好幾次", "好兄弟", "蠻扯", "以為", "抓包", "陪我")
new_user_word(cutter, c(segment_not, doc_in.v))
```
### cutter and tidyr::unnest()
```{r}
ptm <- proc.time() #95 secs
post.df$word <- sapply(post.df$excerpt,
function(x){tryCatch({cutter[x]},
error=function(err){})})
print(proc.time()-ptm)
library(tidyr)
post.u <- unnest(post.df, word)
```
# Filtering by terms
```{r}
exclude <- c("妳", "我們", "說", "人", "一個", "問")
include <- c("母豬", "破麻", "婊子")
include <- c("前男友")
post.u %<>%
filter(!word %in% stopWords) %>%
filter(!word %in% exclude) %>%
filter(!word %in% doc_in.v) %>%
# %>% filter(word %in% include)
filter(!str_detect(word, "[a-zA-Z0-9]+")) %>%
filter(nchar(word) > 1)
```
# (Deprecated)Counting frequency of words
```{r}
post_word_freq <- text_token %>%
count(post, word, sort=T)
post_words <- post_word_freq %>%
bind_tf_idf(word, post, n)
```
# LDA
```{r}
# Building dtm
dcard_tdm <- post.u %>%
count(id, word) %>%
cast_dtm(id, word, n)
# dcard_tdm
# LDA
dcard_lda <- LDA(dcard_tdm, k = 5, control = list(seed = 1234))
# convert to tidy form for visualization
dcard_topics <- tidy(dcard_lda, matrix = "beta")
```
# Visualization
```{r}
dcard_top_terms <- dcard_topics %>%
group_by(topic) %>%
top_n(15, beta) %>%
ungroup() %>%
arrange(topic, -beta)
# font names https://d.cosx.org/d/101521-101521
dcard_top_terms %>%
mutate(term = reorder(term, beta)) %>%
ggplot(aes(term, beta, fill = factor(topic))) +
geom_col(show.legend = FALSE) +
theme(text=element_text(family="STKaiti")) +
facet_wrap(~ topic, scales = "free") +
coord_flip()
```