如何修复以下代码中找不到的对象?

时间:2019-01-18 11:51:46

标签: r sentiment-analysis

我正在R中进行情感分析项目,每当我运行代码,所使用的库和代码如下时,都会收到一条错误消息“找不到对象”(同样,我也没有忘记输入api详细信息在我的代码中):

library("twitteR")
library("ROAuth")
library("NLP")
library("twitteR")
library("syuzhet")
library("tm")
library("SnowballC")
library("stringi")
library("topicmodels")
library("syuzhet")
library("ROAuth")
library("wordcloud")
library("ggplot2")

# authorisation keys
#provided by me in the code.

setup_twitter_oauth(consumer_key,consumer_secret,access_token, access_secret)
tweets_g <- searchTwitter("#google", n=500,lang = "en")

google_tweets <- twListToDF(tweets_g)
View(google_tweets)
google_text<- google_tweets$text

google_text<- tolower(google_text) 
google_text <- gsub("rt", "", google_text)
google_text <- gsub("@\\w+", "", google_text)
google_text <- gsub("[[:punct:]]", "", google_text)
google_text <- gsub("http\\w+", "", google_text)
google_text <- gsub("[ |\t]{2,}", "", google_text)
google_text <- gsub("^ ", "", google_text)
google_text <- gsub(" $", "", google_text)

#clean up by removing stop words
google_tweets.text.corpus <- tm_map(google_tweets.text.corpus, function(x)removeWords(x,stopwords()))

#generate wordcloud
wordcloud(google_tweets.text.corpus,min.freq = 10,colors=brewer.pal(8, "Dark2"),random.color = TRUE,max.words = 500)

#getting emotions using in-built function
mysentiment_google<-get_nrc_sentiment((google_text))

#calculationg total score for each sentiment
Sentimentscores_google<-data.frame(colSums(mysentiment_google[,]))

names(Sentimentscores_google)<-"Score"
Sentimentscores_google<-cbind("sentiment"=rownames(Sentimentscores_google),Sentimentscores_google)
rownames(Sentimentscores_google)<-NULL

#plotting the sentiments with scores
ggplot(data=Sentimentscores_google,aes(x=sentiment,y=Score))+geom_bar(aes(fill=sentiment),stat = "identity")+
  theme(legend.position="none")+
  xlab("Sentiments")+ylab("scores")+ggtitle("Sentiments of people behind the tweets on tech giant GOOGLE")

运行R脚本时出现的错误消息是:

Loading required package: RColorBrewer

Attaching package: ‘ggplot2’

The following object is masked from ‘package:NLP’:

    annotate

[1] "Using direct authentication"
Error in tm_map(google_text.corpus, function(x) removeWords(x, stopwords())) :
  object 'google_text.corpus' not found
Execution halted````


2 个答案:

答案 0 :(得分:1)

尝试删除语料库。只需用此代码段替换您的代码


#generate wordcloud
wordcloud(min.freq = 10,colors=brewer.pal(8, "Dark2"),random.color = TRUE,max.words = 500)

答案 1 :(得分:0)

键为object 'google_text.corpus' not found,您正在尝试调用尚未定义的变量。您需要问自己自己“ google_text.corpus”应该是什么,然后对其进行定义,就像对变量google_tweets所做的那样,行google_tweets <- twListToDF(tweets_g)一样。