步驟 3:整理(與清理)文字

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 = " ")
R 情感分析

進一步整理

目標:用 tidy 化的租屋評論建立 tidy 格式的極性分數。

library(tidytext)
library(dplyr)

tidy_reviews <- bos_reviews %>% 
    unnest_tokens(word, comments)

    idy_reviews <- tidy_reviews %>% 
    group_by(id) %>%
    mutate(original_word_order = seq_along(word))
R 情感分析

Tidy 文字的極性評分

回想 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 情感分析

一起來練習吧!

R 情感分析

Preparing Video For Download...