首页 > 解决方案 > 如何在 R 中使用自定义元保存 TM VCorpus

问题描述

我正在 R 中保存一个 TM 语料库。

writeCorpus(as.character(thecrp), dirdst, filenames = NULL)

一切都很好,直到我得到一个错误:

Error in UseMethod("meta", x) : no method applicable for meta applicable to an object of class "character"

注意。我在语料库的每个文档中添加了三个自定义元字段。

这是一个可重现的示例:

# Create corpus from a folder of documents on disk
thetxts <- readtext(pthtxt, cache = FALSE)
thecrp <- corpus(thetxts)
# Add docvars (= columns)
# Reference
# NOTE: Add Meta to the corpus
meta(thecrp, tag = "doctyp", type = "corpus") <- "Reports, 2018"
# NOTE: Add Meta to each of the documents
for (i in 1:length(thecrp)){
    # ID derived from the name of the file (withdraw .txt)
    meta(thecrp[[i]], tag = "id", type = "indexed") <- gsub("\\.txt", "", meta(thecrp[[i]], tag = "id", type = "indexed"))
    # constants
    meta(thecrp[[i]], tag = "author", type = "indexed") <- "author"
    meta(thecrp[[i]], tag = "type", type = "indexed") <- "individual"
    meta(thecrp[[i]], tag = "prov", type = "indexed") <- "ocr"
}
# Then I perform usual and **mildly** transformative actions on the corpus
...
# Then I save it to disk: the **error occurs here**
dirdst <- "~/test"
writeCorpus(as.character(thecrp), dirdst, filenames = NULL)

标签: rtmcorpus

解决方案


推荐阅读