使用R语言将PDF文件转换为文本文件,以进行文本挖掘。

21

我有将近一千份PDF期刊文章存放在一个文件夹中。我需要对整个文件夹中所有文章的摘要进行文本挖掘。目前我正在执行以下操作:

dest <- "~/A1.pdf"

# set path to pdftotxt.exe and convert pdf to text
exe <- "C:/Program Files (x86)/xpdfbin-win-3.03/bin32/pdftotext.exe"
system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = F)

# get txt-file name and open it
filetxt <- sub(".pdf", ".txt", dest)
shell.exec(filetxt)

我正在将一个pdf文件转换为一个.txt文件,然后将摘要复制到另一个.txt文件中并手动汇编它。这项工作很麻烦。

如何从文件夹中读取所有单独的文章并将它们转换为.txt文件,其中每篇文章只包含摘要。可以通过限制每篇文章中摘要和引言之间的内容来完成;但我无法做到。任何帮助都将不胜感激。


这并不是一个真正的R问题。你需要一个从pdf文档中提取文本的工具,而这不是R的设计目标。我投票关闭的原因是因为这是对这样一个工具的隐含呼吁。 - IRTFM
不完全是一个R问题;但是Ben的回答对我很有帮助。谢谢。 - S Das
可能是如何自动将PDF表单字段导出为XML的重复问题。 - James Kingsbery
2个回答

22

是的,正如IShouldBuyABoat指出的那样,这并不是一个真正的R问题,但只需要轻微的调整就可以用R来完成...

使用R将PDF文件转换为txt文件...

# folder with 1000s of PDFs
dest <- "C:\\Users\\Desktop"

# make a vector of PDF file names
myfiles <- list.files(path = dest, pattern = "pdf",  full.names = TRUE)

# convert each PDF file that is named in the vector into a text file 
# text file is created in the same directory as the PDFs
# note that my pdftotext.exe is in a different location to yours
lapply(myfiles, function(i) system(paste('"C:/Program Files/xpdf/bin64/pdftotext.exe"', 
             paste0('"', i, '"')), wait = FALSE) )

从txt文件中提取摘要...

# if you just want the abstracts, we can use regex to extract that part of
# each txt file, Assumes that the abstract is always between the words 'Abstract'
# and 'Introduction'
mytxtfiles <- list.files(path = dest, pattern = "txt",  full.names = TRUE)
abstracts <- lapply(mytxtfiles, function(i) {
  j <- paste0(scan(i, what = character()), collapse = " ")
  regmatches(j, gregexpr("(?<=Abstract).*?(?=Introduction)", j, perl=TRUE))
})

将摘要写入单独的txt文件中...

# write abstracts as txt files 
# (or use them in the list for whatever you want to do next)
lapply(1:length(abstracts),  function(i) write.table(abstracts[i], file=paste(mytxtfiles[i], "abstract", "txt", sep="."), quote = FALSE, row.names = FALSE, col.names = FALSE, eol = " " ))

现在您已经准备好对摘要进行一些文本挖掘。


1
“pdftotext.exe” 是我们需要安装的软件吗? - user7440629
那就是它看起来的样子。 - netskink

8
我们可以使用库pdftools
library(pdftools)
# you can use an url or a path
pdf_url <- "https://cran.r-project.org/web/packages/pdftools/pdftools.pdf"

# `pdf_text` converts it to a list
list_output <- pdftools::pdf_text('https://cran.r-project.org/web/packages/pdftools/pdftools.pdf')

# you get an element by page
length(list_output) # 5 elements for a 5 page pdf

# let's print the 5th
cat(list_output[[5]])
# Index
# pdf_attachments (pdf_info), 2
# pdf_convert (pdf_render_page), 3
# pdf_fonts (pdf_info), 2
# pdf_info, 2, 3
# pdf_render_page, 2, 3
# pdf_text, 2
# pdf_text (pdf_info), 2
# pdf_toc (pdf_info), 2
# pdftools (pdf_info), 2
# poppler_config (pdf_render_page), 3
# render (pdf_render_page), 3
# suppressMessages, 2
# 5

为了从文章中提取摘要,OP选择提取介于AbstractIntroduction之间的内容。
我们会获取CRAN pdf列表并将作者作为AuthorMaintainer之间的文本进行提取(我手动挑选了一些具有兼容格式的文件)。
为此,我们循环遍历我们的url列表,然后提取内容,并将所有的文本合并到每个pdf中,然后使用regex提取相关信息。
urls <- c(pdftools = "https://cran.r-project.org/web/packages/pdftools/pdftools.pdf",
          Rcpp     = "https://cran.r-project.org/web/packages/Rcpp/Rcpp.pdf",
          jpeg     = "https://cran.r-project.org/web/packages/jpeg/jpeg.pdf")

lapply(urls,function(url){
  list_output <- pdftools::pdf_text(url)
  text_output <- gsub('(\\s|\r|\n)+',' ',paste(unlist(list_output),collapse=" "))
  trimws(regmatches(text_output, gregexpr("(?<=Author).*?(?=Maintainer)", text_output, perl=TRUE))[[1]][1])
})

# $pdftools
# [1] "Jeroen Ooms"
# 
# $Rcpp
# [1] "Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey, Qiang Kou, Nathan Russell, Douglas Bates and John Chambers"
# 
# $jpeg
# [1] "Simon Urbanek <Simon.Urbanek@r-project.org>"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接