我對一組推特進行了情感分析,我現在想知道如何將詞組添加到正面和負面詞典。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