情感分析中的规范化

时间:2017-12-09 11:31:02

标签: r normalization sentiment-analysis

我'我实际上正在进行情绪分析。为了计算一组评论的情绪,我计算了每个句子的正面,负面词的数量。我有一个正面和负面的单词列表(AFINN),得分范围从-5到+5。 问题是该函数只给出了每个评论的正面和负面词之间的总和,而不是每个评论的一般情绪。 为了计算一般情绪:

1)相对于每个评论的最大值进行标准化(我的每个单词的得分从-5到+5,我必须用代数加上得分并除以情感词的数量)

2)所有评论的平均值

function(sentences, pos.words, neg.words, .progress='none')
{

    scores = laply(sentences, function(sentence, pos.words, neg.words) {
sentence = tolower(sentence)
sentence = gsub('[[:punct:]]', '', sentence)

sentence = gsub('[[:cntrl:]]', '', sentence)

sentence = gsub('\\d+', '', sentence)
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.words)
neg.matches = match(words, neg.words)

# match() returns the position of the matched term or NA
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)
}

0 个答案:

没有答案