【问题标题】:How to fix object not found in the following code?如何修复以下代码中未找到的对象?
【发布时间】:2019-01-19 22:05:53
【问题描述】:

我正在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````


【问题讨论】:

  • 哪一行删除了错误?
  • 注释掉这一行可以消除错误,但同时这些是至关重要的。``` google_tweets.text.corpus

标签: r sentiment-analysis


【解决方案1】:

尝试删除语料库。只需用这个 sn-p 替换您的代码


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

【讨论】:

    【解决方案2】:

    关键是object 'google_text.corpus' not found 你正试图调用一个你没有定义的变量。您需要问自己您认为“google_text.corpus”应该是什么,然后定义它,就像您对变量google_tweets 使用google_tweets &lt;- twListToDF(tweets_g) 行所做的那样。

    【讨论】:

      猜你喜欢
      • 2013-09-22
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 2021-05-20
      相关资源
      最近更新 更多