R 文本从 CSV 文件中挖掘文档

R text mining documents from CSV file

首先,我很抱歉要重复 2013 年 8 月 1 日提出的问题。但是我不能对原始问题发表评论,因为我必须有 50 个声誉才能发表评论,而我没有。可以从 R text mining documents from CSV file (one row per doc) 检索原始问题。

我正在尝试使用 R 中的 tm 包,并且有一个文章摘要的 CSV 文件,每一行都是不同的摘要。我希望每一行都是语料库中的不同文档。我的数据集中有 2,000 行。

我 运行 按照 Ben 先前的建议使用以下代码:

# change this file location to suit your machine
file_loc <- "C:/Users/.../docs.csv"
# change TRUE to FALSE if you have no column headings in the CSV
x <- read.csv(file_loc, header = TRUE)
require(tm)
corp <- Corpus(DataframeSource(x))
docs <- DocumentTermMatrix(corp)

当我检查 class:

# checking class
class(docs)
[1] "DocumentTermMatrix"    "simple_triplet_matrix" 

问题是 tm 转换对此不起作用 class:

# Preparing the Corpus
# Simple Transforms
toSpace <- content_transformer(function(x, pattern) gsub(pattern, " ", x))
docs <- tm_map(docs, toSpace, "/")

我收到这个错误:

Error in UseMethod("tm_map", x) : 
no applicable method for 'tm_map' applied to an object of class "c('DocumentTermMatrix', 'simple_triplet_matrix')"

或其他代码:

docs <- tm_map(docs, toSpace, "/|@|nn|")

我得到同样的错误:

Error in UseMethod("tm_map", x) : 
no applicable method for 'tm_map' applied to an object of class "c('DocumentTermMatrix', 'simple_triplet_matrix')"

非常感谢您的帮助。

代码

docs <- tm_map(docs, toSpace, "/|@|nn|")

必须替换为

docs <- tm_map(docs, toSpace, "/|@|\|").

然后就可以正常工作了。