运行' tolower'运行后出现FUN错误同时制作Twitter wordcloud

时间:2015-01-03 16:02:18

标签: r twitter tm

尝试从twitter数据创建wordcloud,但收到以下错误:

Error in FUN(X[[72L]], ...) : 
  invalid input '������������❤������������ "@xxx:bla, bla, bla... http://t.co/56Fb78aTSC"' in 'utf8towcs' 

运行“mytwittersearch_corpus< - 后出现此错误   tm_map(mytwittersearch_corpus,tolower)“code

mytwittersearch_list <-sapply(mytwittersearch, function(x) x$getText())

mytwittersearch_corpus <-Corpus(VectorSource(mytwittersearch_corpus_list))
mytwittersearch_corpus<-tm_map(mytwittersearch_corpus, tolower)
mytwittersearch_corpus<-tm_map( mytwittersearch_corpus, removePunctuation)
mytwittersearch_corpus <-tm_map(mytwittersearch_corpus, function(x) removeWords(x, stopwords()))

我在其他页面上看到这可能是由于R难以处理非英语语言中的符号,表情符号和字母,但这似乎不是R有问题的“错误推文”的问题。我确实运行了代码:

mytwittersearch_corpus <- tm_map(mytwittersearch_corpus, function(x) iconv(enc2utf8(x), sub = "byte"))
mytwittersearch_corpus<- tm_map(mytwittersearch_corpus, content_transformer(function(x)    iconv(enc2utf8(x), sub = "bytes")))

这些没有帮助。即使content_transformer已经检查并运行,我也发现找不到函数tm-package

我在OS X 10.6.8上运行它并使用最新的RStudio。

8 个答案:

答案 0 :(得分:10)

我使用此代码来摆脱问题字符:

tweets$text <- sapply(tweets$text,function(row) iconv(row, "latin1", "ASCII", sub=""))

答案 1 :(得分:2)

从Twitter数据创建wordcloud的一个很好的例子是here。使用示例和下面的代码,并在创建TermDocumentMatrix时传递tolower参数,我可以创建一个Twitter wordcloud。

library(twitteR)
library(tm)
library(wordcloud)
library(RColorBrewer)
library(ggplot2)


#Collect tweets containing 'new year'
tweets = searchTwitter("new year", n=50, lang="en")

#Extract text content of all the tweets
tweetTxt = sapply(tweets, function(x) x$getText())

#In tm package, the documents are managed by a structure called Corpus
myCorpus = Corpus(VectorSource(tweetTxt))

#Create a term-document matrix from a corpus
tdm = TermDocumentMatrix(myCorpus,control = list(removePunctuation = TRUE,stopwords = c("new", "year", stopwords("english")), removeNumbers = TRUE, tolower = TRUE))

#Convert as matrix
m = as.matrix(tdm)

#Get word counts in decreasing order
word_freqs = sort(rowSums(m), decreasing=TRUE) 

#Create data frame with words and their frequencies
dm = data.frame(word=names(word_freqs), freq=word_freqs)

#Plot wordcloud
wordcloud(dm$word, dm$freq, random.order=FALSE, colors=brewer.pal(8, "Dark2"))

enter image description here

答案 2 :(得分:2)

您是否尝试更新tm并使用stri_trans_tolower中的stringi

library(twitteR)
library(tm)
library(stringi)
setup_twitter_oauth("CONSUMER_KEY", "CONSUMER_SECRET")
mytwittersearch <- showStatus(551365749550227456) 
mytwittersearch_list <- mytwittersearch$getText()
mytwittersearch_corpus <- Corpus(VectorSource(mytwittersearch_list))

mytwittersearch_corpus <- tm_map(mytwittersearch_corpus, content_transformer(tolower))
# Error in FUN(content(x), ...) : 
#   invalid input 'í ½í±…í ¼í¾¯â¤í ¼í¾§í ¼í½œ "@comScore: Nearly half of #Millennials do at least some of their video viewing from a smartphone or tablet: http://t.co/56Fb78aTSC"' in 'utf8towcs'

