gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:22:52 33 4
gpt4 key购买 nike

我的文件夹中有近一千篇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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com