Skip to contents

Extract TWW (topic-word weights) from a supported topic-model object and return a standardized wide data.table.

Usage

get_tww(x)

Arguments

x

A supported topic-model object. This includes nlp_topic_fit, raw topicmodels fits, raw seededlda fits, raw topicmodels.etm objects, raw text2vec::LDA objects, and already standardized TWW tables.

Value

A data.table with one row per topic, a topic_id column using the Topic### convention, and one column per term.

Examples

dtm <- methods::as(
  Matrix::Matrix(
    matrix(
      c(1, 0, 1,
        1, 1, 0,
        0, 1, 1,
        1, 1, 1),
      nrow = 4,
      byrow = TRUE
    ),
    sparse = TRUE
  ),
  "dgCMatrix"
)
rownames(dtm) <- paste0("doc", 1:4)
colnames(dtm) <- paste0("term", 1:3)

fit <- fit_topic_model(
  dtm,
  engine = "text2vec",
  model = "lda",
  k = 2,
  control = list(fit = list(n_iter = 25, progressbar = FALSE))
)

get_tww(fit)
#>    topic_id     term1     term2     term3
#>      <char>     <num>     <num>     <num>
#> 1: Topic001 1.0000000 0.0000000 0.0000000
#> 2: Topic002 0.1428571 0.4285714 0.4285714