Search code examples
rmetadatapdftk

Parse colon-separated list into data.frame


This question is a follow-up of this.

The following metadata.txt has been generated by: pdftk sample.pdf dump_data > metadata.txt

metadata.txt:

InfoBegin
InfoKey: ModDate
InfoValue: D:20170817080316Z00'00'
InfoBegin
InfoKey: CreationDate
InfoValue: D:20170817080316Z00'00'
InfoBegin
InfoKey: Creator
InfoValue: Adobe Acrobat 7.0
InfoBegin
InfoKey: Producer
InfoValue: Mac OS X 10.9.5 Quartz PDFContext
PdfID0: 76cf9fd41f0778314abfec8b34d8388d
PdfID1: 76cf9fd41f0778314abfec8b34d8388d
NumberOfPages: 612
BookmarkBegin
BookmarkTitle: Contents
BookmarkLevel: 1
BookmarkPageNumber: 11
BookmarkBegin
BookmarkTitle: Preface 
BookmarkLevel: 1
BookmarkPageNumber: 5
BookmarkBegin
BookmarkTitle: Explanatory Note and Abbreviations Used 
BookmarkLevel: 1
BookmarkPageNumber: 7
PageMediaBegin
PageMediaNumber: 1
PageMediaRotation: 0
PageMediaRect: 0 0 405 616
PageMediaDimensions: 405 616

I would like R to read the Table-of-Contents (TOC) information from metadata.txt into a data.frame, starting from the first BookmarkBegin to the BookmarkPageNumber immediately before PageMediaBegin.

The area of interest can be filtered out with the following code:

require(stringi)

connect=file('metadata.txt')
metadata=readLines(connect)

existing_toc=c(min(grep('BookmarkBegin', metadata)),max(grep('BookmarkPageNumber', metadata)))
metadata_toc=metadata[existing_toc[1]:existing_toc[2]]

Removing BookmarkBegin and splitting the strings on each line by every first occurrence of : via:

toc_data=metadata_toc[-grep('BookmarkBegin', metadata_toc)]
toc_data_split=stri_split_fixed(toc_data, ": ", n=2)

lands me with the following list:

[[1]]
[1] "BookmarkTitle" "Contents"     

[[2]]
[1] "BookmarkLevel" "1"            

[[3]]
[1] "BookmarkPageNumber" "11"                

[[4]]
[1] "BookmarkTitle" "Preface "     

[[5]]
[1] "BookmarkLevel" "1"            

[[6]]
[1] "BookmarkPageNumber" "5"                 

[[7]]
[1] "BookmarkTitle"                           
[2] "Explanatory Note and Abbreviations Used "

[[8]]
[1] "BookmarkLevel" "1"            

[[9]]
[1] "BookmarkPageNumber" "7"

How should I continue from here to get a data.frame like so:

structure(list(BookmarkTitle = structure(c(1L, 3L, 2L), .Label = c("Contents", 
"Explanatory Note and Abbreviations Used", "Preface"), class = "factor"), 
    BookmarkLevel = c(1, 1, 1), BookMarkPageNumber = c(11, 5, 
    7)), .Names = c("BookmarkTitle", "BookmarkLevel", "BookMarkPageNumber"
), row.names = c(NA, -3L), class = "data.frame")

                            BookmarkTitle BookmarkLevel
1                                Contents             1
2                                 Preface             1
3 Explanatory Note and Abbreviations Used             1
  BookMarkPageNumber
1                 11
2                  5
3                  7

Solution

  • This base solution will convert metadata_toc to a data frame. First replace each line not having a colon with an empty line. It is now in Debian Control File (DCF) format so read it using read.dcf. Convert the resulting matrix m to a data frame DF and convert the column types to character and numeric.

    metadata_toc[grep(":", metadata_toc, invert = TRUE)] <- ""
    m <- read.dcf(textConnection(metadata_toc))
    DF <- as.data.frame(m, stringsAsFactors = FALSE)
    DF[] <- lapply(DF, type.convert, as.is = TRUE)
    

    giving:

    > DF
                                BookmarkTitle BookmarkLevel BookmarkPageNumber
    1                                Contents             1                 11
    2                                 Preface             1                  5
    3 Explanatory Note and Abbreviations Used             1                  7
    

    Note

    metadata_toc <- c("BookmarkBegin", "BookmarkTitle: Contents", "BookmarkLevel: 1", 
    "BookmarkPageNumber: 11", "BookmarkBegin", "BookmarkTitle: Preface ", 
    "BookmarkLevel: 1", "BookmarkPageNumber: 5", "BookmarkBegin", 
    "BookmarkTitle: Explanatory Note and Abbreviations Used ", "BookmarkLevel: 1", 
    "BookmarkPageNumber: 7")