【问题标题】:FUN-error after running 'tolower' while making Twitter wordcloud在制作 Twitter wordcloud 时运行“tolower”后出现 FUN 错误
【发布时间】:2015-03-01 15:25:42
【问题描述】:

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

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

运行“mytwittersearch_corpus”后出现此错误

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。

【问题讨论】:

  • 也许你想试试 stringi 包中的 tolower 函数:tm_map(mytwittersearch_corpus, content_transformer(stringi::stri_trans_tolower)).
  • content_transformer 相对较新。您可能需要更新软件包。 packageVersion("tm") 是什么?
  • 正如 Richard 所说,发布 R 的版本和加载的包的版本可能更重要。 sessionInfo() 函数是收集和呈现该信息的最简单方法。
  • 感谢您的帮助,理查德!我试图运行代码,但不幸的是我得到了同样的信息。我会尝试更新 tm 包。 content_transformer 的缺失可以解释错误吗?
  • 查看使用精简版mytwittersearch(可能是small &lt;- head(mytwittersearch))是否会出现同样的错误。如果是这样,那么您应该发布dput(small) 的输出

标签: r twitter tm


【解决方案1】:

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

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

【讨论】:

  • 感谢 Deb,我尝试运行它,但遇到了同样的“tolower”问题:tolower(txt) 中的错误:无效输入“我的新年希望:: : : 获取 ✨@Harry_Styles ✨ 这是我从 2013 年开始的梦想.. 我从心底里爱他❤️ 请在 'utf8towcs' 中......它可以工作,但“tm”包似乎有问题。
  • 我可以在Windows XP OS和RStudio中成功运行上述代码并获得wordcloud。
【解决方案2】:

我使用这段代码来消除问题字符:

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

【讨论】:

  • 不幸的是它对我不起作用,但我想知道,它是否也摆脱了表情符号或只有字母字符?
  • 它为我删除了所有的表情符号。
  • 此解决方案有效,但仅适用于拉丁字符。遇到频繁的utf-8转换问题,最简单的转换代码好像是:tweets$text &lt;- iconv(tweets$text, "ASCII", "UTF-8", sub="byte")
【解决方案3】:

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

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"

【讨论】:

  • Stringi 也没有帮助:(这很奇怪,因为我仍然得到响应:match.fun(FUN) 中的错误:找不到函数“content_transformer”。是否可以在任何替代方案中找到 content_transformer包?
  • 请问,content_trasformer 是否列为您的 tm=package 的一部分?当我在我的 tm-package 下搜索它时,我得到“找不到结果”。我觉得这很奇怪。如果这适用于其他人,比较会非常有趣。然后我知道我的 tm 包有问题。
  • @AnneBoysen cran.r-project.org/web/packages/tm/tm.pdf#page=4 - 是的,它是 tm 的一部分。
  • 现在,使用 tm 包版本 0.5-10,您可以简单地使用:mytwittersearch_corpus
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    代替

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

    使用

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

    【讨论】:

      【解决方案6】:

      在使用与上述类似的代码并在我自己的电脑上运行良好的 word cloud shiny 应用程序上工作时,但在 amazon aws 或 Shiny apps.io 上都不能工作,我发现带有“口音”的文本,例如santé 没有像 csv 文件一样上传到云端。我通过使用记事本将文件保存为 .txt 文件和 utf-8 并重写我的代码以允许文件不再是 csv 而是 txt 的事实找到了解决方案。我的 R 版本是 3.2.1 而 Rstudio 是版本 0.99.465

      【讨论】:

        【解决方案7】:

        上述解决方案可能已经奏效,但在最新版本的 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/

        【讨论】:

          【解决方案8】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-05-25
            • 2012-07-11
            • 1970-01-01
            • 2019-08-25
            • 1970-01-01
            • 1970-01-01
            • 2021-06-16
            相关资源
            最近更新 更多