【问题标题】:Getting data from twitter with R?使用 R 从 twitter 获取数据?
【发布时间】:2016-06-03 20:23:13
【问题描述】:

我在平台上使用 R 3.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' 没有适用的方法 应用于“try-error”类的对象另外:警告 消息:1:在 mclapply(x$content[i], function(d) tm_reduce(d, x$lazy$maps)) :所有计划的核心在用户代码中遇到错误 2:在 mclapply(unname(content(x)), termFreq, control) 中:全部 调度核心在用户代码中遇到错误

有人知道解决方案吗?

【问题讨论】:

  • 这还不是一个最小的工作示例。请减少代码(例如各种清理步骤)以准确找出导致错误的原因。
  • 问题出在removeWords函数上。没有该函数,代码运行良好,但仍需要为该函数找到解决方法。

标签: r text-mining tm twitter-r


【解决方案1】:

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)

【讨论】:

    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2013-09-01
    • 1970-01-01
    • 2014-02-18
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多