【问题标题】:Score Sentiment function in R, return always 0R中的评分情感函数,总是返回0
【发布时间】:2016-02-05 11:23:56
【问题描述】:

我对 score.sentiment 有一个(可能)愚蠢的问题 我正在尝试将此函数与 3 个默认短语一起使用,问题是该函数返回分数 0.0.0,但它应该返回 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)

谢谢你的建议

【问题讨论】:

    标签: r sentiment-analysis


    【解决方案1】:

    为我工作

    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.
    

    【讨论】:

    猜你喜欢
    • 2016-07-21
    • 1970-01-01
    • 2011-08-27
    • 2017-08-10
    • 2018-09-26
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多