查找语料库中单个文档的特定单词的频率 - R,TermDocumentMatrix,TM

Find frequency of specific words for individual documents in corpus - R, TermDocumentMatrix, TM

对于我正在进行的研究项目,我已经将 pdf 文档读入 R,创建了一个语料库和一个 TermDocumentMatrix。我想检查我语料库中每个文档中特定单词的频率。 下面的代码给出了我想要的那种矩阵,其中包含文档中单词的频率,但显然它只处理高频词而不是特定词。

ft <- findFreqTerms(tdm, lowfreq = 100, highfreq = Inf)
as.matrix(opinions.tdm[ft,])

我在另一条评论中找到了下面的代码,它允许搜索特定术语的频率,但是,它会跨文档求和。我如何调整它以便我在每个文档中而不是跨文档搜索特定术语?

library(tm)
data("crude")
crude <- as.VCorpus(crude)
crude <- tm_map(crude, stripWhitespace)
crude <- tm_map(crude, removePunctuation)
crude <- tm_map(crude, content_transformer(tolower))
crude <- tm_map(crude, removeWords, stopwords("english"))


tdm <- TermDocumentMatrix(crude)

# turn tdm into dense matrix and create frequency vector. 
freq <- rowSums(as.matrix(tdm))
freq["crude"]
crude 
   21 
freq["oil"]
oil 
 85 

跳过rowSums部分,只参考矩阵

term_matrix <-as.matrix(tdm)
term_matrix["crude",]
# 127 144 191 194 211 236 237 242 246 248 273 349 352 353 368 489 
#   2   0   2   3   0   2   0   0   0   0   5   2   0   2   0   0 
# 502 543 704 708 
#   0   2   0   1 
term_matrix["oil",]
# 127 144 191 194 211 236 237 242 246 248 273 349 352 353 368 489 
#   5  12   2   1   1   7   3   3   5   9   5   4   5   4   3   4 
# 502 543 704 708 
#   5   3   3   1