我有一个问题,我知道很多次在这个页面回答,我尝试了所有这些,但不幸的是其中任何一个都不适合我。我是R的新人,我读了很多,但我找不到答案。如果有人能帮助我,我真的很感激。
我把我的代码放在这里错误是“UseMethod中的错误(”meta“,x):没有适用于”meta“的方法应用于类”try-error“的对象”
这是我的代码,
#init
libs <- c("tm" , "plyr" , "class", "wordcloud", "SnowballC" )
lapply(libs, require, character.only = TRUE)
#set options
options(stringsAsFactors = FALSE)
#set parameters
candidates <- c("obama","romney")
pathname <- "/home/sahar/R/sample-text/speeches"
#clean text
cleancorpus <- function(corpus)
{
corpus.tmp <- tm_map(corpus, removeNumbers, lazy = TRUE)
print( corpus.tmp)
corpus.tmp <- tm_map(corpus, removepunctuation, lazy = TRUE)
print( corpus.tmp)
corpus.tmp <- tm_map(corpus.tmp, stripwhitespace, lazy = TRUE )
print( corpus.tmp)
corpus.tmp <- tm_map(corpus.tmp, content_transformer(tolower), lazy = TRUE)
print( corpus.tmp)
#corpus.tmp <- tm_map(corpus.tmp, tolower)
# corpus.tmp <- tm_map(corpus.tmp, PlainTextDocument)
corpus.tmp <- tm_map(corpus.tmp, removewords, stopwords("english"), lazy = TRUE)
print( corpus.tmp)
corpus.tmp <- tm_map(corpus.tmp, stemDocument, lazy = TRUE)
print( corpus.tmp)
# corpus.tmp<- tm_map(corpus.tmp, content_transformer(tolower(x) iconv(x, to='UTF-8-MAC', sub='byte')), mc.cores=1)
# wordcloud(corpus.tmp)
return(corpus.tmp)
}
#build TDM
generateTDM <- function(cand, path)
{
s.dir <- sprintf("%s/%s" , path, cand)
# s.cor <- Corpus(DirSource(directory = s.dir, encoding = "ANSI"))
print(s.dir)
s.cor <- VCorpus(DirSource(directory = s.dir), readerControl = list(reader = readPlain))
print(s.cor)
# s.cor <- tm_map(s.cor,
# content_transformer(function(x) iconv(x, to='UTF-8-MAC', sub='byte')),
# mc.cores=1
# )
s.cor.cl <- cleancorpus(s.cor)
print(s.cor.cl)
s.tdm <- TermDocumentMatrix(s.cor.cl)
print(s.tdm)
s.tdm <- removeSparseTerms(s.tdm, 0.7)
print(s.tdm)
result <- list(name = cand, tdm = s.tdm)
}
tdm <- lapply(candidates, generateTDM, path = pathname)