我的目标是使用 R 进行基于词典的情感分析!
我有两个字符向量。一个用正面词,一个用负面词。例如
pos <- c("good", "accomplished", "won", "happy")
neg <- c("bad", "loss", "damaged", "sued", "disaster")
我现在有一个包含数千篇新闻文章的语料库,我想知道每篇文章,我的向量 pos 和 neg 在文章中有多少元素。
例如(不确定语料库功能如何在这里工作,但你明白了:我的语料库中有两篇文章)
mycorpus <- Corpus("The CEO is happy that they finally won the case.", "The disaster caused a huge loss.")
我想得到这样的东西:
article 1: 2 element of pos and 0 element of neg
article 2: 0 elements of pos, 2 elements of neg
另一件好事是,如果我能为每篇文章获得以下信息:
(pos 词数 - neg 词数)/(文章总词数)
非常感谢你!!
编辑:
@Victorp:这似乎不起作用
我得到的矩阵看起来不错:
mytdm[1:6,1:10]
Docs
Terms 1 2 3 4 5 6 7 8 9 10
aaron 0 0 0 0 0 1 0 0 0 0
abandon 1 1 0 0 0 0 0 0 0 0
abandoned 0 0 0 3 0 0 0 0 0 0
abbey 0 0 0 0 0 0 0 0 0 0
abbott 0 0 0 0 0 0 0 0 0 0
abbotts 0 0 1 0 0 0 0 0 0 0
但是当我执行您的命令时,每个文档都会得到零!
colSums(mytdm[rownames(mytdm) %in% pos, ])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
这是为什么??