0

我的工作进展顺利,但我遇到了一些问题,因为我的一些 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' 中的无效输入'📧'

inspect(Comments.tdm[1:32,])

ap_td <- tidy(Comments.tdm)
write.csv(ap_td, file = "Terms 50.csv")

任何帮助深表感谢。ps,此代码在其他 pdf 上完美运行。

4

1 回答 1

1

再看一下前面的讨论。这个解决方案终于对我有用:

myCleanedText <- sapply(myText, function(x) iconv(enc2utf8(x), sub = "byte"))

请记住遵循 Fransisco 的说明:“Chad 的解决方案对我不起作用。我将它嵌入到一个函数中,它给出了一个关于 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")
于 2017-05-18T08:47:26.480 回答