我正在将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))))
试试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