R中的Score Sentiment函数,return始终为0

Score Sentiment function in R, return always 0

我有一个(可能)愚蠢的问题 score.sentiment 我正在尝试将此函数与 3 个默认短语一起使用,问题是函数 return 得分为 0.0.0,但它应该 return 2.-5.4 我不明白这个问题,因为 RGui 没有给我错误,我正在学习教程!

我已经用

下载了负面词和正面词的列表
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

如果有用的话,这里有我安装的库和包。

install.packages('twitteR', dependencies=T);
install.packages('ggplot2', dependencies=T);
install.packages('XML', dependencies=T);
install.packages('plyr', dependencies=T);
install.packages('doBy', dependencies=T);
install.packages('tm', dependencies=T);
install.packages('RJSONIO', dependencies=T)
install.packages('RWeka')
install.packages('base64enc')

library(twitteR);
library(ggplot2);
library(XML);
library(plyr);
library(doBy);
library(RJSONIO)
library(tm)
library(RWeka)

谢谢指教

适合我

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.