0

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

一个 PDF 文件

    my.file<-"//.../cover-letter.pdf"
    my.page<-pdf_text(my.file)[1] %>% as.character()
    my.result<-keyword_search(my.page, keyword = c('reason','not being marketed', 'available for sale', 'withdrawn from sale', 'commercial distribution', 'target date'), ignore_case = TRUE)
    my.result$Cover_Letter<-my.file
    
    my.result<-select(my.result, -5)
    result<-merge(TotNoMark_clean, my.result, by = "Cover_Letter", all.x = TRUE)

多个 PDF 文件:尝试失败


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"
4

2 回答 2

0

目前,即使您使用范围运算符,您的Result也永远不会仅保留过去的迭代<<-,因为您不使用列表或在循环中增长您的对象(后者是不明智的)。实际上你确实需要<<-,因为for循环不在本地而是在全局对象上运行。如果您的最后一项有空行,next则会导致Result为空。

考虑构建一个数据帧列表,然后bind_rows在循环外运行以获取最终输出:

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)

或者,用于lapply避免初始化列表和分配列表项的簿记:

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")
于 2019-10-07T16:18:01.527 回答
0

在我检查以确保适当的字段位于正确的类中之后,这就是我最终要做的事情,这很有效:

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)
}
于 2019-10-08T19:31:53.543 回答