對于 R 中的 pdf 挖掘任務,我需要你的幫助。
我希望挖掘 1061 個具有檔案名的多頁 pdf 檔案pdf_filenames,為此我想提取每個 pdf 檔案前兩頁的內容。
到目前為止,我已經設法使用庫中的函式和庫中的函式獲取所有 pdf檔案的map內容。purrrpdf_textpdftools
> pdfs = pdf_filenames %>%
map(pdf_text)
這將輸出一個串列,串列中的每個元素代表一個 pdf 檔案。串列的結構pdfs是:
> str(pdfs)
List of 1061
$ : chr [1:3] "Content page 1_pdf1" "Content page 2_pdf1" "Content page 3_pdf1"
$ : chr [1:4] "Content page 1_pdf2" "Content page 2_pdf2" "Content page 3_pdf2" "Content page 4_pdf2"
$ : chr [1:2] "Content page 1_pdf3" "Content page 2_pdf3"
.
.
.
我想要的輸出是:
List of 1061
$ : chr [1:2] "Content page 1_pdf1 Content page 2_pdf1" "Content page 3_pdf1"
$ : chr [1:3] "Content page 1_pdf2 Content page 2_pdf2" "Content page 3_pdf2" "Content page 4_pdf2"
$ : chr [1:1] "Content page 1_pdf3 Content page 2_pdf3"
.
.
.
我試過這個map功能
> pdfs = pdf_filenames %>%
map(pdf_text) %>%
map(c(1,2))
但這回傳了一個空串列。
> pdfs
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
.
.
.
非常感謝您的幫助!謝謝!
uj5u.com熱心網友回復:
我們可以使用 lambda 運算式 ( ~) 分別pdf_text對元素應用,然后paste/str_c是前兩個元素(基于預期輸出)
library(dplyr)
library(pdftools)
library(purrr)
library(stringr)
pdf_filenames %>%
map( ~ {
x1 <- pdf_text(.x)
c(str_c(head(x1, 2), collapse = " "), tail(x1, -2) )
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/428363.html
