错误将文本转换与tm_map为小写(...,tolower的)(Error converting t

2019-07-04 07:37发布

我尝试使用tm_map 。 它给了以下错误。 我怎样才能解决这个问题?

 require(tm)
 byword<-tm_map(byword, tolower)

Error in UseMethod("tm_map", x) : 
  no applicable method for 'tm_map' applied to an object of class "character"

Answer 1:

使用基础R函数tolower()

tolower(c("THE quick BROWN fox"))
# [1] "the quick brown fox"


Answer 2:

扩大我的评论在这里更详细的解答:你有包裹tolower内部content_transformer不要搞砸了VCorpus对象-是这样的:

> library(tm)
> data('crude')
> crude[[1]]$content
[1] "Diamond Shamrock Corp said that\neffective today it had cut its contract prices for crude oil by\n1.50 dlrs a barrel.\n    The reduction brings its posted price for West Texas\nIntermediate to 16.00 dlrs a barrel, the copany said.\n    \"The price reduction today was made in the light of falling\noil product prices and a weak crude oil market,\" a company\nspokeswoman said.\n    Diamond is the latest in a line of U.S. oil companies that\nhave cut its contract, or posted, prices over the last two days\nciting weak oil markets.\n Reuter"
> tm_map(crude, content_transformer(tolower))[[1]]$content
[1] "diamond shamrock corp said that\neffective today it had cut its contract prices for crude oil by\n1.50 dlrs a barrel.\n    the reduction brings its posted price for west texas\nintermediate to 16.00 dlrs a barrel, the copany said.\n    \"the price reduction today was made in the light of falling\noil product prices and a weak crude oil market,\" a company\nspokeswoman said.\n    diamond is the latest in a line of u.s. oil companies that\nhave cut its contract, or posted, prices over the last two days\nciting weak oil markets.\n reuter"


Answer 3:

myCorpus <- Corpus(VectorSource(byword))
myCorpus <- tm_map(myCorpus , tolower)

print(myCorpus[[1]])


Answer 4:

以这种方式使用的tolower有不良的副作用:如果您尝试了阴茎的后创造的一个术语文档矩阵,它就会失败。 这是因为在TM上进行了变更无法处理的tolower的返回类型。 相反,使用:

myCorpus <- tm_map(myCorpus, PlainTextDocument)


文章来源: Error converting text to lowercase with tm_map(…, tolower)