我对一组推特进行了情感分析,我现在想知道如何将词组添加到正面和负面词典。R词典在词典中的情感分析
我已经阅读过我想测试的短语文件,但是在运行情感分析时,它并没有给我一个结果。
在通过情感算法进行阅读时,我可以看到它将单词与词典匹配,但有没有方法可以扫描单词以及短语?
下面是代码:
score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
require(plyr)
require(stringr)
# we got a vector of sentences. plyr will handle a list
# or a vector as an "l" for us
# we want a simple array ("a") of scores back, so we use
# "l" + "a" + "ply" = "laply":
scores = laply(sentences, function(sentence, pos.words, neg.words) {
# clean up sentences with R's regex-driven global substitute, gsub():
sentence = gsub('[[:punct:]]', '', sentence)
sentence = gsub('[[:cntrl:]]', '', sentence)
sentence = gsub('\\d+', '', sentence)
# and convert to lower case:
sentence = tolower(sentence)
# split into words. str_split is in the stringr package
word.list = str_split(sentence, '\\s+')
# sometimes a list() is one level of hierarchy too much
words = unlist(word.list)
# compare our words to the dictionaries of positive & negative terms
pos.matches = match(words, pos)
neg.matches = match(words, neg)
# match() returns the position of the matched term or NA
# we just want a TRUE/FALSE:
pos.matches = !is.na(pos.matches)
neg.matches = !is.na(neg.matches)
# and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
score = sum(pos.matches) - sum(neg.matches)
return(score)
}, pos.words, neg.words, .progress=.progress)
scores.df = data.frame(score=scores, text=sentences)
return(scores.df)
}
analysis=score.sentiment(Tweets, pos, neg)
table(analysis$score)
这是结果我得到:
0
20
,而我的标准表后,该功能提供 例如
-2 -1 0 1 2
1 2 3 4 5
例如。
有没有人有关于如何在短语上运行此任何想法? 注意:TWEETS文件是一个句子文件。
不知道,但我想你可能意味着lapply而不是laply? – dd3
@ dd3它是从plyr包裹中重叠的,而不是从基地的lapply。 – WhiteViking
我是R的初学者。你在这里做什么“进展”?好像你没有在你的功能中使用它? – alwaysaskingquestions