R 中的情感分析
Ted Kwartler
Data Dude
初始目标:使用 polarity() 为文本划分待分析的小节。
pos_comments <- subset(bos_reviews$comments,
bos_reviews$polarity > 0)
neg_comments <- subset(bos_reviews$comments,
bos_reviews$polarity < 0)
pos_terms <- paste(pos_comments, collapse = " ")
neg_terms <- paste(neg_comments, collapse = " ")
目标:用整洁化的租房评论生成整洁格式的极性评分。
library(tidytext)
library(dplyr)
tidy_reviews <- bos_reviews %>%
unnest_tokens(word, comments)
tidy_reviews <- tidy_reviews %>%
group_by(id) %>%
mutate(original_word_order = seq_along(word))
回顾:sentiments 中的 "bing" 词典将词标注为正面或负面。
library(tidytext)
library(tidyr)
library(dplyr)
bing <- sentiments %>%
filter(lexicon == "bing")
pos_neg <- tidy_reviews %>%
inner_join(bing) %>%
count(sentiment) %>%
pivot_wider(names_from = sentiment, values_from = n, values_fill = 0) %>%
mutate(polarity = positive - negative)
R 中的情感分析