{R version 3.1.2}
I am trying to make a function that can take the name of any txt file in the working directory and automatically read it, remove stop words, and create a word cloud. Here is the code so far:
word_cloud <- function(x){
fileName <- paste("./", x , sep="")
txt = readLines(fileName, warn=FALSE)
myCorpus = Corpus(VectorSource(txt))
myCorpus = tm_map(myCorpus, tolower)
myCorpus = tm_map(myCorpus, removePunctuation)
myCorpus = tm_map(myCorpus, removeNumbers)
myCorpus = tm_map(myCorpus, removeWords, stopwords("english"))
myDTM = TermDocumentMatrix(myCorpus, control =
list(minWordLength = 1))
m = as.matrix(myDTM)
v = sort(rowSums(m), decreasing = TRUE)
set.seed(4363)
wordcloud(names(v), v, min.freq = 50)
}
When it runs down to the line where it assigns myDTM, it throws the error,
"Error in UseMethod("meta", x) : no applicable method for 'meta' applied to an object of class "character""
and the warning:
"In addition: Warning message: In mclapply(unname(content(x)), termFreq, control) : all scheduled cores encountered errors in user code"
What's going on here?
再添加一行代码-
myCorpus <- tm_map(myCorpus, PlainTextDocument)
readLines is likely not creating a length-1 vector like you are assuming it to. When I read the file in It creates a 198 length vector, which VCorpus is going to think is 198 documents. Try adding this after readLines:
paste(txt, collapse = " ") -> txt
When I run your code with that addition it works
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.