r - corpus 包的 tolower 函数抛出错误

标签 r text-mining tm corpus

我尝试使用 Twitter 数据进行一些文本挖掘。我执行以下操作:

#connect to twitter API
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)

#set radius and amount of requests
N=200  # tweets to request from each query
S=200  # radius in miles

lats=c(38.9,40.7)
lons=c(-77,-74)

roger=do.call(rbind,lapply(1:length(lats), function(i) searchTwitter('Roger+Federer',
                                                                  lang="en",n=N,resultType="recent",
                                                                  geocode=paste(lats[i],lons[i],paste0(S,"mi"),sep=","))))

这一切都工作正常,但是当我想像这样使用语料库包的 tolower 函数时:

data=as.data.frame(cbind(tweet=rogertext))
corpus=Corpus(VectorSource(data$tweet))
corpus=tm_map(corpus,tolower)

它引发了这个错误:

> corpus=tm_map(corpus,tolower)
Error in FUN(X[[i]], ...) : 
invalid input 'RT @Federerism: Roger Federer reaches  5 million followers   on twitter  Love You Roger í ½í¸˜ í ½í¸ í ½í¸˜ í ½í¸ #Roger #Federer #   Federerism #Maestro https:/…' in 'utf8towcs'

有没有想过哪里出了问题?

最佳答案

base::tolower 因特殊字符而被阻塞。在挖掘推文时,这通常是一个问题。您可以 try catch 错误或仅使用 stringi 的 tolower 吊坠:

# tw <- searchTwitter('Roger Federer reaches  5 million followers   on twitter  Love You Roger', n=1) 
download.file("https://www.dropbox.com/s/33ilhcu2v82nwuq/twitter_tolower.rda?dl=1", tf <- tempfile(fileext = ".rda"), mode="wb")
load(tf) 

tw[[1]]$getText()
# [1] "RT @Federerism: Roger Federer reaches  5 million followers on twitter  Love You Roger \xed��\xed�\u0098 \xed��\xed�\u008d \xed��\xed�\u0098 \xed��\xed�\u008d #Roger #Federer # Federerism #Maestro https:/…"

## Does not work:
tolower(tw[[1]]$getText())
# Error in tolower(tw[[1]]$getText()) : 
#   invalid input 'RT @Federerism: Roger Federer reaches  5 million followers on twitter  Love You Roger í ½í¸˜ í ½í¸ í ½í¸˜ í ½í¸ #Roger #Federer # Federerism #Maestro https:/…' in 'utf8towcs'

## Works:
stringi::stri_trans_tolower(tw[[1]]$getText())
# [1] "rt @federerism: roger federer reaches  5 million followers on twitter  love you roger \xed��\xed�\u0098 \xed��\xed�\u008d \xed��\xed�\u0098 \xed��\xed�\u008d #roger #federer # federerism #maestro https:/…"

## Works, too:
library(tm)
corp <- Corpus(VectorSource(tw[[1]]$getText()))
corp <- tm_map(corp, content_transformer(stringi::stri_trans_tolower))
content(corp[[1]])
# [1] "rt @federerism: roger federer reaches  5 million followers on twitter  love you roger \xed��\xed�\u0098 \xed��\xed�\u008d \xed��\xed�\u0098 \xed��\xed�\u008d #roger #federer # federerism #maestro https:/…"

关于r - corpus 包的 tolower 函数抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37497070/

相关文章:

r - 为什么在使用 rmarkdown 和 bookdown 时将外部 Rmd 文件包含在 latex 方程环境中会导致不同的 DOCX 输出?

nlp - 文本摘要: how to choose the right n-gram size

R tm 包创建 N 个最频繁项的矩阵

r - 如何在语料库中手动设置文档 ID?

r - 找不到 R 函数 row_sums 和 col_sums 的文档

r - 在 R 中使用 tm 包计数器 ngram

r - 有效地对大列中的所有先前行求和

r - 在读入 R 之前检查文件的分隔方式

r - R 中的网络和弦图问题

r - 文本分类-当训练数据和测试数据具有不同特征时该怎么办