【发布时间】:2021-04-09 06:21:53
【问题描述】:
我正在使用 R 编程语言。我学习了如何从互联网上获取 pdf 文件并将它们加载到 R 中。例如,下面我将莎士比亚的 3 部不同的书籍加载到 R 中:
library(pdftools)
library(tidytext)
library(textrank)
library(tm)
#1st document
url <- "https://shakespeare.folger.edu/downloads/pdf/hamlet_PDF_FolgerShakespeare.pdf"
article <- pdf_text(url)
article_sentences <- tibble(text = article) %>%
unnest_tokens(sentence, text, token = "sentences") %>%
mutate(sentence_id = row_number()) %>%
select(sentence_id, sentence)
article_words <- article_sentences %>%
unnest_tokens(word, sentence)
article_words_1 <- article_words %>%
anti_join(stop_words, by = "word")
#2nd document
url <- "https://shakespeare.folger.edu/downloads/pdf/macbeth_PDF_FolgerShakespeare.pdf"
article <- pdf_text(url)
article_sentences <- tibble(text = article) %>%
unnest_tokens(sentence, text, token = "sentences") %>%
mutate(sentence_id = row_number()) %>%
select(sentence_id, sentence)
article_words <- article_sentences %>%
unnest_tokens(word, sentence)
article_words_2<- article_words %>%
anti_join(stop_words, by = "word")
#3rd document
url <- "https://shakespeare.folger.edu/downloads/pdf/othello_PDF_FolgerShakespeare.pdf"
article <- pdf_text(url)
article_sentences <- tibble(text = article) %>%
unnest_tokens(sentence, text, token = "sentences") %>%
mutate(sentence_id = row_number()) %>%
select(sentence_id, sentence)
article_words <- article_sentences %>%
unnest_tokens(word, sentence)
article_words_3 <- article_words %>%
anti_join(stop_words, by = "word")
这些文件中的每一个(例如 article_words_1)现在都是一个“tibble”文件。从这里开始,我想将这些转换为“文档术语矩阵”,以便我可以对这些执行文本挖掘和 NLP:
#convert to document term matrix
myCorpus <- Corpus(VectorSource(article_words_1, article_words_2, article_words_3))
tdm <- TermDocumentMatrix(myCorpus)
inspect(tdm)
但这似乎会导致错误:
Error in VectorSource(article_words_1, article_words_2, article_words_3) :
unused arguments (article_words_2, article_words_3)
谁能告诉我我做错了什么?
谢谢
【问题讨论】:
标签: r text nlp text-mining term-document-matrix