我在一个文件夹中有近一千个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
问题,因为ishuldbuyaboat注释,而是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 = " " ))
现在您准备在摘要上进行一些文本挖掘。
我们可以使用库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选择在Abstract
和Introduction
之间提取内容。
我们将列出CRAN
PDF的列表,然后将作者作为Author
和Maintainer
之间的文本提取(我对一些具有兼容格式的人进行了挑选)。
为此,我们循环在我们的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>"