“ML_for_Hackers”[2]

今天在处理文本回归时,遇到一个问题,需要记录。

ranks <- read.csv('/Users/lvxubo/Desktop/ML_for_Hackers-master/06-Regularization/data/oreilly.csv',stringsAsFactors=FALSE)
library('tm')
documents <- data.frame(Text=ranks$Long.Desc.)
row.names(documents) <- 1:nrow(documents)

corpus <- Corpus(DataframeSource(documents))
corpus <- tm_map(corpus,tolower)
corpus <- tm_map(corpus,stripWhitespace)
corpus <- tm_map(corpus,removeWords,stopwords('english'))
corpus <- tm_map(corpus, PlainTextDocument)

dtm <- DocumentTermMatrix(corpus)

如果没有标记的一句代码,会报错:

Error in UseMethod("meta", x) : "meta"没有适用于"character"目标对象的方法
此外: Warning message:
In mclapply(unname(content(x)), termFreq, control) :
  all scheduled cores encountered errors in user code

这是stackoverflow上的解决:

It seems this would have worked just fine in tm 0.5.10 but changes in tm 0.6.0 seems to have broken it. The problem is that the functions tolower and trim won't necessarily return TextDocuments (it looks like the older version may have automatically done the conversion). They instead return characters and the DocumentTermMatrix isn't sure how to handle a corpus of characters.

So you could change to

corpus_clean <- tm_map(news_corpus, content_transformer(tolower))

Or you can run

corpus_clean <- tm_map(corpus_clean, PlainTextDocument)

after all of your non-standard transformations (those not in getTransformations()) are done and just before you create the DocumentTermMatrix. That should make sure all of your data is in PlainTextDocument and should make DocumentTermMatrix happy.

posted @ 2016-05-27 22:10  吕吕吕吕吕  阅读(623)  评论(0编辑  收藏  举报