我在平台上使用R3.1.3: x86_64-apple-darwin13.4.0 (64位)和tm_0.6-2版本的软件包
以下是我的代码:
install.packages(c("twitterR","ROAuth","RCurl","tm","wordcloud","SnowballC"))
library(SnowballC)
library(twitteR)
library(ROAuth)
library(RCurl)
library(tm)
library(wordcloud)
#twitter authentication
consumerKey <- " "
consumerSecret <- " "
accessToken <- " "
accessTokenSecret <- " "
twitteR::setup_twitter_oauth(consumerKey,consumerSecret,accessToken,accessTokenSecret)
#retrive tweets from twitter
tweets=searchTwitter("euro2016+france",lang = "en",n=500,resultType = "recent")
class(tweets)
head(tweets)
#converting list to vector
tweets_text=sapply(tweets,function(x) x$getText())
str(tweets_text)
#creates corpus from vector of tweets
tweets_corpus=Corpus(VectorSource(tweets_text))
inspect(tweets_corpus[100])
#cleaning
tweets_clean=tm_map(tweets_corpus,removePunctuation,lazy= T)
tweets_clean=tm_map(tweets_clean,content_transformer(tolower),lazy = T)
tweets_clean=tm_map(tweets_clean,removeWords,stopwords("english"),lazy = T)
tweets_clean=tm_map(tweets_clean,removeNumbers,lazy = T)
tweets_clean=tm_map(tweets_clean,stripWhitespace,lazy = T)
tweets_clean=tm_map(tweets_clean,removeWords,c("euro2016","france"),lazy = T)
#wordcloud play with parameters
wordcloud(tweets_clean)
当我运行最后一行时,我得到:
UseMethod中的错误(“meta”,x):不适用于类“尝试-错误”对象的“meta”方法,此外:警告消息: 1:在mclapply中(x$contenti,函数(D) tm_reduce(d,x$lazz$tm_reduce)):用户代码2中遇到的所有调度内核都遇到错误:mclapply(unname(内容(X)),termFreq,control):所有计划好的核心在用户代码中遇到错误。
有人知道这个问题的解决方案吗?
发布于 2016-06-03 22:11:19
当removeWords
函数与tm_map
函数一起使用时,似乎存在编码问题(也请参阅here)。
在您将文本加载到语料库时,可能会在更早的时候使用该函数:
#converting list to vector
tweets_text=sapply(tweets,function(x) x$getText())
str(tweets_text)
# removing words
tweets_text<- sapply(tweets_text, function(x) removeWords(x, c("euro2016","france")))
tweets_text<- sapply(tweets_text, function(x) removeWords(x, stopwords("english")))
#creates corpus from vector of tweets
tweets_corpus=Corpus(VectorSource(tweets_text))
inspect(tweets_corpus[100])
#cleaning
tweets_clean=tm_map(tweets_corpus,removePunctuation)
tweets_clean=tm_map(tweets_clean,content_transformer(tolower))
#tweets_clean=tm_map(tweets_clean,removeWords,stopwords("english"))
tweets_clean=tm_map(tweets_clean,removeNumbers,lazy = T)
tweets_clean=tm_map(tweets_clean,stripWhitespace,lazy = T)
#tweets_clean=tm_map(tweets_clean,removeWords,c("euro2016","france"),lazy = T)
wordcloud(tweets_clean)
https://stackoverflow.com/questions/37622774
复制相似问题