如何在 R 中的 DTM 中查找词频?

How to find term frequency within a DTM in R?

我一直在使用 tm 包创建一个 DocumentTerm 矩阵,如下所示:

library(tm)
library(RWeka)
library(SnowballC)
src <- DataframeSource(data.frame(data3$JobTitle))

# create a corpus and transform data
# Sets the default number of threads to use
options(mc.cores=1)
c_copy <- c <- Corpus(src)
c <- tm_map(c, content_transformer(tolower), mc.cores=1)
c <- tm_map(c,content_transformer(removeNumbers), mc.cores=1)
c <- tm_map(c,removeWords, stopwords("english"), mc.cores=1)
c <- tm_map(c,content_transformer(stripWhitespace), mc.cores=1)

#make DTM
dtm <- DocumentTermMatrix(c, control = list(tokenize = BigramTokenizer))

现在,DTM 运行良好 - 我想要做的是获取 DTM 中频繁出现的术语的频率。显然,我可以使用 findFreqTerms 来获取术语本身,而不是实际频率。 termFreq 仅适用于 TextDocument,不适用于 DTM 或 TDM - 有什么想法吗?

str 的输出 - 频繁项在 $ 项中:

> str(dtm)
List of 6
 $ i       : int [1:190] 1 2 3 4 5 6 7 8 9 10 ...
 $ j       : int [1:190] 1 2 3 4 5 6 7 8 9 10 ...
 $ v       : num [1:190] 1 1 1 1 1 1 1 1 1 1 ...
 $ nrow    : int 119
 $ ncol    : int 146
 $ dimnames:List of 2
  ..$ Docs : chr [1:119] "1" "2" "3" "4" ...
  ..$ Terms: chr [1:146] "account administrator" "account assistant" "account director" "account executive" ...
 - attr(*, "class")= chr [1:2] "DocumentTermMatrix" "simple_triplet_matrix"
 - attr(*, "weighting")= chr [1:2] "term frequency" "tf"

感谢 NicE 的建议 - 它运行良好。添加加权参数可以让我在检查 DTM 时找出术语频率。然后按列总结简单的事情。

dtm <- DocumentTermMatrix(c, control = list(tokenize = BigramTokenizer, weighting=weightTf))
freqs <- as.data.frame(inspect(dtm))
colSums(freqs)

您可以使用 Tyler Rinker 的优秀 qdap 包。 freq_term 函数给出了项及其频率。此示例采用 30 个最频繁的术语,如果它们至少有 4 个字母,并使用 qdap 的停用词包之一——它比英语中内置的 tm 停用词更广泛 (200对比大约 175).

qdap.freq <- freq_terms(dtm, top = 20, at.least = 4, stopwords = Top200Words)