在所有采用 pattern 参数的 quanteda 函数中,有效的模式类型是字符向量、列表和字典。因此,评估每个字典类别(我们也称为字典key)中每个顶级特征的最佳方法是在该字典上进行选择,然后使用topfeatures()。
以下是使用内置 data_corpus_irishbudget2010 对象的方法,例如,使用 Lexicoder 情感词典。
library("quanteda")
## Package version: 1.4.3
# tokenize and select just the dictionary value matches
toks <- tokens(data_corpus_irishbudget2010) %>%
tokens_select(pattern = data_dictionary_LSD2015)
lapply(toks[1:5], head)
## $`Lenihan, Brian (FF)`
## [1] "severe" "distress" "difficulties" "recovery"
## [5] "benefit" "understanding"
##
## $`Bruton, Richard (FG)`
## [1] "failed" "warnings" "sucking" "losses" "debt" "hurt"
##
## $`Burton, Joan (LAB)`
## [1] "remarkable" "consensus" "Ireland" "opposition" "knife"
## [6] "dispute"
##
## $`Morgan, Arthur (SF)`
## [1] "worst" "worst" "well" "corrupt" "golden" "protected"
##
## $`Cowen, Brian (FF)`
## [1] "challenge" "succeeding" "challenge" "oppose"
## [5] "responsibility" "support"
要探索肯定条目的顶部匹配项,我们可以通过将字典设置为 Positive 键来进一步选择它们。
# top positive matches
tokens_select(toks, pattern = data_dictionary_LSD2015["positive"]) %>%
dfm() %>%
topfeatures()
## benefit support recovery fair create confidence
## 68 52 44 41 39 37
## provide well credit help
## 36 33 31 29
对于负数:
# top negative matches
tokens_select(toks, pattern = data_dictionary_LSD2015[["negative"]]) %>%
dfm() %>%
topfeatures()
## ireland benefit not support crisis recovery
## 79 68 52 52 47 44
## fair create deficit confidence
## 41 39 38 37
为什么“Ireland”是否定匹配?因为 LSD2015 包含 ir* 作为否定词,旨在匹配 ire 和 ireful 但使用默认的不区分大小写匹配,也匹配 Ireland(本示例语料库中经常使用的术语)。这是一个“误报”匹配的示例,在使用通配符或使用多义词和同形异义词率非常高的语言(例如英语)时,在字典中总是存在风险。