【问题标题】:invalid input '📧' in 'utf8towcs when using tm and pdftools使用 tm 和 pdftools 时在 'utf8towcs 中输入无效的 '📧'
【发布时间】:2017-10-16 01:37:41
【问题描述】:

我的工作进展顺利,但我遇到了一些问题,因为我的一些 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 上完美运行。

【问题讨论】:

    标签: pdf tm xpdf


    【解决方案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")
    

    【讨论】:

      猜你喜欢
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 2018-09-01
      相关资源
      最近更新 更多