Quanteda - 通过文档变量将函数应用于 DFM

Quanteda - Apply Function to DFM Over Document Variables

我正在使用 R 的 quanteda 包以及 R 和包的最新版本。我有数以百万计的文档语料库。

假设我有一个从 quanteda 生成的 DFM,每个文档都有日期的 docvar。在给定的一天中生成了数千个文档,但我想按天获取应用于文档的 DFM(这样我就有了每天每个术语的总字数)。我知道 quanteda 是使用 data.table 构建的,因此应该可以执行此操作,但我在 "Getting Started with Quanteda" 或 Whosebug 上几乎没有找到提供干净方法的方法。

有什么建议吗?

你想要 'groups' 参数到 dfm:

> # Add some random dates to an existing corpus
> docvars(data_corpus_inaugural)$date <- rep(as.Date(runif(19, 1, 18000), origin='1970-01-01'), 3)

> dfm_inaugural <- dfm(data_corpus_inaugural, groups='date')
> head(dfm_inaugural)
Document-feature matrix of: 19 documents, 9,215 features (80.8% sparse).
(showing first 6 documents and first 6 features)
            features
docs         fellow citizens  i appear before you
  1970-12-27      4        7 39      2     10  17
  1972-04-25      8       13 29      1      8   8
  1973-08-22      1        3 48      1      6   1
  1973-10-11      2        4 25      0      3   5
  1974-01-05      3        9 57      0      7   2
  1975-04-12      7       21 63      4      6  16