-
Notifications
You must be signed in to change notification settings - Fork 0
/
seededLDA_2.R
415 lines (341 loc) · 10.4 KB
/
seededLDA_2.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
#install.packages("tidyverse")
#install.packages("tidytext")
#install.packages("lubridate")
#install.packages("quanteda")
#install.packages("readtext")
#install.packages("seededlda")
#install.packages("writexl")
#install.packages("reticulate")
library(tidyverse)
library(tidytext)
library(lubridate)
library(quanteda)
library(readtext)
library(seededlda)
library(writexl)
library(reticulate)
#setwd("./카카오톡 받은 파일")
pkg_v <- c("tidyverse", "tidytext", "lubridate", "quanteda", "readtext", "seededlda")
purrr::map(pkg_v, require, ch = T)
file_path <- "./everytime_keywords2.xlsx"
readxl::read_excel(file_path) %>%
glimpse()
readxl::read_excel(file_path) %>%
select(text, keyword) -> vac_df
vac_df %>% head(3)
# nan값 제거
#na.omit(vac_df) ->vac_df
# 발화별 ID 부여
vac_df %>%
mutate(ID = factor(row_number())) -> vac2_df
vac2_df %>% glimpse()
vac2_df %>%
unnest_tokens(word, keyword, token = "regex", pattern = ",") -> vac_tk
vac_tk %>%
select(text, word) %>%
head(n = 5)
vac_tk %>%
count(word, sort = T) -> count_df
count_df %>% head(n = 10)
#텍스트 전처리
combined_df <-
vac_tk %>%
group_by(ID) %>%
summarise(text2 = str_flatten(word, " ")) %>%
ungroup() %>%
inner_join(vac2_df, by = "ID")
combined_df %>% glimpse()
combined_df %>%
corpus(text_field = "text2") -> c_corp
c_corp %>% glimpse()
c_corp <- quanteda::corpus(combined_df, text_field = "text2")
docvars_df <- quanteda::docvars(c_corp)
docvars_df %>% glimpse()
c_corp %>%
tokens(remove_punct = T) %>%
dfm() %>%
dfm_trim(min_termfreq = 0.8,
termfreq_type = "quantile",
max_docfreq = 0.1,
docfreq_type = "prop") -> c_dfm
c_dfm %>% topfeatures(n = 20)
set.seed(37)
c_dfm %>% textmodel_lda(k = 13) -> c_lda
c_lda %>% glimpse()
# lda 성능 평가
c_lda %>% divergence(weighted = TRUE, min_size = 0.01, select = NULL)->r
print(r)
c_lda %>% terms(n = 10) %>% as.data.frame() %>%
select(topic1:topic13)
# phi threshold 지정
phi_threshold <- 0.015
# meaningful keyword 추출
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic1 %>% desc) %>%
round(digits = 3) %>%
subset(topic1>threshold) -> meaningful1
dimnames(meaningful1)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic2 %>% desc) %>%
round(digits = 3) %>%
subset(topic2>threshold) -> meaningful2
dimnames(meaningful2)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic3 %>% desc) %>%
round(digits = 3) %>%
subset(topic3>threshold) -> meaningful3
dimnames(meaningful3)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic4 %>% desc) %>%
round(digits = 3) %>%
subset(topic4>threshold) -> meaningful4
dimnames(meaningful4)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic5 %>% desc) %>%
round(digits = 3) %>%
subset(topic5>threshold) -> meaningful5
dimnames(meaningful5)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic6 %>% desc) %>%
round(digits = 3) %>%
subset(topic6>threshold) -> meaningful6
dimnames(meaningful6)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic7 %>% desc) %>%
round(digits = 3) %>%
subset(topic7>threshold) -> meaningful7
dimnames(meaningful7)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic8 %>% desc) %>%
round(digits = 3) %>%
subset(topic8>threshold) -> meaningful8
dimnames(meaningful8)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic9 %>% desc) %>%
round(digits = 3) %>%
subset(topic9>threshold) -> meaningful9
dimnames(meaningful9)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic10 %>% desc) %>%
round(digits = 3) %>%
subset(topic10>threshold) -> meaningful10
dimnames(meaningful10)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic11 %>% desc) %>%
round(digits = 3) %>%
subset(topic11>threshold) -> meaningful11
dimnames(meaningful11)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic12 %>% desc) %>%
round(digits = 3) %>%
subset(topic12>threshold) -> meaningful12
dimnames(meaningful12)[[1]]
c_lda$phi %>% t() %>%
as.data.frame() %>%
arrange(topic13 %>% desc) %>%
round(digits = 3) %>%
subset(topic13>threshold) -> meaningful13
dimnames(meaningful13)[[1]]
keywords_vec <- c(dimnames(meaningful1)[[1]],
dimnames(meaningful2)[[1]],
dimnames(meaningful3)[[1]],
dimnames(meaningful4)[[1]],
dimnames(meaningful5)[[1]],
dimnames(meaningful6)[[1]],
dimnames(meaningful7)[[1]],
dimnames(meaningful8)[[1]],
dimnames(meaningful9)[[1]],
dimnames(meaningful10)[[1]],
dimnames(meaningful11)[[1]],
dimnames(meaningful12)[[1]],
dimnames(meaningful13)[[1]])
keywords_vec
c_lda$theta %>% as.data.frame() -> clda_theta_df
clda_theta_df %>%
select(topic1:topic13) %>%
round(digits = 3) %>% head(3)
quanteda::docvars(c_corp) -> docvars_df
docvars_df %>% glimpse()
bind_cols(docvars_df, clda_theta_df) %>%
mutate(textID = factor(row_number()), .before = ID) -> theta_df
theta_df %>% glimpse()
theta_df %>%
writexl::write_xlsx("theta_df.xlsx")
theta_df %>%
select(topic1:topic10) %>%
pmap(sum) %>%
head(3)
theta_df %>%
arrange(topic9 %>% desc) %>%
pull(text) %>% head(5)
c_lda %>% terms(n = 5) %>% as.data.frame() -> c_ldaterm5_df
c_ldaterm5_df %>%
writexl::write_xlsx("ldaterms.xlsx")
# 우울 발화 사전 불러오기
depressed_dic <- read_csv(file="cleaned_similarity80.csv")
depressed_dic1 <- subset(depressed_dic, tag == 1, select=c(cleaned_word))$cleaned_word
depressed_dic2 <- subset(depressed_dic, tag == 2, select=c(cleaned_word))$cleaned_word
depressed_dic3 <- subset(depressed_dic, tag == 3, select=c(cleaned_word))$cleaned_word
depressed_dic4 <- subset(depressed_dic, tag == 4, select=c(cleaned_word))$cleaned_word
depressed_dic5 <- subset(depressed_dic, tag == 5, select=c(cleaned_word))$cleaned_word
depressed_dic6 <- subset(depressed_dic, tag == 6, select=c(cleaned_word))$cleaned_word
depressed_dic7 <- subset(depressed_dic, tag == 7, select=c(cleaned_word))$cleaned_word
depressed_dic8 <- subset(depressed_dic, tag == 8, select=c(cleaned_word))$cleaned_word
depressed_dic9 <- subset(depressed_dic, tag == 9, select=c(cleaned_word))$cleaned_word
depressed_dic1 %>% strsplit(',') %>% unlist -> dep_dic1
depressed_dic2 %>% strsplit(',') %>% unlist -> dep_dic2
depressed_dic3 %>% strsplit(',') %>% unlist -> dep_dic3
depressed_dic4 %>% strsplit(',') %>% unlist -> dep_dic4
depressed_dic5 %>% strsplit(',') %>% unlist -> dep_dic5
depressed_dic6 %>% strsplit(',') %>% unlist -> dep_dic6
depressed_dic7 %>% strsplit(',') %>% unlist -> dep_dic7
depressed_dic8 %>% strsplit(',') %>% unlist -> dep_dic8
depressed_dic9 %>% strsplit(',') %>% unlist -> dep_dic9
# list(
# s1우울기분 = c("혼자", "기분", "싫다","아프다", "마음", "행복하다", "우울증",
# "우울하다"),
# s2흥미감소 = c("공부", "싫다"),
# s3체중변화 = c("버리다", "먹다", "약", "안", "많이", "살"),
# s4수면장애 = c("자다", "많이", "잠", "안"),
# s5초조지연 = c("시간", "공부", "시험", "무섭디"),
# s6활력상실 = c("공부", "만나다"),
# s7무가치감 = c("혼자", "친구", "동생", "부모님"),
# s8집중력감소 = c("공부"),
# s9자살사고 = c("자살", "살다", "살", "죽다")
# ) -> ldaterm_dsm5_l
# seed word 구성
list() -> ldaterm_dsm5_l
#s1
seed_vec <- c()
for (dep in dep_dic1){
if (dep %in% keywords_vec){
seed_vec <- c(seed_vec, dep)
}
}
seed_vec<- unique(seed_vec)
ldaterm_dsm5_l <- c(ldaterm_dsm5_l, list(s1=seed_vec))
#s2
seed_vec <- c()
for (dep in dep_dic2){
if (dep %in% keywords_vec){
seed_vec <- c(seed_vec, dep)
}
}
seed_vec<- unique(seed_vec)
ldaterm_dsm5_l <- c(ldaterm_dsm5_l, list(s2=seed_vec))
#s3
seed_vec <- c()
for (dep in dep_dic3){
if (dep %in% keywords_vec){
seed_vec <- c(seed_vec, dep)
}
}
seed_vec<- unique(seed_vec)
ldaterm_dsm5_l <- c(ldaterm_dsm5_l, list(s3=seed_vec))
#s4
seed_vec <- c()
for (dep in dep_dic4){
if (dep %in% keywords_vec){
seed_vec <- c(seed_vec, dep)
}
}
seed_vec<- unique(seed_vec)
ldaterm_dsm5_l <- c(ldaterm_dsm5_l, list(s4=seed_vec))
#s5
seed_vec <- c()
for (dep in dep_dic5){
if (dep %in% keywords_vec){
seed_vec <- c(seed_vec, dep)
}
}
seed_vec<- unique(seed_vec)
ldaterm_dsm5_l <- c(ldaterm_dsm5_l, list(s5=seed_vec))
#s6
seed_vec <- c()
for (dep in dep_dic6){
if (dep %in% keywords_vec){
seed_vec <- c(seed_vec, dep)
}
}
seed_vec<- unique(seed_vec)
ldaterm_dsm5_l <- c(ldaterm_dsm5_l, list(s6=seed_vec))
#s7
seed_vec <- c()
for (dep in dep_dic7){
if (dep %in% keywords_vec){
seed_vec <- c(seed_vec, dep)
}
}
seed_vec<- unique(seed_vec)
ldaterm_dsm5_l <- c(ldaterm_dsm5_l, list(s7=seed_vec))
#s8
seed_vec <- c()
for (dep in dep_dic8){
if (dep %in% keywords_vec){
seed_vec <- c(seed_vec, dep)
}
}
seed_vec<- unique(seed_vec)
ldaterm_dsm5_l <- c(ldaterm_dsm5_l, list(s8=seed_vec))
#s9
seed_vec <- c()
for (dep in dep_dic9){
if (dep %in% keywords_vec){
seed_vec <- c(seed_vec, dep)
}
}
seed_vec<- unique(seed_vec)
ldaterm_dsm5_l <- c(ldaterm_dsm5_l, list(s9=seed_vec))
ldaterm_dsm5_l
c(ldaterm_dsm5_l) -> ldaterm_l
dictionary(ldaterm_l) -> dict_topic
set.seed(37)
Sys.time() -> t1
c_dfm %>%
textmodel_seededlda(dictionary = dict_topic) -> c_slda
Sys.time() -> t2
t2 - t1
terms(c_slda, 9)
c_slda$theta %>% as.data.frame() -> cslda_theta_df
quanteda::docvars(c_corp) -> docvars_df
bind_cols(docvars_df, cslda_theta_df) %>%
mutate(textID = row_number(), .before = ID) -> slda_theta_df
slda_theta_df %>% glimpse()
slda_theta_df %>%
writexl::write_xlsx("slda_theta_df.xlsx")
slda_theta_df %>%
arrange(s1 %>% desc) %>%
select(text) %>%
head(10)
slda_theta_df %>%
arrange(s1 %>% desc) %>% head(5) %>% .$textID -> id_v
slda_theta_df %>%
filter(textID %in% id_v) %>%
pull(text)
topics(c_slda) -> r
c(r) -> result
y.matrix = matrix(data = result, ncol = 1, byrow = TRUE)
y.df = as.data.frame(y.matrix)
# seeded LDA 결과 저장
cbind(slda_theta_df, y.df) -> result_df
names(result_df)
names(result_df)[14] <- c("label")
result_df %>%
writexl::write_xlsx("slda_result_0603.xlsx")
# 일반 LDA 성능과 seeded LDA 성능 비교(어느 쪽이 cluster가 더 잘 형성 되었는지)
c_lda %>% divergence(weighted = TRUE, min_size = 0.01, select = NULL)->r
print(r)
c_slda %>% divergence(weighted = TRUE, min_size = 0.01, select = NULL)->sr
print(sr)