从多个PDF'中提取多个短语;s同时使用R

从多个PDF'中提取多个短语;s同时使用R,r,for-loop,lapply,pdftools,R,For Loop,Lapply,Pdftools,我在一个表中列出了一个pdf路径列表,我正在尝试对列出的其他pdf路径重复下面的命令。基本上,我只是将pdf文件转换为文件第一页的文本,然后使用关键字搜索命令对该页中的某些短语进行搜索。我可以一次成功完成一个文件,但我有281个文件。我错过了什么 一个PDF文件 my.file当前,即使使用范围运算符,您的结果也不会保留过去的迭代,只保留最后一项,在我检查以确保适当的字段在正确的类中之后,我最后做了以下操作,这起到了作用: PhrasePull<-function(){ DF<-as

我在一个表中列出了一个pdf路径列表,我正在尝试对列出的其他pdf路径重复下面的命令。基本上,我只是将pdf文件转换为文件第一页的文本,然后使用关键字搜索命令对该页中的某些短语进行搜索。我可以一次成功完成一个文件,但我有281个文件。我错过了什么

一个PDF文件
my.file当前,即使使用范围运算符,您的结果也不会保留过去的迭代,只保留最后一项,
在我检查以确保适当的字段在正确的类中之后,我最后做了以下操作,这起到了作用:

PhrasePull<-function(){
DF<-as.data.frame(TotNoMark_clean)
file.names<-DF$Cover_Letter
Result<-data.frame()
for(i in 1:length(file.names)){
    {pdf_pages<-pdf_text(file.names[i])[1]
    pdf_result<-keyword_search(pdf_pages, keyword = c('reason','not being marketed', 'has not marketed', 'will be able to market', 'will market', 'is not marketing', 'available for sale', 'withdrawn from sale', 'commercial marketing', 'commercial distribution', 'target date', 'will be available', 'marketing of this product has been started', 'commercially marketed', 'discontinued', 'launch.', 'not currently marketed', 'unable to market', 'listed in the active section of the Orange Book', 'not currently being manufactured or marketed'), ignore_case = TRUE)
    if (!nrow(pdf_result)) {next}
    pdf_result$Cover_Letter<-file.names[i]
    }
  Result <- bind_rows(Result, pdf_result)
  }
output<<-merge(DF, Result, by = "Cover_Letter", all.x = TRUE)
}

最后,我和我的一个朋友想出了办法。但是是的,这就是问题所在。感谢您花时间回复!为什么这个解决方案不起作用?请告知任何错误。它遵循您的单个PDF文件过程。就在昨天,我自己解决了它,但您建议我使用bind_rows函数是正确的。这是主要问题。我没有机会尝试你的代码,因为我刚才看到了。我会在可能的时候测试它,我会让你知道的!在第一个解决方案中,它只识别for循环中的前3项,因此i的长度为3。另外,当您初始化此行中的空列表“Result”(结果)dfs Hmmmm…时,没有3个截止点:
seq(DF$Cover_Letter)
与您的
1:length(file.names)
相同,这实际上是
1:length(DF$Cover_Letter)
。至于最后一个问题,我认为您混淆了列表的长度和数据帧的nrow。在这里的任一解决方案中,
pdf\u result
是任意行数的数据帧,但将是分配给列表的每个迭代的一个数据帧。请确保运行的数据与您的工作解决方案相同,该解决方案看起来非常相似,但关键字不同,并且没有
next
或循环中不断增长的对象。您执行了我在R中提到的操作是不明智的:不要在循环中增长对象,例如
bind\u rows
。理想情况下,您可以像我建议的那样在tibble列表的循环外运行一次。否则,会导致低效、过多的内存复制。另外,避免范围运算符


DF<-as.data.frame(TotNoMark_clean)
file.names<-DF$Cover_Letter

for(i in 1:length(file.names)){
  {pdf_pages<-pdf_text(file.names[i])[1]
  pdf_result<-keyword_search(pdf_pages, keyword = c('reason','not being marketed', 'available for sale', 'withdrawn from sale', 'commercial distribution', 'target date'))
  pdf_result$Cover_Letter<-file.names[i]
  if (!nrow(pdf_result)) {next}
  }
  Result<<-pdf_result
}
Result<-select(Result, -5)
Result<-merge(DF, Result, by = "Cover_Letter", all.x = TRUE)

    "Error in `$<-.data.frame`(`*tmp*`, "Cover_Letter", value = "//cover-letters/***.pdf") : 
  replacement has 1 row, data has 0"
DF <- as.data.frame(TotNoMark_clean)
# INITIALIZE EMPTY LIST
Result_dfs <- vector(mode="list", length=nrow(DF))

for(i in seq_along(DF$Cover_Letter)) {
  pdf_pages <- pdf_text(DF$Cover_Letter[i])[1]
  pdf_result <- keyword_search(pdf_pages, 
                               keyword = c('reason','not being marketed', 'available for sale', 
                                           'withdrawn from sale', 'commercial distribution', 
                                           'target date'))
  pdf_result$Cover_Letter <- DF$Cover_Letter[i]

  # SAVE TO LIST REGARDLESS OF NROWs 
  Result_dfs[i] <- pdf_result
}

# BIND ALL DFs TOGETHER AND SELECT LAST FIVE COLS
Result <- dplyr::select(dplyr::bind_rows(Result_dfs), -5)

# MERGE TO ORIGINAL
Result <- merge(DF, Result, by = "Cover_Letter", all.x = TRUE)
DF <- as.data.frame(TotNoMark_clean)

Result_dfs <- lapply(DF$Cover_Letter, function(f) {
    pdf_pages <- pdf_text(f)[1]
    pdf_result <- keyword_search(pdf_pages, 
                                 keyword = c('reason','not being marketed', 'available for sale', 
                                             'withdrawn from sale', 'commercial distribution', 
                                             'target date'))
    pdf_result$Cover_Letter <- f
    return(pdf_result)
})

# BIND ALL DFs TOGETHER AND SELECT LAST FIVE COLS
Result <- dplyr::select(dplyr::bind_rows(Result_dfs), -5)

# LEFT JOIN TO ORIGINAL
Result <- dplyr::left_join(DF, Result, by="Cover_Letter")
PhrasePull<-function(){
DF<-as.data.frame(TotNoMark_clean)
file.names<-DF$Cover_Letter
Result<-data.frame()
for(i in 1:length(file.names)){
    {pdf_pages<-pdf_text(file.names[i])[1]
    pdf_result<-keyword_search(pdf_pages, keyword = c('reason','not being marketed', 'has not marketed', 'will be able to market', 'will market', 'is not marketing', 'available for sale', 'withdrawn from sale', 'commercial marketing', 'commercial distribution', 'target date', 'will be available', 'marketing of this product has been started', 'commercially marketed', 'discontinued', 'launch.', 'not currently marketed', 'unable to market', 'listed in the active section of the Orange Book', 'not currently being manufactured or marketed'), ignore_case = TRUE)
    if (!nrow(pdf_result)) {next}
    pdf_result$Cover_Letter<-file.names[i]
    }
  Result <- bind_rows(Result, pdf_result)
  }
output<<-merge(DF, Result, by = "Cover_Letter", all.x = TRUE)
}