2014-01-30 8 views
14

Ich habe fast tausend PDF-Journal-Artikel in einem Ordner. Ich muss meinen Text in allen Abstracts des Artikels aus dem ganzen Ordner schreiben. Jetzt mache ich folgendes:Verwenden Sie R, um PDF-Dateien in Textdateien für Text-Mining zu konvertieren

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) 

Damit ich eine PDF-Datei auf eine TXT-Datei am Konvertieren und dann das Kopieren der Zusammenfassung in einer anderen .txt-Datei und kompilieren Sie es manuell. Diese Arbeit ist mühsam.

Wie kann ich alle einzelnen Artikel aus dem Ordner lesen und sie in. TXT-Datei, die nur die Zusammenfassung von jedem Artikel enthalten konvertieren. Es kann getan werden, indem der Inhalt zwischen ABSTRACT und EINLEITUNG in jedem Artikel begrenzt wird; aber ich kann das nicht. Jede Hilfe wird geschätzt.

+0

Dies ist nicht wirklich eine R Frage. Sie benötigen ein Dienstprogramm, um Text aus PDF-Dokumenten zu extrahieren, und das ist KEIN Design-Ziel von R. Meine Abstimmung zum Schließen basiert auf der Tatsache, dass dies ein impliziter Aufruf für ein solches Tool ist. –

+0

Nicht ganz eine R-Frage; Aber Bens Antwort hat mir sehr geholfen. Vielen Dank. –

+0

mögliche Duplikate von [Wie man PDF-Formularfelder automatisch in XML exportiert] (http://stackoverflow.com/questions/21009608/how-to-export-pdf-form-fields-to-xml-automatisch) –

Antwort

18

Ja, nicht wirklich eine R Frage IShouldBuyABoat Noten, sondern etwas, das R mit nur geringen Verrenkungen tun kann ...

Verwenden R PDF-Dateien zu konvertieren Dateien in 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)) 

Extract nur aus tXT-Dateien abstrahiert ...

# 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)) 
}) 

schreiben Abstracts in separate txt-Dateien ...

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

Und jetzt sind Sie bereit, einige Text Mining auf den Abstracts zu tun.

+0

Vielen Dank. Daran habe ich gerungen. Danke noch einmal. –

+0

Keine Sorge, froh, dass es funktioniert – Ben

+0

Diese Antwort war sehr hilfreich für mich, danke! – nasim

1

Wir Bibliothek verwenden können 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 

Um Abstracts von Artikeln zu extrahieren, wählt OP-Gehalt zwischen Abstract und Introduction zu extrahieren.

Wir werden eine Liste von CRAN pdfs nehmen und den Autor (en) als Text zwischen Author und Maintainer (I Hand gepflückt ein paar, die ein kompatibles Format hatte) zu extrahieren.

Dazu wir auf unserer URL-Liste Schleife dann extrahieren Sie den Inhalt, alle Texte in eine für jedes PDF zu reduzieren, und extrahieren Sie die relevanten Informationen mit 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]>"