0

我需要将文件夹中的 24 个 PDF 文件转换为 txt 文件,以便对它们进行语义分析。我看了看这个问题,然后从那里开始。但是,在第一次让代码工作之后,我改变了一些东西,现在我收到以下错误:

In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  EOF within quoted string

正因为如此,bodies下面代码的变量中保存的只是一个 24 个空白的列表,我最终得到了 24 个空白文本文件(除了通过将 PDF 转换为 txt 创建的 24 个文本文件之外)。我不确定我做错了什么——在某一时刻,这段代码有效!

我已经查看了我可以找到的有关此错误的信息,但这些都与 相关read.csv,并且他们建议的修复(设置white.space=TRUEquote="")不起作用。

这是代码(错误在第 20-23 行):

# folder with journal articles
PDFfolder_path <- "~/Dropbox/The Egoist PDFs/PDFs"
# vector of PDF file names
PDFfiles <- list.files(path=PDFfolder_path, pattern="*.pdf", full.names=TRUE)
# location of pdftotext.exe file
converter <- "~/Widgets/PDFConverter/bin64/pdftotext"
# folder with text files
textfolder_path <- "~/Dropbox/The Egoist PDFs/textfiles"

# convert PDFs in origin folder into txt files
lapply(PDFfiles, function(i) {
  system(paste(converter, paste0('"', i, '"')), wait=FALSE)
})
# it takes DropBox a bit of time to catch all of the folders
# without this we only end up with 23 txt files for some reason
Sys.sleep(.5)
txtfiles_in_PDFfolder_path <- list.files(path=PDFfolder_path, pattern="*.txt", full.names=TRUE)

# extracting only the Bodies of the articles
bodies <- lapply(txtfiles_in_PDFfolder_path, function(i){
  j <- paste0(scan(i, what = character()),  collapse = " ")
  regmatches(j, gregexpr("(?<=Published).*?(?=Prepaid Advertisements)", j, perl=TRUE))
})

# write article-bodies into txt files
lapply(1:length(bodies), function(i){
  write.table(bodies[i], file=paste(txtfiles_in_PDFfolder_path[i], "body", "txt", sep="."), quote=FALSE, row.names=FALSE, col.names=FALSE, eol=" ")
})

编辑:关于变量结果的更多信息bodies:结果是一个 24 的列表,它采用以下形式(在 R Studio 控制台上,我不确定它的实际名称): bodys: list of 24 : List of 1 ..$ : chr(0) :List of 1 ..$ : chr(0) (重复 24 次)

但我终其一生都无法弄清楚为什么会这样——我认为这与这里chr(0)发生的同样的事情有关——我绝对没有捕捉到所有的台词。

我已经尝试了我能想到的一切,甚至切换readLines()scan(),并且我已经看过这是否会有所帮助。我什至换scan()read.table(),但事实证明,它read.table()本身就依赖scan!所以......我被困住了,我只是在兜圈子。

4

0 回答 0