From 34cbdc082756735f339bcc98ddb74f56573a5652 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Thu, 22 Feb 2024 06:47:33 +0900 Subject: [PATCH 1/2] Add as.dictionary for future quanteda versions --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/utils.R | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index fc6572a..7674671 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,5 +34,5 @@ Imports: Suggests: testthat Language: en-GB -RoxygenNote: 7.3.0 +RoxygenNote: 7.3.1 Roxygen: list(markdown = TRUE) diff --git a/NAMESPACE b/NAMESPACE index 7144c06..8479ca3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ S3method(as.coefficients_textmodel,data.frame) S3method(as.coefficients_textmodel,matrix) S3method(as.coefficients_textmodel,numeric) +S3method(as.dictionary,textmodel_newsmap) S3method(as.list,textmodel_newsmap) S3method(as.statistics_textmodel,data.frame) S3method(as.statistics_textmodel,matrix) diff --git a/R/utils.R b/R/utils.R index 9025feb..91147dd 100644 --- a/R/utils.R +++ b/R/utils.R @@ -6,6 +6,12 @@ as.list.textmodel_newsmap <- function(x, ...) { lapply(coef(x, ...), names) } +#' @export +#' @method as.dictionary textmodel_newsmap +as.dictionary.textmodel_newsmap <- function(x, ...) { + dictionary(lapply(coef(x, ...), names), separator = x$concatenator) +} + #' @export #' @method print textmodel_newsmap print.textmodel_newsmap <- function(x, ...) { From 733c719cbd40ea552a8091978c6ae6b724bfb345 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Thu, 22 Feb 2024 07:08:34 +0900 Subject: [PATCH 2/2] Add test --- tests/testthat/test-textmodel.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-textmodel.R b/tests/testthat/test-textmodel.R index 7d9ae42..d1730f6 100644 --- a/tests/testthat/test-textmodel.R +++ b/tests/testthat/test-textmodel.R @@ -214,6 +214,7 @@ test_that("coef() and dictionary() are working", { expect_error(coef(map, select = character()), "The length of select must be between 1 and 16") + # TODO: remove as.list() lis1 <- as.list(map) expect_equal(length(lis1), 16) expect_true(all(sapply(lis1, is.character))) @@ -223,4 +224,15 @@ test_that("coef() and dictionary() are working", { expect_equal(names(lis2), c("ru", "fr")) expect_true(all(sapply(lis2, is.character))) expect_true(all(lengths(lis2) == 20)) + + # TODO: change to as.dictionary() + dict1 <- newsmap:::as.dictionary.textmodel_newsmap(map) + expect_s4_class(dict1, "dictionary2") + expect_true(all(lengths(as.list(dict1)) == 10)) + + dict2 <- newsmap:::as.dictionary.textmodel_newsmap(map, n = 20, c("ru", "fr")) + expect_s4_class(dict2, "dictionary2") + expect_equal(names(dict2), c("ru", "fr")) + expect_true(all(lengths(dict2) == 20)) + })