library(learnr) library(tidyverse) library(textdata) library(tidytext) library(biztextp) library(wordcloud) knitr::opts_chunk$set(echo = FALSE) # AFINN 예제를 위한 데이터 tc <- commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text) %>% anti_join(stop_words) sent_mat <- commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text) %>% anti_join(stop_words) %>% inner_join(sentiments) %>% count(para_number, sentiment) %>% pivot_wider(names_from = sentiment, values_from = n, values_fill = list(n = 0)) %>% mutate(sent = positive - negative) %>% select(para_number, sent)
다음은 tidytext
패키지가 제공하는 sentiments
의 감정 어휘 사전이다.
sentiments
sentiments
감정 어휘 사전의 감정(sentiment
)의 빈도수를 출력해 보시오.sentiments %>% count(...)
sentiments %>% count(sentiment)
다음은 textdata
패키지가 제공하는 nrc
감정 어휘 사전이다.
get_sentiments("nrc")
get_sentiments
함수를 이용하여 nrc 감정 어휘 사전을 불러와서, nrc 사전의 감정(sentiment
)의 빈도수를 출력해 보시오. 단, 빈도가 많은 감정이 위에 나타나도록 하시오.get_sentiments(...) %>% count(...)
get_sentiments("nrc") %>% count(sentiment, sort=TRUE)
다음은 textdata
패키지가 제공하는 afinn
감정 어휘 사전이다.
get_sentiments("afinn")
get_sentiments
함수를 이용하여 afinn 감정 어휘 사전을 불러와서, afinn 사전의 단어의 긍부정 값(value
)에 대한 막대 그래프를 그려보시오. get_sentiments(...) %>% ggplot() + geom_bar(aes(...))
get_sentiments("afinn") %>% ggplot() + geom_bar(aes(value))
biztextp
패키지의 commencement
데이터를 이용하여 다음 문제를 풀어보시오. commencement
데이터는 Steve Jobs가 스탠포드 대학의 졸업식에서 한 연설문이다.
author
는 저자text
문단 단위로 텍스를 가지고 있는 문자열.텍스트 열이 보이지 않으면 상단에 표시된 오른쪽 화살표를 클릭해 보시오.
commencement
comencement
데이터에 문단 단위로 번호를 부여하여 para_number
라는 열로 추가하시오.
결과에서 새롭게 부여한 열이 보이지 않으면 상단에 표시된 오른쪽 화살표를 클릭해 보시오. commencement %>% mutate(... = ...)
commencement %>% mutate(para_number = row_number())
page_number
가 부여된 결과에서 다음 명령을 수정하여 text
열을 단어로 토큰화하시오. 토큰화된 결과는 word
라는 열 이름을 부여하시오. commencement %>% mutate(... = ...) %>% unnest_tokens(output = ..., input = ...)
commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text)
stop_words
불용어 사전을 이용하여 불용어를 제거하시오.commencement %>% mutate(... = ...) %>% unnest_tokens(output = ..., input = ...) %>% anti_join(...)
commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text) %>% anti_join(stop_words)
sentiments
감정 어휘 사전을 이용하여 감정 어휘를 추출하시오.commencement %>% mutate(... = ...) %>% unnest_tokens(output = ..., input = ...) %>% anti_join(...) %>% inner_join(...)
commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text) %>% anti_join(stop_words) %>% inner_join(sentiments)
commencement %>% mutate(... = ...) %>% unnest_tokens(output = ..., input = ...) %>% anti_join(...) %>% inner_join(...) %>% count(..., ...)
commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text) %>% anti_join(stop_words) %>% inner_join(sentiments) %>% count(para_number, sentiment)
sentiment
열의 내용이 열의 이름으로, n
이 각 열의 값으로 들어가게 하시오. 아울러 데이터가 없는 셀의 값은 0이 되도록 하시오.commencement %>% mutate(... = ...) %>% unnest_tokens(output = ..., input = ...) %>% anti_join(...) %>% inner_join(...) %>% count(..., ...) %>% pivot_wider(names_from = ..., values_from = ..., values_fill = list(... = ...))
commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text) %>% anti_join(stop_words) %>% inner_join(sentiments) %>% count(para_number, sentiment) %>% pivot_wider(names_from = sentiment, values_from = n, values_fill = list(n = 0))
positive
열에서 negative
열을 뺀 결과를 sent
열로 덧붙이시오. commencement %>% mutate(... = ...) %>% unnest_tokens(output = ..., input = ...) %>% anti_join(...) %>% inner_join(...) %>% count(..., ...) %>% pivot_wider(names_from = ..., values_from = ..., values_fill = list(... = ...)) %>% mutate(... = ...)
commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text) %>% anti_join(stop_words) %>% inner_join(sentiments) %>% count(para_number, sentiment) %>% pivot_wider(names_from = sentiment, values_from = n, values_fill = list(n = 0)) %>% mutate(sent = positive - negative)
para_number
, 세로축은 sent
로 하여 막대 그래프를 그리시오. commencement %>% mutate(... = ...) %>% unnest_tokens(output = ..., input = ...) %>% anti_join(...) %>% inner_join(...) %>% count(..., ...) %>% pivot_wider(names_from = ..., values_from = ..., values_fill = list(... = ...)) %>% mutate(... = ...) %>% ggplot() + geom_col(aes(..., ...))
commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text) %>% anti_join(stop_words) %>% inner_join(sentiments) %>% count(para_number, sentiment) %>% pivot_wider(names_from = sentiment, values_from = n, values_fill = list(n = 0)) %>% mutate(sent = positive - negative) %>% ggplot() + geom_col(aes(para_number, sent))
sent
)가 0보다 크면 "긍정", 0보다 작거나 같으면 "부정"으로 나누어 막대 그래프의 채우기 색이 달리 나타나도록 해 보시오.commencement %>% mutate(... = ...) %>% unnest_tokens(output = ..., input = ...) %>% anti_join(...) %>% inner_join(...) %>% count(..., ...) %>% pivot_wider(names_from = ..., values_from = ..., values_fill = list(... = ...)) %>% mutate(... = ...) %>% ggplot() + geom_col(aes(..., ..., fill = ifelse(... > 0 , ..., ...)))
commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text) %>% anti_join(stop_words) %>% inner_join(sentiments) %>% count(para_number, sentiment) %>% pivot_wider(names_from = sentiment, values_from = n, values_fill = list(n = 0)) %>% mutate(sent = positive - negative) %>% ggplot() + geom_col(aes(para_number, sent, fill = ifelse(sent > 0, "긍정", "부정")))
commencement %>% mutate(... = ...) %>% unnest_tokens(output = ..., input = ...) %>% anti_join(...) %>% inner_join(...) %>% count(..., ...) %>% pivot_wider(names_from = ..., values_from = ..., values_fill = list(... = ...)) %>% mutate(... = ...) %>% ggplot() + geom_col(aes(..., ..., fill = ifelse(... > 0 , ..., ...))) + labs(... = ..., ... = ..., ... = ...)
commencement %>% mutate(para_number = row_number()) %>% unnest_tokens(output = word, input = text) %>% anti_join(stop_words) %>% inner_join(sentiments) %>% count(para_number, sentiment) %>% pivot_wider(names_from = sentiment, values_from = n, values_fill = list(n = 0)) %>% mutate(sent = positive - negative) %>% ggplot() + geom_col(aes(para_number, sent, fill = ifelse(sent > 0, "긍정", "부정"))) + labs(fill = "감성", x = "문단 번호", y = "감성 점수")
다음은 앞 단게에서 수행한 commencement
데이터를 토큰화한 결과이다. 이 데이터는 현재 tc
라는 이름의 변수에 저장되어 있다.
tc
tc
에서 afinn
감정 어휘 사전을 이용하여 감정 어휘를 추출하시오.tc %>% inner_join(...)
tc %>% inner_join(get_sentiments("afinn"))
tc %>% inner_join(...) %>% count(..., wt = ...)
tc %>% inner_join(get_sentiments("afinn")) %>% count(para_number, wt = value)
para_number
, 세로축은 n
로 하여 막대 그래프를 그리시오. tc %>% inner_join(...) %>% count(..., wt = ...) %>% ggplot() + geom_col(aes(..., ...))
tc %>% inner_join(get_sentiments("afinn")) %>% count(para_number, wt = value) %>% ggplot() + geom_col(aes(para_number, n))
n
)이 0보다 크면 "긍정", 0보다 작거나 같으면 "부정"으로 나누어 막대 그래프의 채우기 색이 달리 나타나도록 해 보시오. 아울러 채우기 색의 레이블을 "감성"으로, x-축의 레이블을 "문단 번호", y-축의 레이블을 "감성 점수"로 바꾸어 보시오.tc %>% inner_join(...) %>% count(..., wt = ...) %>% ggplot() + geom_col(aes(..., ..., fill = ifelse(... > 0 , ..., ...))) + labs(... = ..., ... = ..., ... = ...)
tc %>% inner_join(get_sentiments("afinn")) %>% count(para_number, wt = value) %>% ggplot() + geom_col(aes(para_number, n, fill = ifelse(n > 0, "긍정", "부정"))) + labs(fill = "감성", x = "문단 번호", y = "감성 점수")
다음은 앞 단계에서 sentiments 감정 어휘 사전을 사용하여 commencement의 문단별 감성을 분석한 결과이다. 이 데이터는 sent_mat
데이터에 저장되어 있다.
sent_mat
sent_mat
의 결과를 문답 번호(para_number
)로 내부 조인하시오.tc %>% inner_join(...) %>% count(..., wt = ...) %>% inner_join(...)
tc %>% inner_join(get_sentiments("afinn")) %>% count(para_number, wt = value) %>% inner_join(sent_mat)
tc %>% inner_join(...) %>% count(..., wt = ...) %>% inner_join(...) %>% ggplot() + geom_point(aes(..., ...))
tc %>% inner_join(get_sentiments("afinn")) %>% count(para_number, wt = value) %>% inner_join(sent_mat) %>% ggplot() + geom_point(aes(n, sent))
다음은 앞 단게에서 수행한 commencement
데이터를 토큰화한 결과이다. 이 데이터는 현재 tc
라는 이름의 변수에 저장되어 있다.
tc
tc
에서 sentiments
감정 어휘 사전을 이용하여 감정 어휘를 추출하시오.tc %>% inner_join(...)
tc %>% inner_join(sentiments)
tc %>% inner_join(...) %>% count(..., ...)
tc %>% inner_join(sentiments) %>% count(word, sentiment)
tc %>% inner_join(...) %>% count(..., ...) %>% filter(... )
tc %>% inner_join(sentiments) %>% count(word, sentiment) %>% filter(n >= 2)
tc %>% inner_join(...) %>% count(..., ..., sort = ...) %>% filter(... ) %>% ggplot() + geom_col(aes(..., ...)) + coord_....()
tc %>% inner_join(sentiments) %>% count(word, sentiment, sort = TRUE) %>% filter(n >= 2) %>% ggplot() + geom_col(aes(word, n)) + coord_flip()
tc %>% inner_join(...) %>% count(..., ..., sort = ...) %>% filter(... ) %>% mutate(word = reorder(..., ...)) %>% ggplot() + geom_col(aes(..., ...)) + coord_....()
tc %>% inner_join(sentiments) %>% count(word, sentiment, sort = TRUE) %>% filter(n >= 2) %>% mutate(word = reorder(word, n)) %>% ggplot() + geom_col(aes(word, n)) + coord_flip()
tc %>% inner_join(...) %>% count(..., ..., sort = ...) %>% filter(... ) %>% mutate(word = reorder(..., ...)) %>% ggplot() + geom_col(aes(..., ...)) + coord_....() + facet_wrap(..., scales = ...)
tc %>% inner_join(sentiments) %>% count(word, sentiment, sort = TRUE) %>% filter(n >= 2) %>% mutate(word = reorder(word, n)) %>% ggplot() + geom_col(aes(word, n)) + coord_flip() + facet_wrap(~ sentiment, scales = "free_y")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.