使用R包“情感”的情绪分析错误

时间:2017-06-05 11:45:44

标签: r sentiment-analysis

我正在使用R Package“Sentiment”进行感伤分析

neg_words = scan(“C:/ Users / kothasan / Desktop / Sentiment Analysis / neg.words.txt”,what ='character',comment.char =';')

pos_words = scan(“C:/ Users / kothasan / Desktop / Sentiment Analysis / pos.words.txt”,what ='character',comment.char =';')

score.sentiment = function(句子,pos.words,neg.words,.progress ='none')

{

require(plyr);
require(stringr);
scores = laply(sentences, function(sentence, pos.words, neg.words) {
    sentence = gsub('[^A-z ]','', sentence)
    sentence = tolower(sentence);
    word.list = str_split(sentence, '\\s+');
    words = unlist(word.list);
    pos.matches = match(words, pos.words);
    neg.matches = match(words, neg.words);
    pos.matches = !is.na(pos.matches);
    neg.matches = !is.na(neg.matches);
    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);

}

示例数据由两行组成: “text”是列名。

                          text

1。我很满意你的服务

  1. 未解决的问题
  2. 使用的功能:

    analysis = score.sentiment(sample,pos_words,neg_words)

    当我运行上述功能时,我得到了以下警告,输出分数为0,0这是错误的:

    警告讯息: 在data.frame(得分=得分,文本=句子):   行名称是从一个短变量中找到的并且已被丢弃

    输出:

    得分文字

    1. 0我对您的服务感到满意

    2. 0未解决的问题

    3. 当我只使用一行作为输入时,我得到了正确的分数。

      有人可以帮我解决这个问题吗?

      谢谢,

      和Sandeep

1 个答案:

答案 0 :(得分:1)

你能检查你要执行的文本列senti分析是一个向量。 示例:如果数据集有2列:数字和文本。您想对文本执行情绪分析。因此,在运行代码之前,请将其设置为vector = dataset $ text。现在使用代码中的句子。