R:导入PDF并创建用文件名作为ID的TermDocumentMatrix



我正在将pdf导入r,以进行一些文本分析。我有许多PDF文件,其名称是其出版年度(每年一个出版物)。

我想创建一个TermDocumentMatrix之后,将其导入其第一个术语"文档"(即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/packages/readtext/readtext/vignettes/readtext_vignette.html,我过去曾经使用过它来读取它文件,也可以转换和导入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

最新更新