tests/testthat/test-textstat_entropy.R

test_that("test textstat_entropy is working", {
    skip_if_not_installed("entropy")

    mt <- quanteda::data_dfm_lbgexample
    expect_equal(unname(apply(mt, 1, entropy::entropy, unit = "log2")),
                 textstat_entropy(mt, "documents")[["entropy"]])
    expect_equal(unname(apply(mt, 2, entropy::entropy, unit = "log2")),
                 textstat_entropy(mt, "features")[["entropy"]])
    expect_equal(unname(apply(mt, 1, entropy::entropy, unit = "log10")),
                 textstat_entropy(mt, "documents", base = 10)[["entropy"]])
    expect_equal(unname(apply(mt, 2, entropy::entropy, unit = "log10")),
                 textstat_entropy(mt, "features", base = 10)[["entropy"]])
    expect_is(textstat_entropy(mt, "documents"), "data.frame")
})

test_that("test textstat_entropy works with empty documents or features", {
    mt <- quanteda::data_dfm_lbgexample
    mt[3,] <- 0
    mt[5,] <- 0
    mt <- quanteda::as.dfm(mt)
    expect_silent(textstat_entropy(mt, "documents"))
    expect_silent(textstat_entropy(mt, "features"))
})

Try the quanteda.textstats package in your browser

Any scripts or data that you put into this service are public.

quanteda.textstats documentation built on Nov. 2, 2023, 5:07 p.m.