【问题标题】:Extract Subpart of pdf text in r在r中提取pdf文本的子部分
【发布时间】:2017-09-14 06:14:13
【问题描述】:

我在一个文件夹中有一个 .pdf 文件列表,我想首先访问该文件夹的前两段文本,然后将它们存储在 .csv 文件中,我可以转换 pdf 文本但不能先提取两段。

这是我尝试过的

setwd("D/All_PDF_Files")
install.packages("pdftools")
install.packages("qdapRegex")
library(pdftools)
library(qdapRegex)
All_files=Sys.glob("*.pdf")
txt <- pdf_text("first.pdf")
cat(txt[1])
rm_between(txt, 'This ', '1. ', extract=TRUE)[[1]]

但这给了我“NA

cat(txt[1])的输出为:

"Maharashtra Real Estate Regulatory Authority
                                         REGISTRATION CERTIFICATE OF PROJECT
                                                             FORM 'C'
                                                           [See rule 6(a)]
This registration is granted under section 5 of the Act to the following project under project registration number :
P52100000255
Project: Ganga Legend A3 And B3.., Plot Bearing / CTS / Survey / Final Plot No.: Sr No 305 P , 306 P and 339 P ,
Village Bavdhan Budruk, Taluka Mulashi,District Pune at Pune (M Corp.), Pune City, Pune, 411001;
   1. Goel Ganga Developers (I) Pvt Ltd having its registered office / principal place of business at Tehsil: Pune City,
      District: Pune, Pin: 411001.
   2. This registration is granted subject to the following conditions, namely:­"

我要提取的是文字

This registration is granted under section 5 of the Act to the following project under project registration number :
P52100000255
Project: Ganga Legend A3 And B3.., Plot Bearing / CTS / Survey / Final Plot No.: Sr No 305 P , 306 P and 339 P ,
Village Bavdhan Budruk, Taluka Mulashi,District Pune at Pune (M Corp.), Pune City, Pune, 411001;

有更好的方法吗?

【问题讨论】:

  • 查看textreadr 包中的read_pdf 函数,其中在开始读取数据之前可以跳过行数以满足您的目的
  • 使用 read_pdf::: s=read_pdf("D:/All_PDF_Files/first.pdf", skip = 4, remove.empty = TRUE, trim = TRUE) s$text[1:4] , 在不同的行中给出所有行,而不是在一行中
  • 在上述步骤之后,仅删除包含“Maharastra......”的行不会解决问题吗?

标签: r apply text-extraction qdapregex


【解决方案1】:
library(pdftools)

setwd("D/All_PDF_Files")
All_files=Sys.glob("*.pdf")

df <- data.frame()
for (i in 1:length(All_files))
{
  txt <- pdf_text(All_files[i])
  
  file_name <- All_files[i]
  #skip first 4 header rows (you may need to adjust this count according to your use case)
  FirstPara <- unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[1+4]
  SecondPara <- unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[2+4]
  
  df <- rbind(df, cbind(file_name, FirstPara, SecondPara))
}
df

【讨论】:

  • 这个解决方案提供了 20% 的解决方案......我认为你应该检查我想要提取的预期文本......我想消除前 4 个标题行并只提取这个文本“此注册根据该法案第 5 条授予以下项目注册号:P52100000255 项目:Ganga Legend A3 和 B3..,地块轴承/CTS/勘测/最终地块编号:Sr No 305 P、306 P 和339 P , Village Bavdhan Budruk, Taluka Mulashi,District Pune at Pune (M Corp.), Pune City, Pune, 411001;"
  • 您的代码在三列中生成输出,而不是我希望只有两列“文件名”“文本”和文本列应该包含预期的文本。
  • 我相信如果您打印 strsplit(txt[1], split=c("\r\n", "\r", "\n")) 并查看这 4 个标题行构成了多少个列表项,那么您可以简单地将 4 (在上面的代码中)替换为该数字,它应该会给您想要的结果。
  • 为了在df 中只获得两列,您可以将file_name &lt;- 之后的代码替换为text &lt;- paste(unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[(1:2)+4], collapse = ";"); df &lt;- rbind(df, cbind(file_name, text))
  • 您的回答只是提取“此注册是根据法案第 5 条授予项目注册号:P52100000255 下的以下项目”,而不是预期输出中提到的其他文本。
【解决方案2】:

如果有人需要,请使用@Prem 的代码发布答案。

All_files=Sys.glob("*.pdf")

df <- data.frame()
for (i in 1:length(All_files))
{
  txt <- pdf_text(All_files[i])

  file_name <- All_files[i]

  FirstPara <- unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[1+4]
  SecondPara <- unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[2+4]
  ThirdPara <- unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[3+4]
  ThirdPara_new <- sub("[^:]+:\\s*([^,]+),.*", "\\1",ThirdPara)
  t1=unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[4+4]
  t2=unlist(strsplit(txt[1], split=c("\r\n", "\r", "\n")))[5+4]
  conct=paste(t1,t2)
  FourthPara=gsub(".*1. \\s*|having.*|son.*", "", conct)

  df <- rbind(df, cbind(file_name, SecondPara, ThirdPara_new, FourthPara))

}

【讨论】:

    猜你喜欢
    • 2020-11-26
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    相关资源
    最近更新 更多