从 R 中的 dtm 中按每个文档的频率提取顶级特征 [英] Extract top features by frequency per document from a dtm in R

查看:44
本文介绍了从 R 中的 dtm 中按每个文档的频率提取顶级特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 dtm,想从文档术语矩阵中按频率提取每个文档的前 5 个术语.

I have a dtm and want to extract the top 5 terms by frequency for each document from the document term matrix.

我有一个使用 tm 包构建的 dtm

I have a dtm built using the tm package

  Terms                     
Docs aaaa aac abrt abused accept accepted
1 0 0 0 0 0 0 
2 0 0 0 0 0 0
3 0 0 0 0 0 0
4 0 0 0 0 0 0
5 0 0 0 0 0 0
6 0 0 0 0 0 0

所需的输出应采用以下形式:

Id   
1   Term1 Term2 Term3 Term4 Term5
2   Term1 Term2 Term3 Term4 Term5
and so on for all the documents.

我已经尝试了从 stackoverflow 和其他来源获得的所有解决方案像 制作数据框在 R 中使用 tm 包的多个语料库的前 N ​​个频繁术语(转换为 tdm 并尝试带入输出形式但没有用)和其他但注意似乎有效.

I have tried all the solutions available from stackoverflow ans other sources like Make dataframe of top N frequent terms for multiple corpora using tm package in R (converted to tdm and tried to bring to the output form but did not work)and others but noting seem to work.

推荐答案

使用 Quanteda:

Using Quanteda:

library(quanteda)
txt <- c("hello world world fizz", "foo bar bar buzz")
dfm <- dfm(txt)
topfeatures(dfm, n = 2, groups = seq_len(ndoc(dfm)))
# $`1`
# world hello 
# 2     1 
# 
# $`2`
# bar foo 
# 2   1 

您还可以在 DocumentTermMatrixdfm 之间进行转换.

You can also convert between DocumentTermMatrix and dfm.

或者使用经典的tm:

library(tm)
packageVersion("tm")
# [1] ‘0.7.1’
txt <- c(doc1="hello world world", doc2="foo bar bar fizz buzz")
dtm <- DocumentTermMatrix(Corpus(VectorSource(txt)))
n <- 5
(top <- findMostFreqTerms(dtm, n = n))
# $doc1
# world hello 
# 2     1 
# 
# $doc2
# bar buzz fizz  foo 
# 2    1    1    1 
do.call(rbind, lapply(top, function(x) { x <- names(x);length(x)<-n;x }))
# [,1]    [,2]    [,3]   [,4]  [,5]
# doc1 "world" "hello" NA     NA    NA  
# doc2 "bar"   "buzz"  "fizz" "foo" NA 

findMostFreqTermstm 版本起可用0.7-1.

这篇关于从 R 中的 dtm 中按每个文档的频率提取顶级特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