我正在将pdf导入R以进行一些文本分析。我有许多pdf文件,它们的名称是它们的出版年份(每年出版一次)。
在导入它们后,我想创建一个TermDocumentMatrix,其第一个术语“ docs”(即tdm的第一列)采用发布年份而不是文档编号。确实,当我创建tdm时,它们会为其分配编号(1、2、3等...)。
有关如何操作的任何想法?我的代码如下。
谢谢!
#creates the list of pdf files to be picked up (from the working directory)
files <- list.files(pattern = "pdf$")
#read the pdf files from the list (number of pages in brackets in front)
new_files <- sapply(files, pdf_text)
#create corpus
new_corp <- Corpus(VectorSource(new_files))
IMF_tdm <- TermDocumentMatrix(new_corp, control = list(removePunctuation = TRUE,
stopwords = TRUE,
tolower = TRUE,
stemming = TRUE,
removeNumbers = TRUE,
bounds = list(global =c(2, Inf))))
答案 0 :(得分:1)
尝试readtext
https://cran.r-project.org/web/packages/readtext/vignettes/readtext_vignette.html,我过去曾用它读取纯文本和CSV文件,它也可以转换和导入PDF。它将在第一列中输出一个带有文档文件名的数据帧,在第二列中输出一个文档字符串的整个文本。
以下是使用与readtext
库一起分发的一些数据文件的小插图示例:
## Read in Universal Declaration of Human Rights pdf files
(rt_pdf <- readtext(paste0(DATA_DIR, "/pdf/UDHR/*.pdf"),
docvarsfrom = "filenames",
docvarnames = c("document", "language"),
sep = "_"))
## readtext object consisting of 11 documents and 2 docvars.
## # data.frame [11 × 4]
## doc_id text document language
## <chr> <chr> <chr> <chr>
## 1 UDHR_chinese.pdf "\"世界人权宣言\n联合国\"..." UDHR chinese
## 2 UDHR_czech.pdf "\"VŠEOBECNÁ \"..." UDHR czech
## 3 UDHR_danish.pdf "\"Den 10. de\"..." UDHR danish
## 4 UDHR_english.pdf "\"Universal \"..." UDHR english
## 5 UDHR_french.pdf "\"Déclaratio\"..." UDHR french
## 6 UDHR_greek.pdf "\"ΟΙΚΟΥΜΕΝΙΚ\"..." UDHR greek
## # ... with 5 more rows