library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.1.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(KoNLP)
## Checking user defined dictionary!
library(wordcloud2)
library(webshot)
library(htmlwidgets)
library(RColorBrewer)
useNIADic()
## Backup was just finished!
## 1213109 words dictionary was built.
# install.packages("webshot")
# webshot::install_phantomjs()

2000년 - 2010년 다이나믹듀오 노래(불면증, 고백, 어머니의 된장국) 3곡의 가사로 워드클라우드 생성

readLines("../Data/dynamicduo_lyrics2000.txt", encoding = "UTF-8") %>% 
    str_replace_all("\\W", " ") %>% 
    str_replace_all("\\d", " ") %>% 
    str_squish() %>%      # str_squish : 사이드에 있는 space를 삭제하고, 가운데는 하나만 남김
    extractNoun() -> DD_lyrics2000

DD_lyrics2000 %>% unlist() %>% table() %>% data.frame() %>% 
    rename_with(function(n) {
        c('word','freq')
    }) %>% 
    filter(str_length(word)>=2) %>% 
    arrange(desc(freq))-> df_lyrics2000

DynamicDuo_2000Lyrics = wordcloud2(head(df_lyrics2000, n=200), shape = "circle", size = 0.8)   
saveWidget(DynamicDuo_2000Lyrics,"tmp1.html", selfcontained = F)
webshot("tmp1.html", "DynamicDuo_2000Lyrics.png", delay = 5, vwidth = 480, vheight = 480)

2010년 - 2020년 다이나믹듀오 노래(봉제선, 요즘어때, 있어줘) 3곡의 가사로 워드클라우드 생성

readLines("../Data/dynamicduo_lyrics2010.txt", encoding = "UTF-8") %>% 
    str_replace_all("\\W", " ") %>% 
    str_replace_all("\\d", " ") %>% 
    str_squish() %>%      # str_squish : 사이드에 있는 space를 삭제하고, 가운데는 하나만 남김
    extractNoun() -> DD_lyrics2010

DD_lyrics2010 %>% unlist() %>% table() %>% data.frame() %>% 
    rename_with(function(n) {
        c('word','freq')
    }) %>% 
    filter(str_length(word)>=2) %>% 
    arrange(desc(freq))-> df_lyrics2010

dynamicDuo_2010Lyrics = wordcloud2(head(df_lyrics2010, n=200), shape = "circle", size = 0.6)   
saveWidget(dynamicDuo_2010Lyrics,"tmp2.html", selfcontained = F)
webshot("tmp2.html", "dynamicDuo_2010Lyrics.png", delay = 5, vwidth = 480, vheight = 480)

2020년 이후 다이나믹듀오 노래(혼자, SOON, 기다렸다 가) 3곡의 가사로 워드클라우드 생성

readLines("../Data/dynamicduo_lyrics2020.txt", encoding = "UTF-8") %>% 
    str_replace_all("\\W", " ") %>% 
    str_replace_all("\\d", " ") %>% 
    str_squish() %>%      # str_squish : 사이드에 있는 space를 삭제하고, 가운데는 하나만 남김
    extractNoun() -> DD_lyrics2020

DD_lyrics2020 %>% unlist() %>% table() %>% data.frame() %>% 
    rename_with(function(n) {
        c('word','freq')
    }) %>% 
    filter(str_length(word)>=2) %>% 
    arrange(desc(freq))-> df_lyrics2020

dynamicDuo_2020Lyrics = wordcloud2(head(df_lyrics2020, n=200), shape = "circle", size = 0.4)   
saveWidget(dynamicDuo_2020Lyrics,"tmp3.html", selfcontained = F)
webshot("tmp3.html", "dynamicDuo_2020Lyrics.png", delay = 5, vwidth = 480, vheight = 480)

2000년 - 2010년 3곡의 가사의 단어 중 빈도 수 상위 5개

df_lyrics2000 %>% arrange(!freq) %>% head(10) -> most2000
most2000 %>% ggplot(aes(reorder(word, -freq), freq)) + geom_col(fill = brewer.pal(10, "Set3")) + xlab("단어") + ylab("빈도수")

2010년 - 2020년 3곡의 가사의 단어 중 빈도 수 상위 5개

df_lyrics2010 %>% arrange(!freq) %>% head(10) -> most2010
most2010 %>% ggplot(aes(reorder(word, -freq), freq)) + geom_col(fill = brewer.pal(10, "Set3")) + xlab("단어") + ylab("빈도수")

2020년 이후 3곡의 가사의 단어 중 빈도 수 상위 5개

df_lyrics2020 %>% arrange(!freq) %>% head(10) -> most2020
most2020 %>% ggplot(aes(reorder(word, -freq), freq)) + geom_col(fill = brewer.pal(10, "Set3")) + xlab("단어") + ylab("빈도수")