R中的情感分析

时间:2015-09-16 02:45:47

标签: r sentiment-analysis

我是情绪分析的新手,并且完全不知道如何使用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. 

谢谢。

1 个答案:

答案 0 :(得分:4)

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

我建议在文本分析包 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文件,尽管您可能会遇到该文件的问题,因为“内容”字段包含包含逗号的文本。

当然还有很多其他方法可以衡量情绪,但如果你对情绪挖掘和R不熟悉,那应该可以帮助你开始。您可以通过以下方式阅读有关情绪挖掘方法的更多信息(如果您已经遇到过这些方法,则道歉):