在R中评分情绪函数,总是返回0

时间:2016-02-05 11:23:57

标签: r sentiment-analysis

我对score.sentiment有一个(可能)愚蠢的问题 我尝试使用这个函数有3个默认短语,问题是函数返回得分0.0.0,但它应该返回2.-5.4 我不明白这个问题,因为RGui不会给我错误,而且我会按照教程进行操作!

我已经用

下载了负面和正面单词的列表
With sht.Range(Cells(1, 1), Range("A1").SpecialCells(xlCellTypeLastCell))

我有分数功能

hu.liu.pos = scan('https://www.dropbox.com/sh/3xctszdxx4n00xq/AAA_Go_Y3kJxQACFaVBem__ea/positive-words.txt?dl=0', what='character', comment.char=';');
hu.liu.neg = scan('https://www.dropbox.com/sh/3xctszdxx4n00xq/AABTGWHitlRZcddq1pPXOSqca/negative-words.txt?dl=0', what='character', comment.char=';');

这是我的示例代码

score.sentiment = function(sentences, 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);
  }

如果可以使用,那么我已经安装了库和软件包。

    sample=c("You're awesome and I love you","I hate and hate and hate. So angry. Die!","Impressed and amazed: you are peerless in your achievement of unparalleled mediocrity.")
result=score.sentiment(sample,pos.words,neg.words)
class(result)
result$score
result

谢谢你的建议

1 个答案:

答案 0 :(得分:2)

为我工作

hu.liu.pos = readLines('https://www.dropbox.com/sh/3xctszdxx4n00xq/AAA_Go_Y3kJxQACFaVBem__ea/positive-words.txt?dl=1');
hu.liu.neg = readLines('https://www.dropbox.com/sh/3xctszdxx4n00xq/AABTGWHitlRZcddq1pPXOSqca/negative-words.txt?dl=1');

score.sentiment = function(sentences, 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);
  }

sample=c("You're awesome and I love you","I hate and hate and hate. So angry. Die!","Impressed and amazed: you are peerless in your achievement of unparalleled mediocrity.")
result=score.sentiment(sample,hu.liu.pos,hu.liu.neg)
result
#   score                                                                                   text
# 1     2                                                          You're awesome and I love you
# 2    -5                                               I hate and hate and hate. So angry. Die!
# 3     4 Impressed and amazed: you are peerless in your achievement of unparalleled mediocrity.