-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathV2.Rmd
207 lines (158 loc) · 5.3 KB
/
V2.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
---
title: "songs"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r message=FALSE, warning=FALSE, error=FALSE}
library(jiebaR)
library(stringr)
# Get txt file paths
fps_m <- list.files('male', full.names = T)
fps_f <- list.files('female', full.names = T)
# Initialize jiebaR
seg <- worker()
# determining song names
name_m <- vector('character', length(fps_m))
for (i in seq_along(fps_m)) {
name_m[i] <- basename(fps_m[i])
}
name_f <- vector('character', length(fps_f))
for (i in seq_along(fps_f)) {
name_f[i] <- basename(fps_f[i])
}
# determining lyrics
lyric_m <- vector('character', length(fps_m))
for (i in seq_along(fps_m)) {
l_m <- readLines(fps_m[i], encoding = 'UTF-8') %>% str_squish()
segged_m <- segment(l_m, seg)
lyric_m[i] <- paste0(segged_m, collapse = ' ')
}
lyric_f <- vector('character', length(fps_f))
for (i in seq_along(fps_f)) {
l_f <- readLines(fps_f[i], encoding = 'UTF-8') %>% str_squish()
segged_f <- segment(l_f, seg)
lyric_f[i] <- paste0(segged_f, collapse = ' ')
}
# Combine results into dfs
male_df <- tibble::tibble(編號 = seq_along(fps_m), 性別 = '男', 歌名 = name_m, 歌詞 = lyric_m)
female_df <- tibble::tibble(編號 = seq_along(fps_f), 性別 = '女', 歌名 = name_f, 歌詞 = lyric_f)
```
```{r message=FALSE}
library(tidytext)
library(dplyr)
library(readxl)
stopwords <- read_excel('ch_stop_words.xlsx')
tidy_male <- male_df %>%
unnest_tokens(output = '用詞', input = '歌詞', token = 'regex', pattern = ' ') %>%
anti_join(get_stopwords(), by = c('用詞' = 'word')) %>%
anti_join(stopwords, by = c('用詞' = '停用詞'))
freq_male <- tidy_male %>%
group_by(用詞) %>%
summarize(個數 = n()) %>%
arrange(desc(個數)) %>%
print()
tidy_female <- female_df %>%
unnest_tokens(output = '用詞', input = '歌詞', token = 'regex', pattern = ' ') %>%
anti_join(get_stopwords(), by = c('用詞' = 'word')) %>%
anti_join(stopwords, by = c('用詞' = '停用詞'))
freq_female <- tidy_female %>%
group_by(用詞) %>%
summarize(個數 = n()) %>%
arrange(desc(個數)) %>%
print()
```
```{r message=FALSE}
library(ggplot2)
freq_male %>%
top_n(20, 個數) %>%
ggplot() +
geom_bar(aes(用詞, 個數), stat = 'identity') +
coord_flip() +
labs(title = '男歌手高頻用詞')
freq_female %>%
top_n(20, 個數) %>%
ggplot() +
geom_bar(aes(用詞, 個數), stat = 'identity') +
coord_flip() +
labs(title = '女歌手高頻用詞')
```
```{r message=FALSE, warning=FALSE}
library(wordcloud2)
library(webshot)
webshot::install_phantomjs()
library("htmlwidgets")
wordcloud_m <- wordcloud2(freq_male, shape = 'star')
saveWidget(wordcloud_m, 'male.html', selfcontained = F)
webshot('male.html', 'male.png', delay = 5)
```
```{r message=FALSE}
wordcloud_f <- wordcloud2(freq_female, shape = 'star')
saveWidget(wordcloud_f, 'female.html', selfcontained = F)
webshot('female.html', 'female.png', delay = 5)
```
```{r message=FALSE, warning=FALSE}
library(quanteda)
library(stm)
library(ggplot2)
tidy_both <- rbind(male_df, female_df) %>%
unnest_tokens(output = '用詞', input = '歌詞', token = 'regex', pattern = ' ') %>%
anti_join(get_stopwords(), by = c('用詞' = 'word')) %>%
anti_join(stopwords, by = c('用詞' = '停用詞'))
dfm_both <- tidy_both %>%
count(歌名, 用詞, sort = TRUE) %>%
cast_dfm(歌名, 用詞, n)
dfm_male <- tidy_male %>%
count(歌名, 用詞, sort = TRUE) %>%
cast_dfm(歌名, 用詞, n)
dfm_female <- tidy_female %>%
count(歌名, 用詞, sort = TRUE) %>%
cast_dfm(歌名, 用詞, n)
topic_model_both <- stm(dfm_both, K = 6, verbose = FALSE, init.type = "Spectral")
topic_model_male <- stm(dfm_male, K = 6, verbose = FALSE, init.type = "Spectral")
topic_model_female <- stm(dfm_female, K = 6, verbose = FALSE, init.type = "Spectral")
td_beta_both <- tidy(topic_model_both)
td_beta_both %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
mutate(topic = paste0('主題 ', topic),
term = reorder_within(term, beta, topic)) %>%
ggplot(aes(term, beta, fill = as.factor(topic))) +
geom_col(alpha = 0.8, show.legend = FALSE) +
facet_wrap(~ topic, scales = "free_y") +
coord_flip() +
scale_x_reordered() +
labs(x = NULL, y = expression(beta), title = '各主題高頻用詞')
```
```{r}
td_beta_male <- tidy(topic_model_male)
td_beta_male %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
mutate(topic = paste0('主題 ', topic),
term = reorder_within(term, beta, topic)) %>%
ggplot(aes(term, beta, fill = as.factor(topic))) +
geom_col(alpha = 0.8, show.legend = FALSE) +
facet_wrap(~ topic, scales = "free_y") +
coord_flip() +
scale_x_reordered() +
labs(x = NULL, y = expression(beta), title = '男歌手各主題高頻用詞')
```
```{r}
td_beta_female <- tidy(topic_model_female)
td_beta_female %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
mutate(topic = paste0('主題 ', topic),
term = reorder_within(term, beta, topic)) %>%
ggplot(aes(term, beta, fill = as.factor(topic))) +
geom_col(alpha = 0.8, show.legend = FALSE) +
facet_wrap(~ topic, scales = "free_y") +
coord_flip() +
scale_x_reordered() +
labs(x = NULL, y = expression(beta), title = '女歌手各主題高頻用詞')
```