NRC 单词-情感关联词典的构建方式使其非常适合社交媒体数据,因为它已经存在,因此我建议在对其进行更改以进行分析之前taking a look at the details of where it comes from。但是,如果您决定出于您的目的,您需要将单词添加到这样的情感词典中,第一步是通过bind_rows() 将单词逐行添加到数据集中。假设您认为“darcy”是一个肯定词,而“wickham”是一个否定词。
library(tidyverse)
library(tidytext)
nrc_lexicon <- get_sentiments("nrc")
custom_lexicon <- nrc_lexicon %>%
bind_rows(tribble(~word, ~sentiment,
"darcy", "positive",
"wickham", "negative"))
现在,当您想要实施情绪分析时,您可以以相同的方式处理这些数据帧中的任何一个。如果你有文本数据(比如傲慢与偏见的文本),你可以先用unnest_tokens()整理一下,然后用inner_join()进行情感分析。
tidy_PandP <- tibble(text = janeaustenr::prideprejudice) %>%
unnest_tokens(word, text)
tidy_PandP %>%
inner_join(nrc_lexicon)
#> Joining, by = "word"
#> # A tibble: 29,651 x 2
#> word sentiment
#> <chr> <chr>
#> 1 pride joy
#> 2 pride positive
#> 3 prejudice anger
#> 4 prejudice negative
#> 5 truth positive
#> 6 truth trust
#> 7 possession anger
#> 8 possession disgust
#> 9 possession fear
#> 10 possession negative
#> # … with 29,641 more rows
tidy_PandP %>%
inner_join(custom_lexicon)
#> Joining, by = "word"
#> # A tibble: 30,186 x 2
#> word sentiment
#> <chr> <chr>
#> 1 pride joy
#> 2 pride positive
#> 3 prejudice anger
#> 4 prejudice negative
#> 5 truth positive
#> 6 truth trust
#> 7 possession anger
#> 8 possession disgust
#> 9 possession fear
#> 10 possession negative
#> # … with 30,176 more rows
由reprex package (v0.3.0) 于 2019 年 8 月 3 日创建
请注意,您可以以相同的方式对任一词典(原始词典或我们添加单词的词典)实施情感分析。
我确实想指出,NRC 词典的许可允许它免费用于研究目的,但对于任何商业用途,您必须联系 NRC 研究人员并支付商业许可费用。