R,tm转换错误导致文档下降

14

我希望创建一个基于文本关键字权重的网络。但是,在运行与tm_map相关的代码时,出现了错误:

library (tm)
library(NLP)
lirary (openNLP)

text = c('.......')
corp <- Corpus(VectorSource(text))
corp <- tm_map(corp, stripWhitespace)

Warning message:
In tm_map.SimpleCorpus(corp, stripWhitespace) :
transformation drops documents

corp <- tm_map(corp, tolower)

Warning message:
In tm_map.SimpleCorpus(corp, tolower) : transformation drops documents

这些代码2个月前能够工作,现在我正在尝试新数据,但它已经不能工作了。请问有人能告诉我哪里出了错吗?谢谢。

我甚至尝试了下面的命令,但它也不起作用。

corp <- tm_map(corp, content_transformer(stripWhitespace))
1个回答

21
代码应该仍然可以正常工作。您会收到一个警告,而不是错误。只有当您使用基于VectorSource的语料库并且使用Corpus而不是VCorpus时,才会出现此警告。
原因是底层代码中有一个检查,以查看语料库内容的名称数是否与语料库内容的长度相匹配。通过将文本读取为向量,没有文档名称,因此会弹出此警告。这只是一个警告,没有删除任何文档。
请参阅2个示例之间的区别。
library(tm)

text <- c("this is my text with some other text and some more")

# warning based on Corpus and Vectorsource
text_corpus <- Corpus(VectorSource(text))

# warning appears running following line
tm_map(text_corpus, content_transformer(tolower))
<<SimpleCorpus>>
Metadata:  corpus specific: 1, document level (indexed): 0
Content:  documents: 1
Warning message:
In tm_map.SimpleCorpus(text_corpus, content_transformer(tolower)) :
  transformation drops documents

# Using VCorpus
text_corpus <- VCorpus(VectorSource(text))

# warning doesn't appear
tm_map(text_corpus, content_transformer(tolower))
<<VCorpus>>
Metadata:  corpus specific: 0, document level (indexed): 0
Content:  documents: 1
tm_map(text_corpus, content_transformer(tolower))

嗨 @phiver:非常感谢,你的回答真正解答了我的问题。 - Julie
记录一下:VCorpusvolatile corpus,而 "A volatile corpus is fully kept in memory and thus all changes only affect the corresponding R object."(参见 tm)。 - Rafs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接