情感分析 Lexicoder Dictionary Quanteda

Sentiment Analysis Lexicoder Dictionary Quanteda

我正在尝试在 Quanteda 中进行情绪分析,但遇到了使用 2015 Lexicoder 情绪词典无法解决的错误。词典有四个关键词:negative, positive, negative positive(肯定词前面有否定词(用来表达消极情绪)和negative negative(否定词前面有否定词,用来表达积极情绪)。

我在使用词典时无法激活最后两个类别

这是我正在使用的脚本

LexisNexisTools 包将其转换为 quanteda 语料库。当我尝试错误时,我没有得到任何 neg_posneg_negative 命中,所以我添加了例句“This aggressive policy will not win friends” - 其中有一个 neg_positive bigram ('will not') - 从 reference on the quanteda page 到第一个文档的第一行。这是在第一个 dfm 中注册的,可以在 toks_dict 令牌列表中看到。但是,语料库中有更多完全相同的二元组(不会)的实例未被计算在内。此外,语料库中还有其他 neg_posneg_neg 短语根本没有注册。

我完全不确定这是如何解决的。奇怪的是,在第三个 dfm dfm_dict 中,最初的 'will not' 根本没有注册为 neg_positive。类别 negativepositive 的总计数没有改变,因此这不是在其他地方计算缺失值的情况。我真的很想念我做错了什么 - 任何帮助将不胜感激!



rm(list=ls())

library(quanteda)
library(quanteda.corpora)
library(readtext)
library(LexisNexisTools)
library(tidyverse)
library(RColorBrewer)

LNToutput <-lnt_read("word_labour.docx")

corp <- lnt_convert(LNToutput, to = "quanteda")

#uses the package lexisnexistools to create the corpus from the format needed


dfm <- dfm(corp, dictionary = data_dictionary_LSD2015)
dfm

toks_dict <- tokens_lookup(tokens(corp), dictionary = data_dictionary_LSD2015, exclusive= FALSE )
toks_dict

dfm_dict <- dfm(toks_dict, dictionary = data_dictionary_LSD2015, exclusive = FALSE )
dfm_dict


https://www.dropbox.com/s/qdwetdn8bt9fdrd/word_labour.DOCX?dl=0

这是构成语料库原始文本的 word 文档的 link。

对我来说很好用。通过 运行 kwic() 在复合字典键上,您可以看到匹配发生的位置。

library("quanteda", warn.conflicts = FALSE)
## Package version: 2.1.0
## Parallel computing: 2 of 8 threads used.
## See https://quanteda.io for tutorials and examples.

corp <- readtext::readtext("https://www.dropbox.com/s/qdwetdn8bt9fdrd/word_labour.docx?dl=1") %>%
  corpus()

toks <- tokens(corp)

kwic(toks, pattern = data_dictionary_LSD2015["neg_positive"])
##                                                                                
##        [word_labour.docx, 82:83] Body This aggressive policy will |  not win  |
##    [word_labour.docx, 8468:8469]                manifesto as" as" | not worth |
##    [word_labour.docx, 9681:9682]       more high street services. | Not clear |
##    [word_labour.docx, 9778:9779]     will get one-to-one tuition. | Not clear |
##    [word_labour.docx, 9841:9842]      children free school meals. | Not clear |
##  [word_labour.docx, 10338:10339]      western Balkans and Turkey. | Not clear |
##  [word_labour.docx, 13463:13464]              in January. What is | not clear |
##                                   
##  friends. Ed Miliband has         
##  the paper it is written          
##  - Labour has criticised the      
##  - then shadow education secretary
##  - Labour appeared to back        
##  - this is not a                  
##  is if it allows a
kwic(toks, pattern = data_dictionary_LSD2015["neg_negative"])
##                                                                   
##  [word_labour.docx, 10772:10773] over again. It is | not unusual |
##                         
##  for voters to trust the

dfm 反映了这一点:

tokens_lookup(toks, dictionary = data_dictionary_LSD2015) %>%
  dfm()
## Document-feature matrix of: 1 document, 4 features (0.0% sparse).
##                   features
## docs               negative positive neg_positive neg_negative
##   word_labour.docx      512      687            7            1

ps 我使用了 readtext 包来避免你正在做的所有其他事情,这对这个问题来说不是必需的。