且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-07-23 15:53:16

是的,并不是像IShouldBuyABoat所说的,实际上不是一个R问题,但是R只能在很小的扭曲下完成...

Yes, not really an R question as IShouldBuyABoat notes, but something that R can do with only minor contortions...

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

Use R to convert PDF files to txt files...

# 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文件中提取摘要...

Extract only abstracts from txt files...

# 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 into separate txt files...

# 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 = " " ))

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

And now you're ready to do some text mining on the abstracts.