从 VCorpus 导出文本文件,包括 R 中的原始文件名

Exporting textfiles from VCorpus including original file names in R

我对 R 还很陌生,目前正在研究一个项目(年度报告的可读性与性能)。我确实筛选了数百个 post,但找不到合适的解决方案。所以,我被困住了,需要你的帮助。

我的目标 是 tm 大约 1000 个文本文档,将编辑后的文本从 VCorpus 导出到一个文件夹中,包括原始文件名.

到目前为止,我成功导入并进行了(一些)文本挖掘:

### folder of txt files    

dest <- ("C:\Xpdf_pdftotext\TestCorpus")

### create a Corpus in R

docs <- VCorpus(DirSource(dest))

### do some text mining of the txt-documents

for (j in seq(docs)) {
  docs[[j]] <- gsub("\d", "", docs[[j]])
  docs[[j]] <- gsub("\b[A-z]\b{3}", "", docs[[j]])
  docs[[j]] <- gsub("\t", "", docs[[j]])
}

以原始文件名导出语料库中的每个文件。 分配新名称时适用于 1 个文件:

writeLines(as.character(docs[1]), con="text1.txt")

我在 post 中找到了元 ID 的命令,但我不知道如何将其包含在我的代码中

docs[[1]]$meta$id

如何有效地从 VCorpus 中导出编辑后的文本文件,包括它们的原始文件名?

感谢您的帮助

其实很简单

如果你像你一样加载了一个语料库,你可以使用 writeCorpus 用一个命令将整个语料库写入磁盘。元标记 ID 需要填写,但在您的情况下,您已经完成了加载数据的方式。

如果我们以 crude 数据集为例,id 已经包括在内:

library(tm)
data("crude")
crude <- as.VCorpus(crude)
# bit of textcleaning
crude <- tm_map(crude, stripWhitespace)
crude <- tm_map(crude, removePunctuation)
crude <- tm_map(crude, content_transformer(tolower))
crude <- tm_map(crude, removeWords, stopwords("english"))

#write to disk in subfolder data
writeCorpus(crude, path = "data/")

# check files
dir("data/")
[1] "127.txt" "144.txt" "191.txt" "194.txt" "211.txt" "236.txt" "237.txt" "242.txt" "246.txt" "248.txt" "273.txt" "349.txt" "352.txt"
[14] "353.txt" "368.txt" "489.txt" "502.txt" "543.txt" "704.txt" "708.txt"

来自 crude 数据集的文件以 ID 作为文件名写入磁盘。