-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathScript_Summary.r
More file actions
334 lines (238 loc) · 8.68 KB
/
Script_Summary.r
File metadata and controls
334 lines (238 loc) · 8.68 KB
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
# Part 01 -----------------------------------------------------------------
# 전처리
moon <- raw_moon %>%
str_replace_all("[^가-힣]", " ") %>%
str_squish() %>%
as_tibble()
# 토큰화
word_space <- moon %>%
unnest_tokens(input = value,
output = word,
token = "words")
# 단어 빈도 구하기
word_space <- word_space %>%
count(word, sort = T) %>%
filter(str_count(word) > 1)
# 자주 사용된 단어 추출
top20 <- word_space %>%
head(20)
# Part 02 -----------------------------------------------------------------
## 1. 명사 추출하기
# 명사 기준 토큰화
word_noun <- moon %>%
unnest_tokens(input = value,
output = word,
token = extractNoun)
# -------------------------------------------------------------------------
## 2. 특정 단어가 사용된 문장 살펴보기
# 문장 기준 토큰화
sentences_moon <- raw_moon %>%
unnest_tokens(input = value,
output = sentence,
token = "sentences")
# 특정 단어가 사용된 문장 추출
sentences_moon %>%
filter(str_detect(sentence, "국민"))
# Part 03 -----------------------------------------------------------------
## 1. 단어 빈도 비교하기
# 토큰화
speeches <- speeches %>%
unnest_tokens(input = value,
output = word,
token = extractNoun)
# 하위 집단별 단어 빈도 구하기
frequency <- speeches %>%
count(president, word) %>%
filter(str_count(word) > 1)
# 가장 많이 사용된 단어 추출
top10 <- frequency %>%
group_by(president) %>%
slice_max(n, n = 10, with_ties = F)
# -------------------------------------------------------------------------
## 2. 로그 오즈비로 단어 비교하기
# long form을 wide form으로 변환
frequency_wide <- frequency %>%
pivot_wider(names_from = president,
values_from = n,
values_fill = list(n = 0))
# 로그 오즈비 구하기
frequency_wide <- frequency_wide %>%
mutate(log_odds_ratio = log(((moon + 1) / (sum(moon + 1))) /
((park + 1) / (sum(park + 1)))))
# 상대적으로 중요한 단어 추출
top10 <- frequency_wide %>%
group_by(president = ifelse(log_odds_ratio > 0, "moon", "park")) %>%
slice_max(abs(log_odds_ratio), n = 10, with_ties = F)
# -------------------------------------------------------------------------
## 3. TF-IDF로 단어 비교하기
# TF-IDF 구하기
frequency <- frequency %>%
bind_tf_idf(term = word,
document = president,
n = n) %>%
arrange(-tf_idf)
# 상대적으로 중요한 단어 추출
top10 <- frequency %>%
arrange(tf_idf) %>%
group_by(president) %>%
slice_max(tf_idf, n = 10, with_ties = F)
# Part 04 -----------------------------------------------------------------
## 1. 자주 사용된 감정 단어 살펴보기
# 단어에 감정 점수 부여
word_comment <- word_comment %>%
left_join(dic, by = "word") %>%
mutate(polarity = ifelse(is.na(polarity), 0, polarity))
# 감정 분류
word_comment <- word_comment %>%
mutate(sentiment = ifelse(polarity == 2, "pos",
ifelse(polarity == -2, "neg", "neu")))
# 자주 사용된 감정 단어 추출
top10_sentiment <- word_comment %>%
filter(sentiment != "neu") %>%
count(sentiment, word) %>%
group_by(sentiment) %>%
slice_max(n, n = 10)
# -------------------------------------------------------------------------
## 2. 텍스트의 감정 점수 구하기
# 텍스트별로 단어의 감정 점수 합산
score_comment <- word_comment %>%
group_by(id, reply) %>%
summarise(score = sum(polarity)) %>%
ungroup()
# -------------------------------------------------------------------------
## 3. 감정 범주별 주요 단어 살펴보기
# 감정 범주 변수 생성
score_comment <- score_comment %>%
mutate(sentiment = ifelse(score >= 1, "pos",
ifelse(score <= -1, "neg", "neu")))
# 토큰화 및 전처리
comment <- score_comment %>%
unnest_tokens(input = reply,
output = word,
token = "words",
drop = F) %>%
filter(str_detect(word, "[가-힣]") &
str_count(word) >= 2)
# 감정 범주별 단어 빈도 구하기
frequency_word <- comment %>%
count(sentiment, word, sort = T)
# 로그 오즈비 구하기
comment_wide <- frequency_word %>%
filter(sentiment != "neu") %>%
pivot_wider(names_from = sentiment,
values_from = n,
values_fill = list(n = 0))
comment_wide <- comment_wide %>%
mutate(log_odds_ratio = log(((pos + 1) / (sum(pos + 1))) /
((neg + 1) / (sum(neg + 1)))))
# 긍정, 부정 텍스트에 상대적으로 자주 사용된 단어 추출
top10 <- comment_wide %>%
group_by(sentiment = ifelse(log_odds_ratio > 0, "pos", "neg")) %>%
slice_max(abs(log_odds_ratio), n = 10)
# Part 05 -----------------------------------------------------------------
## 1. 동시 출현 단어 분석 - Co-occurrence analysis
# 품사 기준 토큰화
comment_pos <- news_comment %>%
unnest_tokens(input = reply,
output = word,
token = SimplePos22,
drop = F)
# 명사, 동사, 형용사 추출
comment <- comment_pos %>%
separate_rows(word, sep = "[+]") %>%
filter(str_detect(word, "/n|/pv|/pa")) %>%
mutate(word = ifelse(str_detect(word, "/pv|/pa"),
str_replace(word, "/.*$", "다"),
str_remove(word, "/.*$"))) %>%
filter(str_count(word) >= 2) %>%
arrange(id)
# 단어 동시 출현 빈도 구하기
pair <- comment %>%
pairwise_count(item = word,
feature = id,
sort = T)
# -------------------------------------------------------------------------
## 2. 단어 간 상관 분석 - Phi coefficient
# 파이 계수 구하기
word_cors <- comment %>%
add_count(word) %>%
filter(n >= 20) %>%
pairwise_cor(item = word,
feature = id,
sort = T)
# -------------------------------------------------------------------------
## 3. 연이어 사용된 단어쌍 분석 - n-gram
# 텍스트를 한 행으로 구성
line_comment <- comment %>%
group_by(id) %>%
summarise(sentence = paste(word, collapse = " "))
# 바이그램 토큰화
bigram_comment <- line_comment %>%
unnest_tokens(input = sentence,
output = bigram,
token = "ngrams",
n = 2)
# 바이그램 분리
bigram_seprated <- bigram_comment %>%
separate(bigram, c("word1", "word2"), sep = " ")
# 단어쌍 빈도 구하기
pair_bigram <- bigram_seprated %>%
count(word1, word2, sort = T) %>%
na.omit()
# -------------------------------------------------------------------------
## 4. 네트워크 그래프 만들기
# 네트워크 그래프 데이터 만들기
set.seed(1234)
graph_comment <- pair_bigram %>%
filter(n >= 8) %>%
as_tbl_graph(directed = F) %>%
mutate(centrality = centrality_degree(),
group = as.factor(group_infomap()))
# 네트워크 그래프 만들기
set.seed(1234)
ggraph(graph_comment) +
geom_edge_link() +
geom_node_point(aes(size = centrality,
color = group)) +
geom_node_text(aes(label = name))
# Part 06 -----------------------------------------------------------------
## 1. LDA 모델 만들기
# 문서별 단어 빈도 구하기
count_word_doc <- count_word %>%
count(id, word, sort = T)
# DTM 만들기
dtm_comment <- count_word_doc %>%
cast_dtm(document = id, term = word, value = n)
# LDA 모델 만들기
lda_model <- LDA(dtm_comment,
k = 8,
method = "Gibbs",
control = list(seed = 1234))
# -------------------------------------------------------------------------
## 2. 토픽별 주요 단어 살펴보기
# beta 추출
term_topic <- tidy(lda_model, matrix = "beta")
# 토픽별 beta 상위 단어 추출
top_term_topic <- term_topic %>%
group_by(topic) %>%
slice_max(beta, n = 10)
# -------------------------------------------------------------------------
## 3. 문서를 토픽별로 분류하기
# gamma 추출
doc_topic <- tidy(lda_model, matrix = "gamma")
# 문서별로 확률이 가장 높은 토픽 추출
doc_class <- doc_topic %>%
group_by(document) %>%
slice_max(gamma, n = 1)
# 변수 타입 통일
doc_class$document <- as.integer(doc_class$document)
# 문서에 확률이 가장 높은 토픽 번호 부여
news_comment_topic <- raw_news_comment %>%
left_join(doc_class, by = c("id" = "document"))
# -------------------------------------------------------------------------
## 4. 토픽별 주요 문서 살펴보기
# 특정 토픽에서 gamma가 높은 문서 추출
news_comment_topic %>%
filter(topic == 1) %>%
arrange(-gamma) %>%
select(reply)