我想创建一个数据框,其中包含我的 pdf 列表中 ech pdf 的所有文本和标题。我制作了一个 for 循环,但是当我打开生成的数据框时,我发现并非每个 pdf 中的所有文本都已处理为文本,而只有最后一页。
这里的代码:
#folder
folder <- paste0(getwd(), "/data/pdfs/")
#QC
qc <- sample(1:length(pdf_list), size = 1, replace = F)
#Create a vector of all the files in the folder selected
file_vector <- list.files(all.files = T, path = folder, recursive = T)
#Apply a filter to the file vector of files
pdf_list <- file_vector[grepl(".pdf", file_vector)]
#make the data frame
corpus_raw <- data.frame("title" = c(),"text" = c())
#list of pdf files comprehensive of path
files <- paste0(folder,pdf_list)
#cycle for the text fetching:
for (i in 1:length(pdf_list)){
#print i so that I know the loop is working right
print(i)
#take the text
text_from_pdf <- pdf_text(pdf = files[i])
temp_store_data <- data.frame("title" = gsub(pattern = "\\d\\/", replacement = "",
x = pdf_list[i], ignore.case = T),
"text" = text_from_pdf, stringsAsFactors = F)
# quality control
if (i == qc[1]){
print(temp_store_data[i,2])
write(temp_store_data[i,2], "data/quality_control.txt")
}
colnames(temp_store_data) <- c("title", "text")
corpus_raw <- rbind(corpus_raw, temp_store_data)
}
你能帮我解决这个问题吗?谢谢!