【问题标题】:Sentiment Analysis in RR中的情绪分析
【发布时间】:2015-09-16 02:45:46
【问题描述】:

我是情感分析的新手,完全不知道如何使用 R 进行。因此,我想在这方面寻求帮助和指导。

我有一组由意见组成的数据,并想分析意见。

Title      Date            Content    
Boy        May 13 2015     "She is pretty", Tom said. 
Animal     June 14 2015    The penguin is cute, lion added.
Human      March 09 2015   Mr Koh predicted that every human is smart..
Monster    Jan 22 2015     Ms May, a student, said that John has $10.80. 

谢谢。

【问题讨论】:

标签: r sentiment-analysis


【解决方案1】:

情感分析包含一系列广泛的方法,旨在衡量文本中的正面和负面情绪,因此很难简单地回答这个问题。但这里有一个简单的答案:您可以将字典应用于您的文档术语矩阵,然后结合字典的正面和负面关键类别来创建情绪度量。

我建议在文本分析包 quanteda 中尝试这个,它可以处理各种现有的字典格式并允许您创建非常灵活的自定义字典。

例如:

require(quanteda)
mycorpus <- subset(inaugCorpus, Year>1980)
mydict <- dictionary(list(negative = c("detriment*", "bad*", "awful*", "terrib*", "horribl*"),
                          postive = c("good", "great", "super*", "excellent")))
myDfm <- dfm(mycorpus, dictionary = mydict)
## Creating a dfm from a corpus ...
##    ... lowercasing
##    ... tokenizing
##    ... indexing documents: 9 documents
##    ... indexing features: 3,113 feature types
##    ... applying a dictionary consisting of 2 keys
##    ... created a 9 x 2 sparse dfm
##    ... complete. 
## Elapsed time: 0.057 seconds.
myDfm
## Document-feature matrix of: 9 documents, 2 features.
## 9 x 2 sparse Matrix of class "dfmSparse"
##               features
## docs           negative postive
##   1981-Reagan         0       6
##   1985-Reagan         0       6
##   1989-Bush           0      18
##   1993-Clinton        1       2
##   1997-Clinton        2       8
##   2001-Bush           1       6
##   2005-Bush           0       8
##   2009-Obama          2       3
##   2013-Obama          1       3

# use a LIWC dictionary - obviously you need this file
liwcdict <- dictionary(file = "LIWC2001_English.dic", format = "LIWC")
myDfmLIWC <- dfm(mycorpus, dictionary = liwcdict)
## Creating a dfm from a corpus ...
##    ... lowercasing
##    ... tokenizing
##    ... indexing documents: 9 documents
##    ... indexing features: 3,113 feature types
##    ... applying a dictionary consisting of 68 keys
##    ... created a 9 x 68 sparse dfm
##    ... complete. 
## Elapsed time: 1.844 seconds.
myDfmLIWC[, grep("^Pos|^Neg", features(myDfmLIWC))]
## Document-feature matrix of: 9 documents, 4 features.
## 9 x 4 sparse Matrix of class "dfmSparse"
##               features
## docs           Negate Posemo Posfeel Negemo
##   1981-Reagan      46     89       5     24
##   1985-Reagan      28    104       7     33
##   1989-Bush        40    102      10      8
##   1993-Clinton     25     51       3     23
##   1997-Clinton     27     64       5     22
##   2001-Bush        40     80       6     27
##   2005-Bush        25    117       5     31
##   2009-Obama       40     83       5     46
##   2013-Obama       42     80      13     22

对于您的语料库,假设您将其放入名为 data 的 data.frame 中,您可以使用以下方法创建一个 quanteda 语料库:

mycorpus <- corpus(data$Content, docvars = data[, 1:2])

另请参阅?textfile,通过一个简单的命令从文件中加载内容。例如,这适用于 .csv 文件,尽管您会遇到该文件的问题,因为 Content 字段包含包含逗号的文本。

当然,还有许多其他方法可以衡量情绪,但如果您是情绪挖掘和 R 的新手,那应该可以帮助您入门。你可以阅读更多关于情感挖掘方法的信息(如果你已经遇到过这些方法,请见谅):

【讨论】:

  • 嗨,谢谢。我正在尝试代码,并且在运行以下行时收到了有效对象(.Object)中的错误:无效的类“dfmSprase”对象:dictDFM
  • 下载3.2版本后就可以了。但是,我无法打开 LIWC 字典。 @KenBenoit
  • 我也读过很多情感挖掘方法,但我不知道如何应用它们。你能指导我吗?非常感谢。 @KenBenoit
  • LIWC 词典可从liwc.net 购买。 Provalis Research website too 有很多免费词典。关于不同的方法,如果你更具体,我可以尝试提供帮助。
  • 你应该看看 Loughran、T 和 Bill McDonald。 2011.“什么时候责任不是责任?文本分析、字典和 10-Ks。” 金融杂志 66(1): 35–65.他们试图将Harvard-IV pos-neg 类别应用于公司10-K 文件,发现字典需要对其应用进行重大调整。您可以从上面的 Provalis 链接以 Wordstat 格式(quanteda 支持)下载他们的词典。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-01
  • 2017-11-06
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多