0

I am working on a sentiment analysis project in R and getting an error message of " object not found" whenever I run the code, the library used and the code are as follows (also I did not forget to put the api details in my code):

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")

the error message that shows up is on running the R script is:

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````


Siddhinath
  • 111
  • 7
  • Which line drops the error? – MRau Jan 18 '19 at 12:07
  • commenting out this lines removes the error but at the same time these are crucial to have.``` google_tweets.text.corpus <- tm_map(google_tweets.text.corpus, function(x)removeWords(x,stopwords())) – Siddhinath Jan 18 '19 at 16:57

2 Answers2

1

Try removing corpus. Just replace your code with this snippet


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

Dipanshu Chaubey
  • 880
  • 1
  • 9
  • 18
0

The key is object 'google_text.corpus' not found You are trying to call a variable you have not defined. You need to ask yourself what you think 'google_text.corpus' should be and then define it, just as you did for the variable google_tweets with the line google_tweets <- twListToDF(tweets_g).

ovid
  • 51
  • 2
  • 7