R中的Twitter情感分析

时间:2016-10-13 06:51:45

标签: r twitter sentiment-analysis

我正在利用我在网上找到的代码使用AFINN情绪字典进行Twitter情感分析,但我一直收到错误而且我不确定我哪里出错了。我的代码是:

tweets <- data.frame(read.csv("tweets.csv"))
pacman::p_load(tm)
library(tm)
library(SnowballC)
dtm.control <- list(
  tolower = TRUE, 
  removePunctuation = TRUE, 
  removeNumbers = TRUE,
  stopwords = stopwords("english"),
  stemming = FALSE,
  wordLengths = c(3,"inf")
)

corpboston <- Corpus(VectorSource(tweets$Tweet))
dtm.boston <- DocumentTermMatrix(corpboston, control = dtm.control)

afinn_list <- read.delim(file = "AFINN-111.txt", header = FALSE,   
                         stringsAsFactors = FALSE)  
names(afinn_list) <- c("word", "score")  
afinn_list$word <- tolower(afinn_list$word)

sentiment <- function(afinn_list, tweets) {
  tweet.sent < mat.or.vec(nrow(tweets),3)
  colnames(tweet.sent) <- c('doc','epi','total')
  word.frequency <- vector('list', nrow(tweets))
  for(f in 1:nrow(tweets))
  { tweet.sent[f, 1] <- tweets$doc[f]
    tweet.sent[f, 1] <- tweets$epi[f]
      corpboston <- Corpus(VectorSource(tweets$Tweet[f]))
      dtm.boston <- DocumentTermMatrix(corpboston, control = dtm.control)
      TermFreq <- data.frame(findFreqTerms(dtm.boston), dtm.boston$v)
      word.frequency[[f]] <- data.frame(findFreqTerms(dtm.boston), dtm.boston$v)
      sentwords <- intersect(findFreqTerms(dtm.boston), afinn_list[,1])
      sentwords.index <- match(sentwords, afinn_list[,1])
      if(length(sentwords.index) > 0)
    {
      sentwords.score <- affin_list[sentwords.index,2]
      sentwords.freq <- TermFreq[match(sentwords,TermFreq[,1]),2]
      tweet.sent[f,3] <- sum(sentwords.score*sentwords.freq)
    }
  }  
  return(tweet.sent)}
TweetSent <- data.frame(sentiment(affin_list,tweets))

我得到的错误是:

情绪错误(affin_list,tweets):找不到对象'tweet.sent'

关于我需要更改或添加什么以使分析工作的任何想法?

非常感谢所有帮助!

0 个答案:

没有答案