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

时间:2014-01-30 00:33:07

标签: r text-mining tm pdftotext

我在一个文件夹中有近千篇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文件。可以通过限制每篇文章中的摘要和引言之间的内容来完成;但我无法这样做。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:20)

是的,不是真正的R问题,因为IShouldBuyABoat注释,但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 :(得分:4)

我们可以使用库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之间的文本(我精心挑选了一些兼容格式的文本) )。

为此,我们在我们的网址列表上循环,然后提取内容,将所有文本折叠成每个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>"