mytwittersearch_corpus <- tm_map(mytwittersearch_corpus, content_transformer(stri_trans_tolower))
inspect(mytwittersearch_corpus)
# <<VCorpus (documents: 1, metadata (corpus/indexed): 0/0)>>
#   
# [[1]]
# <<PlainTextDocument (metadata: 7)>>
# <ed><U+00A0><U+00BD><ed><U+00B1><U+0085><ed><U+00A0><U+00BC><ed><U+00BE><U+00AF><U+2764><ed><U+00A0><U+00BC><ed><U+00BE><U+00A7><ed><U+00A0><U+00BC><ed><U+00BD><U+009C> "@comscore: nearly half of #millennials do at least some of their video viewing from a smartphone or tablet: http://t.co/56fb78atsc"

答案 3 :(得分:2)

上述解决方案在wordcloud和tm的最新版本中可能已经有效但不再有效。

这个问题几乎让我发疯,但是我找到了一个解决方案,并希望尽力解释它,以拯救任何绝望的人。

由wordcloud隐式调用并负责抛出错误的函数

 Error in FUN(content(x), ...) : in 'utf8towcs'

就是这个:

words.corpus <- tm_map(words.corpus, tolower)

这是

的快捷方式
words.corpus <- tm_map(words.corpus, content_transformer(tolower))

为了提供可重现的示例,这里有一个嵌入解决方案的函数:

plot_wordcloud <- function(words, max_words = 70, remove_words ="",
                           n_colors = 5, palette = "Set1")
{
    require(dplyr)
    require(wordcloud)
    require(RColorBrewer) # for brewer.pal()
    require(tm) # for tm_map()

    # Solution: remove all non-printable characters in UTF-8 with this line
    words <- iconv(words, "ASCII", "UTF-8", sub="byte")

    wc <- wordcloud(words=words.corpus, max.words=max_words,
                    random.order=FALSE,
                    colors = brewer.pal(n_colors, palette),
                    random.color = FALSE,
                    scale=c(5.5,.5), rot.per=0.35) %>% recordPlot
    return(wc)
}

这是失败的原因:

我尝试使用

创建语料库之前和之后转换文本
words.corpus <- Corpus(VectorSource(words))

在:

在文本上转换为UTF-8不适用于:

words <- sapply(words, function(x) iconv(enc2utf8(x), sub = "byte"))

,也不

for (i in 1:length(words))
{
    Encoding(words[[i]])="UTF-8"
}

在:

在语料库中转换为UTF-8不适用于:

    words.corpus <- tm_map(words.corpus, removeWords, remove_words)

,也不

    words.corpus <- tm_map(words.corpus, content_transformer(stringi::stri_trans_tolower))

,也不

    words.corpus <- tm_map(words.corpus, function(x) iconv(x, to='UTF-8'))

,也不

    words.corpus <- tm_map(words.corpus, enc2utf8)

,也不

    words.corpus <- tm_map(words.corpus, tolower)

所有这些解决方案可能在某个时间点起作用,所以我不想诋毁作者。他们将来可能会工作一段时间。但是为什么他们不工作几乎是不可能的,因为有很好的理由为什么他们应该工作。 无论如何,只需记住在创建语料库之前转换文本:

    words <- iconv(words, "ASCII", "UTF-8", sub="byte")

声明: 我在这里得到了更详细解释的解决方案: http://www.textasdata.com/2015/02/encoding-headaches-emoticons-and-rs-handling-of-utf-816/

答案 4 :(得分:0)

我最终更新了我的RStudio和软件包。这似乎解决了tolower / content_transformer问题。我在某处读到最后一个tm-package与tm_map有一些问题,所以也许这就是问题所在。无论如何,这都有效!

答案 5 :(得分:0)

而不是

corp <- tm_map(corp, content_transformer(tolower), mc.cores=1)

使用

corp <- tm_map(corp, tolower, mc.cores=1)

答案 6 :(得分:0)

在使用类似于上面的代码并使用在我自己的电脑上运行良好的文字云闪亮应用程序,但在亚马逊aws或闪亮apps.io上没有工作时,我发现带有'重音符号'的文字,例如其中的santé没有像csv文件一样上传到云端。我通过使用记事本将文件保存为.txt文件并在utf-8中找到了解决方案,并重新编写了我的代码以允许文件不再是csv而是txt。我的R版本是3.2.1,Rstudio版本是0.99.465

答案 7 :(得分:0)

仅提及一下,我在不同的情况下遇到了相同的问题(与tm或Twitter无关)。对我来说,解决方案是iconv(x, "latin1", "UTF-8"),尽管Encoding()告诉我它已经是UTF-8。