在R中添加新词到Bing情感Lexicon

问题描述 投票:3回答:1

使用R Studio分析一些注释。我现在正在使用来自tidytext包的Bing Sentiment词典。

我还有一些其他的话要添加到Bing(运行时或离线)。例如,我可以添加积极性或消极性或任何其他情绪。我怎样才能做到这一点?

r rstudio tidytext
1个回答
1
投票

sentiment是一个tibble,所以添加新词只是一个rbind

additional_sentiment <- tibble(word=c("verygood","verybad"),
                               sentiment=c("positive","negative"))

new_sentiment <- get_sentiments("bing")%>%
                    rbind(additional_sentiment)

tail(new_sentiment)
# A tibble: 6 x 2
      word sentiment
     <chr>     <chr>
1   zenith  positive
2     zest  positive
3    zippy  positive
4   zombie  negative
5 verygood  positive
6  verybad  negative

joined <- austen_books() %>%
  unnest_tokens(word, text) %>%
  left_join(new_sentiment)

head(joined[!is.na(joined$sentiment),])
# A tibble: 6 x 3
                 book        word sentiment
               <fctr>       <chr>     <chr>
1 Sense & Sensibility respectable  positive
2 Sense & Sensibility        good  positive
3 Sense & Sensibility    advanced  positive
4 Sense & Sensibility       death  negative
5 Sense & Sensibility       great  positive
6 Sense & Sensibility        loss  negative
© www.soinside.com 2019 - 2024. All rights reserved.