【问题标题】:Sentiment Analysis using R (code not working correctly)使用 R 进行情绪分析(代码无法正常工作)
【发布时间】:2016-06-21 06:38:34
【问题描述】:

我正在尝试使用基于词典的评分方法对文本进行一些情感分析。 看了堆栈溢出帖子后,我直接从http://analyzecore.com/2014/04/28/twitter-sentiment-analysis/借用了我的代码:R sentiment analysis with phrases in dictionaries

以下是关于我的数据集的一些总结:

> summary(data$text)
   Length     Class      Mode 
       30 character character 
> str(data$text)
 chr [1:30] "Hey everybody, are you guys free on Sunday for a game play + dinner afterwards? I'll reserve a"| __truncated__ ...

以及我正在使用的代码:

require(plyr)  
require(stringr)
require(data.table)
score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
  scores = laply(sentences, function(sentence, pos.words, neg.words) {

    sentence = gsub('[[:punct:]]', '', sentence)
    sentence = gsub('[[:cntrl:]]', '', sentence)
    sentence = gsub('\\d+', '', sentence)
    # and convert to lower case:
    sentence = tolower(sentence)

    # split into words. str_split is in the stringr package
    word.list = str_split(sentence, '\\s+')
    # sometimes a list() is one level of hierarchy too much
    words = unlist(word.list)

    # compare our words to the dictionaries of positive & negative terms
    pos.matches = match(words, pos.words)
    neg.matches = match(words, neg.words)

    pos.matches = !is.na(pos.matches)
    neg.matches = !is.na(neg.matches)

    # and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
    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)
}

我正在使用刘冰的意见词典,我将它们加载为:

pos_BL = read.table(file = 'positive-words.txt', stringsAsFactors = F)
neg_BL = read.table(file = 'negative-words.txt', stringsAsFactors = F)

这是我用来通过评分函数运行数据和字典的代码:

score_result = score.sentiment(sentences = data$text, 
                               pos.words = pos_BL, 
                               neg.words = neg_BL, 
                               .progress= 'text')

但是,无论我做什么,我的 30 根琴弦都只能得到 0 分。 (输出总结见下表):

> table(score_result$score)
 0 
30 

我不知道在哪里修复(在此处发布此问题之前,我确实在自己的代码中发现了许多错误)。非常感谢任何帮助!

【问题讨论】:

  • 检查 qdap 包中的极性!
  • @ChirayuChamoli 你能提供更多细节吗?就像它是什么,我在哪里可以找到一个简单的教程? :)
  • 函数极性基本上与您正在做的事情相同,但它是 sa 的更好实现。查看您将看到的源代码。查看 ?polarity 的语法。

标签: r sentiment-analysis lexicon


【解决方案1】:

一个例子:

list=list(a='This place is awesome', b='I failed in the exam')
lapply(list, polarity)

【讨论】:

  • 你好 chirayu,我尝试加载 qdap 包,但无论我尝试多少次,它总是说“错误:没有名为 'qdap' 的包”,即使它已经加载了所有其他必要的依赖库。可能的问题是什么?你知道吗?谢谢!
  • 可以从here手动加载包
  • 你好 chirayu。我是这里的初学者,所以请耐心等待我的问题...我点击了您共享的链接,但我仍然不确定如何加载阅读该页面的包:(
  • 嘿,在链接上下载 Windows 二进制文件。安装 r-release 并将其放在所有软件包所在的库中。然后使用库(qdap)加载它。但这不会安装依赖项,因为您也必须手动安装它们或使用命令。
  • 我终于让它工作了!它是 b/c,它必须是“library(qdap)”而不是“library('qdap')”。还是谢谢你!
【解决方案2】:

您必须注意不要引入表格或 df 而不是向量作为函数 'score.sentiment' 的 'pos.words' 和 'neg.words' 参数。在这种情况下,它将花费更长的时间并且不返回任何结果。试试这样的:

score_result = score.sentiment(sentences = data$text, 
                               pos.words = as.character(pos_BL[ , 1]), 
                               neg.words = as.character(neg_BL[ , 1]), 
                               .progress= 'text')

也许 'as.character()' 函数不是必需的。

【讨论】:

    猜你喜欢
    • 2012-05-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多