2016-03-28 62 views
1

首先,我要道歉重复提问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')" 

您的帮助将不胜感激。

+0

您必须将您的函数应用于'Corpus'对象而不是'DocumentTermMatrix'。在'corp < - 语料库(DataframeSource(x))'之后,尝试'corp < - tm_map(corp,toSpace,“/”)',然后创建你的'DocumentTermMatrix'。 – nicola

+0

@nicola非常感谢。你是完全正确的。我得到它运行。但是,它似乎工作,直到我创建我的dtm。最后的代码是'docs < - tm_map(docs,stemDocument)'和'inspect(docs [16])''。结果是'内容:字符:1190'这对我来说似乎很好。但是当我创建dtm时,'dim(dtm)'的结果是'[1] 2004 0'。是的,我有2004年的文件,但0?!没有在我的矩阵?!请指教。 – Sahara

+0

这真的取决于你的数据。没有看到它们就无法说出任何事情。一步一步看看你的语料库,看看发生了什么。 – nicola

回答

0

代码

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

必须

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

更换然后它会正常工作。