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

标签 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文件,其中仅包含每篇文章的摘要。可以通过限制每篇文章的摘要和简介之间的内容来完成;但我无法这样做。任何帮助表示赞赏。

最佳答案

是的,实际上不是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 = " " ))


现在,您可以对摘要进行一些文本挖掘了。

关于r - 使用R将PDF文件转换为文本文件以进行文本挖掘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21445659/

相关文章:

r - 使用 R 查找范围内的重叠

r - 使用 rvest 将字段添加到已抓取的表列表中

search - VIM:如何搜索匹配没有特定字符的行?

python - 如何计算 Pandas Dataframe 中的词频 - Python

r - 从 R 中的文本中提取关键字

r - 试图让 tf-idf 加权在 R 中工作

R tm 包 tm.plugin.tags 停止工作

python - R 中的 Collat​​z 猜想

r - 查找组内的原始时间点

linux - R中没有 "tm"的词云