tm_map方法出错

时间:2017-03-25 14:40:08

标签: r classification tm corpus

我是R和tm包的新手。我的目标是使用决策树执行文本文档分类。我关注某人的project.在第14页,有一个完整的代码。有两种类型的文档,我使用DirSource加载时没有任何问题。我的下一步是将这两个语料库合并到集合

   # Merge corpora into one collection 
docs <- c( wheat.train , crude.train , wheat.test , crude.test ) ;

然后我想进行一些预处理。

#pre-processing
docs.p <- docs
docs.p <- tm_map (docs.p, stripWhitespace)

但我收到了这样的错误

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

我知道这个人正在使用tm's之前版本中的一个,目前tm_map将语料库作为参数,而不是语料库的集合。我的问题是如何创建这样的语料库集合,以便对其进行预处理?

1 个答案:

答案 0 :(得分:1)

使用list代替c而不是lapply对我有用。

ex1 <- "bla bla blah   "
ex2 <- "dunno    what else to say    "

wheat <- Corpus(VectorSource(ex1))
crude <- Corpus(VectorSource(ex2))

docs <- list(wheat, crude)
docs.p <- lapply(docs, tm_map, stripWhitespace)