首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >带有搜索条件的PDF文本提取

带有搜索条件的PDF文本提取
EN

Stack Overflow用户
提问于 2020-06-19 05:55:19
回答 1查看 315关注 0票数 0

我需要从PDF中提取文本,我有一个关键字列表,它告诉我我需要提取哪些文本部分。

PDF看起来如下所示:

  • Schema元素: Keyword1这是my关键字

  • Fontsize: 14我不需要这个

  • Guide以完成架构元素:文本文本。这是我需要的文本,可以在2到3行之间。甚至包含多个句子。

  • Schema元素: Keyword2这是my关键字

  • Fontsize: 18我不需要这个

  • Guide来完成模式元素:文本文本,这是我需要的文本,它可以在2到3行之间长。甚至包含多个句子。此文本与上面的文本不同。

到目前为止,这是我的代码:

代码语言:javascript
代码运行次数:0
运行
复制
library(pdftools)
library(pdfsearch)
library(tidyverse)
pdf <- pdf_text(dir(pattern = "*.pdf")) %>%
read_lines()
Keyword_list <- c("swDisproportionateCost", `"swDisproportionateCostOtherEULegislation", "swExemptionsTransboundary","swDisproportionateCostAlternativeFinancing","swDisproportionateCostAnalysis","swDisproportionateCostScale")`

然后我尝试使用keyword_search,但它只告诉我关键字在哪一行。

我想将草书中的文本提取为我的keyword_list中的一个新列。我认为可以用regex来完成,使用关键字和粗体中的文本作为开始和停止。

这是一个到pdf的链接。https://www.dropbox.com/s/kyyzr5wnh8z87if/FINAL%20Draft4_WFD_Reporting_Guidance_2022_resource_page.pdf?dl=0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-22 13:14:30

这只是一个相当平淡无奇的文本提取工作。做这件事有很多种方法,而且我相信有比这更优雅的方法,但这个方法做的是:

代码语言:javascript
代码运行次数:0
运行
复制
library(pdftools)
library(dplyr)

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

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

df <- tibble(keywords, text)

结果如下:

代码语言:javascript
代码运行次数:0
运行
复制
df
#> # A tibble: 15 x 2
#>    keywords                       text                                                   
#>    <chr>                          <chr>                                                  
#>  1 swExemption44Driver            "Required. Select from the enumeration list the driver~
#>  2 swExemption45Impact            "Required. Select from the enumeration list the impact~
#>  3 swExemption45Driver            "Required. Select from the enumeration list the driver~
#>  4 swDisproportionateCost         "Required. Indicate if disproportionate costs have bee~
#>  5 swDisproportionateCostScale    "Conditional. Select from the enumeration list the  sc~
#>  6 swDisproportionateCostAnalysis "Conditional. Select from the enumeration list the  an~
#>  7 swDisproportionateCostAlterna~ "Conditional. Select from the enumeration list the  al~
#>  8 swDisproportionateCostOtherEU~ "Conditional. Indicate whether the costs of basic  mea~
#>  9 swTechnicalInfeasibility       "Required. Report how ‘technical infeasibility’ has be~
#> 10 swNaturalConditions            "Required. Select from the enumeration list the  eleme~
#> 11 swExemption46                  "Required. Select from the enumeration list the reason~
#> 12 swExemption47                  "Required. Select from the enumeration list the  modif~
#> 13 swExemptionsTransboundary      "Required. Indicate whether the application of  exempt~
#> 14 swExemptionsReference          "Required. Provide references or hyperlinks to the  re~
#> 15 driversSWExemptionsReference   "Required. Provide references or hyperlinks to the  re~
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62464137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档