Извлечение текста из PDF с помощью R - PullRequest
0 голосов
/ 14 июля 2020

Мне нужно извлечь текст из отчета на 400 страниц, и я надеюсь, что вы можете помочь.

Это отчет: https://www.dropbox.com/s/c9zkov79upfom0e/FINAL%20Draft4_WFD_Reporting_Guidance_2022_resource_page.pdf?dl=0

Пока что я смогли извлечь текст, который появляется в PDF-файле 570 раз. Теперь мне нужно извлечь текстовый бит, который появляется только иногда, и соединить его с другим текстом. Стандартная настройка в PDF выглядит следующим образом:

Элемент схемы: Одно слово

Тип поля / фасеты: Несколько

строки

Свойства: Одна строка

Руководство по завершению элемента схемы: Много текста

в нескольких строках

Проверки качества: Это поле появляется только иногда, но если оно появляется, оно должно быть связано с указанными выше полями.

library(pdftools)
library(pdfsearch)
library(tidyverse)
library(xlsx)

keywords <- pdf_text("FINAL Draft4_WFD_Reporting_Guidance_2022_resource_page_JERAF.pdf")
keywords <- keywords %>% strsplit("Schema element:") 
keywords <- keywords %>% lapply(function(x) x[-1]) 
keywords <- keywords %>% lapply(function(x) sapply(strsplit(x, "\r\n"), `[`, 1))
keywords <- keywords %>% unlist 
keywords <- keywords %>% trimws()


text <- pdf_text("FINAL Draft4_WFD_Reporting_Guidance_2022_resource_page_JERAF.pdf")
text <- text %>% strsplit("Guidance on completion of schema element:|Guidance:|Guidance on completion of schema:")
text <- text %>% lapply(function(x) x[-1]) 
text <- text %>% lapply(function(x) sapply(strsplit(x, ":"), `[`, 1))
text <- text[lapply(text,length)>0]
text <- text %>% lapply(function(x) sapply(strsplit(x, "\r\n"), 
                           function(y) paste(y[-length(y)], collapse = ""))) 
text <- text %>% unlist() 
text <- text %>% {gsub("  ", " ", .)} 
text <- text %>% trimws() 
text <- text %>% sapply(`[`, 1)

df <- tibble(keywords, text)

> df
# A tibble: 570 x 2
   keywords                 text                                                                                         
   <chr>                    <chr>                                                                                        
 1 countryCode              Required. Two-letter ISO country code10.                                                     
 2 euRBDCode                Required (except in the RBDSUCA file). Unique EU code  of the River Basin District. Prefix t~
 3 created                  Optional. Date of creation of the dataset.                                                   
 4 creatorElectronicMailAd~ Required. E-mail address of the point of contact in the  organisation responsible for the da~
 5 creatorOrganisationName  Required. Name of the organisation doing the  reporting.                                     
 6 description              Optional. Description of the dataset.                                                        
 7 language                 Required. Code of the language of the dataset.                                               
 8 license                  Required. A legal document giving official permission  to do something with the resource. Pr~
 9 title                    Optional. Name given to the dataset.                                                         
10 rights                   Optional. Information about rights held in and over the  resource. If necessary, provide the~
# ... with 560 more rows

Это дает мне DF со столбцом, содержащим слово из элемент схемы и текст из Руководство по завершению элемента схемы , но теперь я также хотел бы извлечь текст после Тип поля / фасеты , Свойства и Проверки качества

Надеюсь, кто-нибудь может мне помочь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...