Score Sentiment function in R, return always 0

[亡魂溺海] 提交于 2020-01-03 00:45:52

问题


I have a (probably) stupid problem with score.sentiment I'm trying to use this function with 3 default phrases, the problem is that the function return score 0.0.0, but it should return 2.-5.4 I don't understand the problem because RGui don't give me errors and I'm following a tutorial!

I've dowloaded lists for negative and positive words with

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=';');

And I have my score function

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);
  }

And this is my example code

    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

If can be usefull, there are the libraries and packages that I've installed.

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)

Thank you in advice


回答1:


Works for me

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.


来源:https://stackoverflow.com/questions/35222946/score-sentiment-function-in-r-return-always-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!