Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中从TM导出语料库_R_Text Mining - Fatal编程技术网

在R中从TM导出语料库

在R中从TM导出语料库,r,text-mining,R,Text Mining,我正在尝试将Corpus对象从R导出到静态文件。语料库包含通过解析文件系统中现有预处理文件创建的带词干的文档。作者在他的《R中的文本挖掘导论》(第2页)中描述了一种实现这一点的方法,建议 > writeCorpus(file) 但迄今为止,我的尝试只能产生以下结果: Error in UseMethod("as.PlainTextDocument", x): no applicable method for 'as.PlainTextDocument' applied to an

我正在尝试将
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)
}
#关闭Java,使其不会干扰Weka接口
系统设置环境(NOAWT=1)
#加载所需的文本挖掘包
要求(商标)
要求(rJava)
要求(RWeka)
要求(滚雪球)
#用预处理目录中的子目录数填充向量

预处理我正在解释为什么要导出语料库。如果你想向其他人展示文本,你可以只使用原始文本

如果您想将其导出并与R一起重用,我的建议是可以使用函数save()将语料库保存到.RData中


如果您想加载它,只需使用load()函数。

实际上,它看起来像是
语料库
在词干生成之前是一个
语料库
类对象,然后在词干生成之后变成
字符
类型对象(不能用
writeCorpus
写入)正在研究将其强制回
语料库
对象的方法!好吧,我是n00b,所以我不能回答我自己的问题,但这里是:
tm
casts
Corpus
对象,它们被
tm\u map
调用为
字符
对象
。在通过调用`>corpora将它们写入文件系统之前,必须将它们强制回
corpora`对象。corpora写入的目录是什么?