简体   繁体   English

将txt文件读入R并使用tm包将其转换为wordcloud时出错

[英]Error reading txt file into R and turning it into a wordcloud using the tm package

{R version 3.1.2} {R版本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. 我正在尝试制作一个函数,该函数可以采用工作目录中任何txt文件的名称并自动读取它,删除停用词并创建词云。 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, 当它运行到分配myDTM的行时,将引发错误,

"Error in UseMethod("meta", x) : no applicable method for 'meta' applied to an object of class "character"" “ UseMethod(“ meta”,x)中的错误:没有适用于“ meta”的适用方法应用于类“ character”的对象

and the warning: 和警告:

"In addition: Warning message: In mclapply(unname(content(x)), termFreq, control) : all scheduled cores encountered errors in user code" “另外:警告消息:在mclapply(unname(content(x)),termFreq,control)中:所有调度的内核在用户代码中均遇到错误”

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. readLines可能不会像您假设的那样创建长度为1的向量。 When I read the file in It creates a 198 length vector, which VCorpus is going to think is 198 documents. 当我阅读其中的文件时,它会创建一个198个长度的向量,而VCorpus会认为这是198个文档。 Try adding this after readLines: 尝试在readLines之后添加以下内容:

paste(txt, collapse = " ") -> txt

When I run your code with that addition it works 当我用添加的代码运行您的代码时,它可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM