ANEW词典可以用于quanteda中的情感分析吗?

Can the ANEW dictionary be used for sentiment analysis in quanteda?

我正在尝试寻找一种方法来实现英语单词的情感规范(荷兰语),以便使用 Quanteda 进行纵向情感分析。我最终想要的是每年 "mean sentiment" 以显示任何纵向趋势。

在数据集中,所有单词 a 由 64 名编码员在四个类别上以 7 分李克特量表评分,这为每个单词提供了一个平均值。我想做的是采用其中一个维度,并用它来分析情绪随时间的变化。我知道 Quanteda 有实现 LIWC 词典的功能,但如果可能的话,我更喜欢使用开源的 ANEW 数据。

基本上,我需要实施方面的帮助,因为我是编码和 R

的新手

ANEW 文件看起来像这样(在 .csv 中):

WORD/SCORE:癌症:1.01,土豆:3.56,爱情:6.56

还没有,直接,但是... ANEW 与其他词典不同,因为它不使用键:值对格式,而是为每个术语分配一个数字分数。这意味着您不是计算值与键的匹配,而是选择特征,然后使用加权计数对它们进行评分。

这可以通过以下方式在 quanteda 中完成:

  1. 将 ANEW 特征放入字符向量中。

  2. 使用 dfm(yourtext, select = ANEWfeatures) 创建仅具有 ANEW 功能的 dfm。

  3. 将每个计数值乘以每个 ANEW 值的效价,按列循环,以便每个特征计数乘以其 ANEW 值。

  4. 在加权矩阵上使用 rowSums() 以获得文档级效价分数。

或者,

  1. 提交 issue,我们会将此功能添加到 quanteda

另请注意 tidytext 使用 ANEW 进行情绪评分,如果您想将 dfm 转换为他们的对象并使用该方法(这基本上是我的版本上面已经建议了)。

已更新:

事实证明,我已经将此功能内置到您需要的 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