加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 03:48:51 所属栏目:大数据 来源:网络整理
导读:我在一个文件夹中有近千篇pdf期刊文章.我需要在整个文件夹中对所有文章的摘要进行文本处理.现在我正在做以下事情: dest - "~/A1.pdf"# set path to pdftotxt.exe and convert pdf to textexe - "C:/Program Files (x86)/xpdfbin-win-3.03/bin32/pdftotext.e
我在一个文件夹中有近千篇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将PDF文件转换为txt文件…

# folder with 1000s of PDFs
dest <- "C:UsersDesktop"

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读