我正在尝试将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