在R(tm包)中使用readPDF

时间:2015-08-18 01:03:07

标签: r text-mining xpdf

我是R的初学者,使用tm包时遇到了一些麻烦。我需要从page 55 through 300 of this中提取特定数据,并认为R可能是一种很好的方法。 (如果有人有更好的想法,请告诉我!)我在安装tm包和xpdf包后进行了一些搜索, 我试过阅读this并尝试了zx8754的解决方案但没有运气。我怀疑它与readPDF命令有关 - 我得到以下内容:

  

readPDF出错(PdftotextOptions =" -layout"):未使用的参数   (PdftotextOptions =" -layout")

我认为这与尝试将tm包和xpdf包一起使用有关,因此我阅读了Tony Breyal的解决方案(我无法发布更多信息)比2个链接),将pdfinfo和pdftotext作为环境变量(我在Win 8上)并重新启动。我确定我遗漏了一些东西 - 现在我在R的工作目录中有pdftotext.exe。任何人都可以帮我正确配置,以便tm包正确调用xpdf文件和readPDF函数喜欢它吗?

同样,我对此非常陌生,如果我离开的话,请道歉。非常感谢所有的帮助。

提前致谢,

贾斯汀

1 个答案:

答案 0 :(得分:2)

为了帮助您入门,下面是一个用于阅读PDF文件的完整readPDF命令的示例。当我尝试直接从您提供的链接检索PDF文件时,readPDF引发了错误,因此我首先将PDF文件下载到我的工作目录。

library(tm)

# File name
filename = "ea0607.pdf"

# Read the PDF file
doc <- readPDF(control = list(text = "-layout"))(elem = list(uri = filename),
                                                 language = "en",
                                                 id = "id1")

上面的代码将PDF文件转换为文本,并将结果存储在doc中。 doc实际上是一个列表,可以通过以下代码看到:

str(doc)

List of 2
 $ content: chr [1:23551] "  STATE UNIVERSITY SYSTEM OF FLORIDA" "" "EXPENDITURE ANALYSIS" "      2006-2007" ...
 $ meta   :List of 7
  ..$ author       : chr "greg.jacques"
  ..$ datetimestamp: POSIXlt[1:1], format: "2007-12-10 11:33:48"
  ..$ description  : NULL
  ..$ heading      : chr " PGM=EASUSI-V01                                        STATE UNIVERSITY SYSTEM                                                 "| __truncated__
  ..$ id           : chr "ea0607.pdf"
  ..$ language     : chr "en"
  ..$ origin       : chr "Acrobat PDFMaker 8.1 for Word"
  ..- attr(*, "class")= chr "TextDocumentMeta"
 - attr(*, "class")= chr [1:2] "PlainTextDocument" "TextDocument"

PDF文件的文本存储在doc$content中,而doc$meta包含有关PDF文件的各种元数据。 doc$content的每一行都是PDF文件中的一行。以下是PDF文件的第300行到第310行:

doc$content[300:310]

 [1] ""                                                                                                                      
 [2] "and General (E&G) budget entity. The Expenditure Analysis continues to reflect special units separately and the"       
 [3] ""                                                                                                                      
 [4] "traditional program components and related activities have been further defined to support the funding formula. The"   
 [5] ""                                                                                                                      
 [6] "Expenditure Analysis format was revised in 1995-96 to include all activities in the funding formula as well as college"
 [7] ""                                                                                                                      
 [8] "detail by activity for the UF Health Science Center, the USF Health Science Center and the FSU Medical School. A"      
 [9] ""                                                                                                                      
[10] "definition of each follows:"                                                                                           
[11] ""    

希望这有助于您入门。