Skip to content

Commit

Permalink
Merge pull request #74 from koheiw/add-as.dictionary2
Browse files Browse the repository at this point in the history
Add as.dictionary for future quanteda versions
  • Loading branch information
koheiw authored Feb 21, 2024
2 parents c97e024 + 733c719 commit 10a97bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ Imports:
Suggests:
testthat
Language: en-GB
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-textmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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))

})

0 comments on commit 10a97bd

Please sign in to comment.