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

标签 r text-mining

我一直在使用 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)

关于r - 如何在 R 中的 DTM 中查找词频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28580460/

相关文章:

r - ShinyTree:如果选中复选框,则将变量设置为值

R - 缓慢地对有序因子进行排序

r - 基于分类变量着色时 geom_line 中的问题

R - 仅微调选定的值并使用 geom_text_repel 保持其他值不变

r - 使用 tm-package 进行文本挖掘 - 词干提取

r - 高效的jaccard相似度DocumentTermMatrix

r - 使用 R 和 Rweka 在 termdocument 矩阵中使用 bigrams 而不是单个单词

python-3.x - 'Word2Vec' 对象没有属性 'index2word'

r - 是否有 R 等效的 python 字符串 `format` 函数?

arrays - 如何用 tidyr 替换数组的 reshape2::melt?