从CSV文件中获取R文本挖掘文档
问题描述:
首先,我要道歉重复提问8月1 '13。但我不能评论最初的问题,因为我必须有50个声望才能评论我没有的东西。原始问题可以从R text mining documents from CSV file (one row per doc)检索。从CSV文件中获取R文本挖掘文档
我正尝试在R中使用tm包,并且每篇文章摘要的CSV文件都是不同的摘要。我希望每一行都是语料库中的不同文档。我的数据集中有2000行。
我运行下面的代码如以前奔建议:
# 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)
当我检查类:
# checking class
class(docs)
[1] "DocumentTermMatrix" "simple_triplet_matrix"
问题是TM转换不会对此类工作:
# 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, "/|@|\\|").
更换然后它会正常工作。
您必须将您的函数应用于'Corpus'对象而不是'DocumentTermMatrix'。在'corp nicola
@nicola非常感谢。你是完全正确的。我得到它运行。但是,它似乎工作,直到我创建我的dtm。最后的代码是'docs Sahara
这真的取决于你的数据。没有看到它们就无法说出任何事情。一步一步看看你的语料库,看看发生了什么。 – nicola