web-dev-qa-db-ja.com

Rを使用してPDFファイルをテキストマイニング用のテキストファイルに変換する

1つのフォルダに約1000の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)

これにより、1つのpdfファイルを1つの.txtファイルに変換し、要約を別の.txtファイルにコピーして手動でコンパイルしています。この作品は面倒です。

フォルダーから個々の記事をすべて読み取り、各記事の要約のみを含む.txtファイルに変換するにはどうすればよいですか。これは、各記事のABSTRACTとINTRODUCTIONの間のコンテンツを制限することで実行できます。しかし、私はそうすることができません。どんな助けでもありがたいです。

17
S Das

はい、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 = " " ))

これで、要約に対してテキストマイニングを実行する準備が整いました。

21
Ben

ライブラリ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はAbstractIntroductionの間のコンテンツを抽出することを選択します。

CRAN pdfsのリストを取得し、著者をAuthorMaintainerの間のテキストとして抽出します(互換性のある形式の一部を厳選しました)。

このために、URLリストでループし、コンテンツを抽出し、すべてのテキストをPDFごとに1つに折りたたみ、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 <[email protected]>"
6