[英]Exporting Corpus from TM in R
我正在尝试将Corpus
对象从R导出到静态文件。 语料库包含通过解析文件系统中现有的预处理文件而创建的词干文档。 作者在他的“R中的文本挖掘简介”(第2页)中描述了一种方法
> writeCorpus(file)
但到目前为止我的尝试只产生以下结果:
Error in UseMethod("as.PlainTextDocument", x):
no applicable method for 'as.PlainTextDocument' applied to an object of class "character"
到目前为止,我的脚本非常简单,我希望它可能是一个简单的疏忽。 非常感谢任何建议:它似乎是边缘问题。
# Turn off Java so it doesn't interfere with Weka interface
Sys.setenv(NOAWT=1)
# Load required text mining packages
require(tm)
require(rJava)
require(RWeka)
require(Snowball)
# Populate a vector with the number of subdirectories in preprocessed dir
preprocessed <- list.files(path="preprocessed_dir", include.dirs=TRUE, full.names=TRUE)
# For each element in the vector
for(i in 1:length(preprocessed)) {
# Get the files in each subdirectory by appending a number to the absolute path
files <- list.files(sprintf("preprocessed_dir/%.0f", i))
# Create a Corpus object of all the files in the subdirectory
corpora <- Corpus(VectorSource(files))
# Stem the words in the Corpus object
corpora <- tm_map(corpora, SnowballStemmer)
# (Try to) write the object to the file system
writeCorpus(corpora)
}
FWIW:调用class(corpora)
返回[1] "VCorpus" "Corpus" "list"
因此对象显然不是类型character
我担心你为什么要导出语料库。 如果要向其他人显示文本,可以使用原始文本。
如果你想导出它并将其重用于R,我的建议是你可以使用函数save()来将语料库保存到.RData中。
然后,如果要加载它,只需使用load()函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.