还没有,直接,但是... ANEW 与其他词典不同,因为它不使用键:值对格式,而是为每个术语分配一个数字分数。这意味着您不是对值与键的匹配进行计数,而是选择特征,然后使用加权计数对它们进行评分。
这可以在quanteda by:
将新的特征放入字符向量中。
Use dfm(yourtext, select = ANEWfeatures)
创建仅具有 ANEW 功能的 dfm。
将每个计数值乘以每个 ANEW 值的化合价,逐列回收,以便每个特征计数乘以其 ANEW 值。
Use rowSums()
在加权矩阵上获得文档级价分数。
或者,
- File an issue https://github.com/kbenoit/quanteda/issues我们将把这个功能添加到quanteda.
另请注意tidytext如果您想将 dfm 转换为他们的对象并使用该方法(这基本上是我上面建议的版本),则使用 ANEW 进行情感评分。
Updated:
事实证明我已经将该功能内置到了quanteda你需要的,只是没有意识到!
这会起作用。首先,加载 ANEW 词典。 (您必须自己提供 ANEW 文件。)
# read in the ANEW data
df_anew <- read.delim("ANEW2010All.txt", stringsAsFactors = FALSE)
# construct a vector of weights with the term as the name
vector_anew <- df_anew$ValMn
names(vector_anew) <- df_anew$Word
现在我们有了一个命名的权重向量,我们可以使用它来应用它dfm_weight()
。下面,我首先通过相对频率对 dfm 进行标准化,以便文档总分不依赖于标记中的文档长度。如果您不想这样做,只需删除下面指示的行即可。
library("quanteda")
dfm_anew <- dfm(data_corpus_inaugural, select = df_anew$Word)
# weight by the ANEW weights
dfm_anew_weighted <- dfm_anew %>%
dfm_weight(scheme = "prop") %>% # remove if you don't want normalized scores
dfm_weight(weights = vector_anew)
## Warning message:
## dfm_weight(): ignoring 1,427 unmatched weight features
tail(dfm_anew_weighted)[, c("life", "day", "time")]
## Document-feature matrix of: 6 documents, 3 features (5.56% sparse).
## 6 x 3 sparse Matrix of class "dfm"
## features
## docs life day time
## 1997-Clinton 0.07393220 0.06772881 0.21600000
## 2001-Bush 0.10004587 0.06110092 0.09743119
## 2005-Bush 0.09380645 0.12890323 0.11990323
## 2009-Obama 0.06669725 0.10183486 0.09743119
## 2013-Obama 0.08047970 0 0.19594096
## 2017-Trump 0.06826291 0.12507042 0.04985915
# total scores
tail(rowSums(dfm_anew_weighted))
## 1997-Clinton 2001-Bush 2005-Bush 2009-Obama 2013-Obama 2017-Trump
## 5.942169 6.071918 6.300318 5.827410 6.050216 6.223944