我的工作进展顺利,但我遇到了一些问题,因为我的一些pdf文件包含奇怪的符号(“§Ÿ”§“)
我回顾了以前的讨论,但这些解决方案都没有奏效:R tm package invalid input in 'utf8towcs'
这是我到目前为止的代码:
setwd("E:/OneDrive/Thesis/Received comments document/Consultation 50")
getwd()
library(tm)
library(NLP)
library(tidytext)
library(dplyr)
library(pdftools)
files <- list.files(pattern = "pdf$")
comments <- lapply(files, pdf_text)
corp <- Corpus(VectorSource(comments))
corp <- VCorpus(VectorSource(comments));names(corp) <- files
Comments.tdm <- TermDocumentMatrix(corp, control = list(removePunctuation = TRUE,
stopwords = TRUE,
tolower = TRUE,
stemming = TRUE,
removeNumbers = TRUE,
bounds = list(global = c(3, Inf)))) 结果:.tolower(txt)中的错误:'utf8towcs‘中的无效输入’inŸ‘§’
inspect(Comments.tdm[1:32,])
ap_td <- tidy(Comments.tdm)
write.csv(ap_td, file = "Terms 50.csv")任何帮助都是非常感谢的。另外,这段代码在其他pdf上运行得很好。
发布于 2017-05-18 16:47:26
重新看了一下前面的讨论。这个解决方案终于对我起作用了:
myCleanedText <- sapply(myText, function(x) iconv(enc2utf8(x), sub = "byte"))记得遵循Fransisco的指示:“查德的解决方案对我不起作用。我把它嵌入到一个函数中,它给出了一个关于iconv需要一个向量作为输入的错误。所以,我决定在创建语料库之前进行转换。”
我的代码现在看起来像这样:
files <- list.files(pattern = "pdf$")
comments <- lapply(files, pdf_text)
comments <- sapply(comments, function(x) iconv(enc2utf8(x), sub = "byte"))
corp <- Corpus(VectorSource(comments))
corp <- VCorpus(VectorSource(comments));names(corp) <- files
Comments.tdm <- TermDocumentMatrix(corp, control = list(removePunctuation = TRUE,
stopwords = TRUE,
tolower = TRUE,
stemming = TRUE,
removeNumbers = TRUE,
bounds = list(global = c(3, Inf))))
inspect(Comments.tdm[1:28,])
ap_td <- tidy(Comments.tdm)
write.csv(ap_td, file = "Terms 44.csv")https://stackoverflow.com/questions/44010481
复制相似问题