-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlda_txt.r
executable file
·474 lines (392 loc) · 15.1 KB
/
lda_txt.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/Rscript --slave
source("common.r")
library(tidytext)
library(forcats)
library(tokenizers)
library(topicmodels)
library(pluralize)
# See http://tidytextmining.com
# read text terms
filenames = readLines(files_path) %>%
as_factor()
vocab = readLines(vocab_path) %>%
as_factor()
text_terms_filtered = readLines(mult_path) %>%
str_extract_all("\\d+:\\d+") %>%
map(str_split, ":", simplify=TRUE) %>%
map(set_colnames, c("term_id", "n")) %>%
map(as_tibble) %>%
bind_rows(.id = "file_id") %>%
transmute(filename = filenames[as.integer(file_id)],
term = vocab[as.integer(term_id)+1],
n = as.integer(n))
# read papers
papers = fromJSON(file.path(data_path, "papers.json")) %>%
as_tibble()
#===============================================================================
# Topic modeling: LDA
#===============================================================================
# convert to DocumentTermMatrix
texts_dtm = text_terms_filtered %>%
cast_dtm(filename, term, n)
# grid of parameters for LDA
# k_vec = c(20, 25, 30)
# alpha_vec = c(0.05, 0.1, 0.2, 0.5)
k_vec = lda_n_topics
alpha_vec = lda_alpha
lda_params = tidyr::crossing(k = k_vec, alpha = alpha_vec) %>%
mutate(log_like = NA)
# loop over the grid
for (i in seq_len(nrow(lda_params))) {
k = lda_params$k[i]
alpha = lda_params$alpha[i]
cat("running lda: k =", k, "\n")
# LDA settings
lda_ctrl = list(alpha = alpha,
estimate.alpha = FALSE,
verbose = 1,
nstart=1,
seed = 4357,
var = list(iter.max=500, tol=1e-6),
em = list(iter.max=1000, tol=1e-4),
keep = 1)
# run LDA
texts_lda = texts_dtm %>%
LDA(k = k, method = "VEM", control = lda_ctrl)
lda_params$alpha[i] = texts_lda@alpha # in case alpha is estimated
lda_params$log_like[i] = last(texts_lda@logLiks)
lda_suffix = paste0("_k", lda_params$k[i], "_alpha", format(lda_params$alpha[i], digits = 2))
# plot log likelihood along iterations
data_frame(iter = seq_len(length(texts_lda@logLiks)),
log_like=texts_lda@logLiks) %>%
ggplot(aes(iter, log_like)) +
geom_point()
# get topics word distributions
beta = texts_lda %>%
tidy(matrix = "beta") %>%
rename(topic_id=topic)
# get documents topic distributions
paper_counts = text_terms_filtered %>%
mutate(filename = as.character(filename)) %>%
group_by(filename) %>%
summarise(n=sum(n))
gamma = texts_lda %>%
tidy(matrix = "gamma") %>%
rename(filename=document, topic_id=topic) %>%
left_join(paper_counts, by="filename") %>%
mutate(gamma=n*gamma) %>% # gamma unnormalized
group_by(topic_id) %>%
mutate(gamma_norm=gamma/sum(gamma)) %>% # gamma normalized by topic
ungroup()
topic_weights = gamma %>%
group_by(topic_id) %>%
summarize(topic_sum=sum(gamma)) %>%
ungroup() %>%
mutate(topic_weight=topic_sum/sum(topic_sum))
# get word assignments
assignments = texts_lda %>%
augment(data = texts_dtm) %>%
rename(filename=document, topic_id=.topic)
# plot topics terms
#===========================================
topic_labeller = function(topic_id) {
ind = match(topic_id, topic_weights$topic_id)
str_c("topic ", topic_id, " [", format(100*topic_weights$topic_weight[ind], digits = 2), "%]")
}
beta %>%
group_by(topic_id) %>%
top_n(10, beta) %>%
mutate(rk = rank(beta, ties.method = "first")) %>%
ungroup() %>%
ggplot(aes(rk, beta, fill = factor(topic_id))) +
geom_col(show.legend = FALSE, alpha=.4) +
geom_text(aes(rk, y=0, label=term), hjust=0) +
facet_wrap(~ topic_id, scales = "free", labeller = as_labeller(topic_labeller)) +
coord_flip() +
theme_minimal() +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
xlab(NULL) + ylab(NULL)
ggsave(file.path(data_path, paste0("topics_terms", lda_suffix, ".png")), height=9, width=16)
# plot topics papers
#===========================================
gamma %>%
group_by(topic_id) %>%
top_n(10, gamma_norm) %>%
mutate(rk = rank(gamma_norm, ties.method = "first")) %>%
ungroup() %>%
left_join(papers %>%
select(filename, title),
by="filename") %>%
ggplot(aes(rk, gamma_norm, fill = factor(topic_id))) +
geom_col(show.legend = FALSE, alpha=.3) +
geom_text(aes(rk, y=0, label=title), hjust=0) +
facet_wrap(~ topic_id, scales = "free", labeller = as_labeller(topic_labeller)) +
coord_flip() +
theme_minimal() +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
xlab(NULL) + ylab(NULL)
ggsave(file.path(data_path, paste0("topics_papers", lda_suffix, ".png")), height=9, width=16)
# subject topic proportions
# =============================
papers_subject_areas = papers %>%
select(filename, subject_area_1, subject_area_2)
gamma_subjects = gamma %>%
left_join(papers_subject_areas, by = "filename") %>%
group_by(topic_id, subject_area_1, subject_area_2) %>%
summarise(gamma = sum(gamma)) %>%
group_by(topic_id) %>%
mutate(gamma_norm = gamma/sum(gamma)) %>%
ungroup()
gamma_subjects %>%
group_by(topic_id) %>%
top_n(10, gamma_norm) %>%
mutate(rk = rank(gamma_norm, ties.method = "first")) %>%
ungroup() %>%
ggplot(aes(rk, gamma_norm, fill = factor(topic_id))) +
geom_col(show.legend = FALSE, alpha=.3) +
geom_text(aes(rk, y=0, label=str_c(subject_area_1, "/" ,subject_area_2)), hjust=0) +
facet_wrap(~ topic_id, scales = "free", labeller = as_labeller(topic_labeller)) +
coord_flip() +
theme_minimal() +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
xlab(NULL) + ylab(NULL)
ggsave(file.path(data_path, paste0("topics_subjects", lda_suffix, ".png")), height=9, width=16)
# plot graph of terms based on topic co-occurences
#=================================================
library(igraph)
beta_filtered = beta %>%
filter(beta>0.03)
terms_graph = beta_filtered %>%
left_join(beta_filtered,
by="topic_id") %>%
filter(term.x < term.y) %>%
group_by(term.x, term.y) %>%
summarise(weight = sum(beta.x*beta.y)) %>%
ungroup() %>%
igraph::graph_from_data_frame(directed=FALSE)
library(ggraph)
set.seed(2017)
terms_graph %>%
ggraph(layout="fr") +
geom_edge_link(aes(edge_alpha = weight), show.legend = FALSE) +
geom_node_point(color = "lightblue", size = 5) +
geom_node_text(aes(label = name), vjust = 1, hjust = 1) +
theme_void()
ggsave(file.path(data_path, paste0("terms_graph", lda_suffix, ".png")), height=9, width=16)
# papers topic proportions
# =============================
paper_topics_nested = gamma %>%
select(filename, topic_id, weight = gamma_norm) %>%
group_by(filename) %>%
arrange(desc(weight)) %>%
nest(.key = "topics")
topic_papers_nested = gamma %>%
select(topic_id, filename, weight = gamma_norm) %>%
left_join(papers %>%
select(paper_id, filename),
by="filename") %>%
select(-filename) %>%
group_by(topic_id) %>%
arrange(desc(weight)) %>%
nest(.key = "papers")
# subject topic proportions
# =============================
topic_subjects_nested = gamma_subjects %>%
select(topic_id, subject_area_1, subject_area_2, weight = gamma_norm) %>%
group_by(topic_id) %>%
arrange(desc(weight)) %>%
nest(.key = "subject_areas")
# topics term proportions
# =======================
topic_terms_nested = beta %>%
select(topic_id, term, weight=beta) %>%
group_by(topic_id) %>%
arrange(desc(weight)) %>%
nest(.key = "terms")
# topic clusters
#================
topic_labels_file = file.path(raw_path, "topic_labels.csv")
download.file(topic_labels_url, topic_labels_file)
topic_labels = read_delim(topic_labels_file, delim = ",", trim_ws = TRUE) %>%
select(topic_id, labels) %>%
mutate(labels = str_split(labels, "/") %>%
map(str_trim))
topic_topic_clusters = topic_weights %>%
# left_join(topic_terms_nested, by="topic_id") %>%
# mutate(labels = map(terms, ~.$term[1])) %>% # use first word
# left_join(topic_subjects_nested, by="topic_id") %>%
# mutate(labels = map(subject_areas, ~.$subject_area_1[1:2])) %>% # use first subject_area_1
left_join(topic_labels, by = "topic_id") %>% # use handmade topic labels
mutate(weight = 1/map_int(labels, length)) %>%
unnest(labels, .drop = FALSE) %>%
rename(label = labels) %>%
filter(!is.na(label))
topic_cluster_weights = topic_topic_clusters %>%
group_by(label) %>%
summarize(topic_cluster_sum=sum(weight*topic_sum),
topics = list(data_frame(topic_id = topic_id,
weight = weight/sum(weight)))) %>%
mutate(topic_cluster_weight=topic_cluster_sum/sum(topic_cluster_sum)) %>%
arrange(desc(topic_cluster_weight)) %>%
mutate(topic_cluster_id = seq_len(n()))
topic_topic_clusters = topic_topic_clusters %>%
left_join(topic_cluster_weights %>%
select(label, topic_cluster_id), by = "label") %>%
select(topic_cluster_id, everything())
topic_clusters_nested = topic_topic_clusters %>%
select(topic_id, topic_cluster_id, weight) %>%
group_by(topic_id) %>%
nest(.key = "topic_clusters")
# join to topics
topics = topic_weights %>%
arrange(desc(topic_weight)) %>%
left_join(topic_terms_nested, by="topic_id") %>%
left_join(topic_papers_nested, by="topic_id") %>%
left_join(topic_subjects_nested, by="topic_id") %>%
left_join(topic_clusters_nested, by="topic_id")
# Analyse topic clusters
# ===========================
# topic clusters terms
topic_cluster_terms_nested = beta %>%
left_join(topic_topic_clusters, by = "topic_id") %>%
group_by(label, term) %>%
summarise(beta = sum(beta*topic_sum*weight)) %>%
group_by(label) %>%
mutate(beta = beta/sum(beta)) %>%
ungroup() %>%
left_join(topic_cluster_weights %>%
select(label, topic_cluster_id), by = "label") %>%
select(topic_cluster_id, term, weight = beta) %>%
group_by(topic_cluster_id) %>%
nest(.key = "terms")
# topic clusters papers
topic_cluster_papers_nested = gamma %>%
left_join(topic_topic_clusters, by = "topic_id") %>%
left_join(papers %>%
select(paper_id, filename),
by="filename") %>%
select(-filename) %>%
group_by(label, paper_id) %>%
summarise(gamma = sum(gamma*weight)) %>%
group_by(label) %>%
mutate(gamma_norm = gamma/sum(gamma)) %>%
ungroup() %>%
left_join(topic_cluster_weights %>%
select(label, topic_cluster_id), by = "label") %>%
select(topic_cluster_id, paper_id, weight = gamma_norm) %>%
group_by(topic_cluster_id) %>%
nest(.key = "papers")
# topic clusters subjects
topic_cluster_subjects_nested = gamma_subjects %>%
left_join(topic_topic_clusters, by = "topic_id") %>%
group_by(label, subject_area_1, subject_area_2) %>%
summarise(gamma = sum(gamma*weight)) %>%
group_by(label) %>%
mutate(gamma_norm = gamma/sum(gamma)) %>%
ungroup() %>%
left_join(topic_cluster_weights %>%
select(label, topic_cluster_id), by = "label") %>%
select(topic_cluster_id, subject_area_1, subject_area_2, weight = gamma_norm) %>%
group_by(topic_cluster_id) %>%
nest(.key = "subject_areas")
# join
topic_clusters = topic_cluster_weights %>%
left_join(topic_cluster_terms_nested, by="topic_cluster_id") %>%
left_join(topic_cluster_papers_nested, by="topic_cluster_id") %>%
left_join(topic_cluster_subjects_nested, by="topic_cluster_id")
# write json
#============
papers = papers %>%
left_join(paper_topics_nested, by = "filename")
papers %>%
toJSON(pretty=TRUE) %>%
write(file.path(data_path, "papers_topics.json"))
topics %>%
toJSON(pretty=TRUE) %>%
write(file.path(data_path, "topics.json"))
topic_clusters %>%
toJSON(pretty=TRUE) %>%
write(file.path(data_path, "topic_clusters.json"))
# write topics.md
#===================
if (require(knitr)) {
print_topic = function(df, n=10) {
out = c(str_c("# [", format(df$topic_weight*100, digit=3), "%] topic ", df$topic_id))
terms = df$terms[[1]] %>%
arrange(desc(weight)) %>%
select(weight, term) %>%
slice(seq_len(n))
out = c(out, knitr::kable(terms), "\n")
papers = df$papers[[1]] %>%
arrange(desc(weight)) %>%
select(weight, title) %>%
slice(seq_len(n))
out = c(out, knitr::kable(papers), "\n")
subject_areas = df$subject_areas[[1]] %>%
arrange(desc(weight)) %>%
select(weight, subject_area_1, subject_area_2) %>%
slice(seq_len(n))
out = c(out, knitr::kable(subject_areas), "\n")
}
paper_titles = papers %>%
select(paper_id, title)
fc = file(file.path(data_path, paste0("topics", lda_suffix, ".md")))
topics %>%
group_by(topic_id) %>%
do(mutate(., papers = map(papers, ~left_join(., paper_titles, by = "paper_id")))) %>%
split(seq_len(nrow(.))) %>%
sapply(print_topic) %>%
writeLines(fc)
print_topic_cluster = function(df, n=10) {
out = c(str_c("# [", format(df$topic_cluster_weight*100, digit=3), "%] topic_cluster ", df$topic_cluster_id, ": ", df$label))
terms = df$terms[[1]] %>%
arrange(desc(weight)) %>%
select(weight, term) %>%
slice(seq_len(n))
out = c(out, knitr::kable(terms), "\n")
papers = df$papers[[1]] %>%
arrange(desc(weight)) %>%
select(weight, title) %>%
slice(seq_len(n))
out = c(out, knitr::kable(papers), "\n")
subject_areas = df$subject_areas[[1]] %>%
arrange(desc(weight)) %>%
select(weight, subject_area_1, subject_area_2) %>%
slice(seq_len(n))
out = c(out, knitr::kable(subject_areas), "\n")
}
fc = file(file.path(data_path, paste0("topic_clusters", lda_suffix, ".md")))
topic_clusters %>%
group_by(topic_cluster_id) %>%
do(mutate(., papers = map(papers, ~left_join(., paper_titles, by = "paper_id")))) %>%
split(seq_len(nrow(.))) %>%
sapply(print_topic_cluster) %>%
writeLines(fc)
close(fc)
}
}
# write theta_v.dat
# -----------------------------
theta_v_path = file.path(data_path, "theta_v.dat")
gamma %>%
select(filename, topic_id, gamma) %>%
spread(topic_id, gamma) %>%
arrange(factor(filename, levels = filenames)) %>%
select(-filename) %>%
write_delim(theta_v_path, delim = " ", col_names = FALSE)
# compute information criterions
# -----------------------------
lda_params = lda_params %>%
mutate(p = k*(n_terms-1) + n_docs*(k-1)) %>% # nb of free parameters
mutate(aic = -2*log_like+2*p+2*p*(p+1)/(n_obs-p-1)) %>%
mutate(bic = -2*log_like+p*log(n_obs)) %>%
mutate(log_pp = (-log_like + p)/n_obs) # log penalized perplexityT
# NOTE: these criterions might be wrong if the loglike returned by LDA is up to a constant
lda_params %>%
filter(alpha<1) %>%
ggplot(aes(k, aic)) +
geom_point()
ggsave(file.path(data_path, "lda_aic.png"), width=8, height=6)