Skip to content

Commit

Permalink
Define as phrase for dictionary2
Browse files Browse the repository at this point in the history
  • Loading branch information
koheiw committed Mar 26, 2022
1 parent 1b28792 commit 1533fca
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ S3method(as.list,tokens)
S3method(as.matrix,dfm)
S3method(as.phrase,collocations)
S3method(as.phrase,default)
S3method(as.phrase,dictionary2)
S3method(as.phrase,list)
S3method(as.phrase,tokens)
S3method(as.tokens,default)
Expand Down
6 changes: 6 additions & 0 deletions R/phrases.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ phrase.collocations <- function(x, separator = " ") {
as.phrase(x)
}

#' @noRd
#' @export
as.phrase.dictionary2 <- function(x) {
phrase(unlist(x, use.names = TRUE), separator = " ")
}

#' @noRd
#' @export
as.phrase.list <- function(x) {
Expand Down
11 changes: 5 additions & 6 deletions tests/testthat/test-collocations.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,26 @@ test_that("kwic works as expected with and without collocations phrases", {
stringsAsFactors = FALSE)
class(coll_tri) <- c("collocations", "data.frame")

suppressWarnings({

expect_equal(
as.data.frame(kwic(txt, coll_bi))$keyword,
as.data.frame(kwic(toks_uni, coll_bi))$keyword,
c("a b", "e g", "g h",
"a b", "e g", "g h")
)
expect_equal(
as.data.frame(kwic(txt, coll_tri))$keyword,
as.data.frame(kwic(toks_uni, coll_tri))$keyword,
c("e g h", "e g h")
)

expect_equal(
as.data.frame(kwic(txt, as.phrase(coll_bi)))$keyword,
as.data.frame(kwic(toks_uni, as.phrase(coll_bi)))$keyword,
c("a b", "e g", "g h",
"a b", "e g", "g h")
)
expect_equal(
as.data.frame(kwic(txt, as.phrase(dict_bi)))$keyword,
as.data.frame(kwic(toks_uni, as.phrase(dict_bi)))$keyword,
c("a b", "a b")
)
})

expect_equal(nrow(kwic(toks_uni, coll_bi)), 6)
expect_equal(nrow(kwic(toks_uni, coll_tri)), 2)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-default-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ test_that("kwic default works", {
test_that("phrase default works", {
expect_error(
phrase(TRUE),
"phrase\\(\\) only works on character.*dictionary2"
"phrase\\(\\) only works on character, collocations, dictionary2, list, tokens objects"
)

expect_error(
as.phrase(TRUE),
"phrase\\(\\) only works on collocations, list, tokens objects"
"phrase\\(\\) only works on collocations, dictionary2, list, tokens objects"
)
})

Expand Down
21 changes: 13 additions & 8 deletions tests/testthat/test-phrases.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ test_that("test phrase for character", {
})

test_that("test phrase for dictionaries", {
dict <- dictionary(list(country = c("United States"),
dict1 <- dictionary(list(country = c("United States"),
institution = c("Congress", "feder* gov*")))
expect_equivalent(
phrase(dict),
phrase(dict1),
list(c("united", "states"), c("congress"), c("feder*", "gov*"))
)
})

test_that("test phrase for dictionaries", {
dict <- dictionary(list(country = c("United States"),
institution = c("Congress", "feder* gov*")))
dict2 <- dictionary(list(country = c("United+States"),
institution = c("Congress", "feder*+gov*")),
separator = "+")
expect_equivalent(
phrase(dict),
phrase(dict2),
list(c("united", "states"), c("congress"), c("feder*", "gov*"))
)
})
Expand Down Expand Up @@ -63,6 +61,13 @@ test_that("test as.phrase", {
suppressWarnings(phrase(col)),
list(c("a", "b"), c("b", "c"), c("c", "d"), c("a", "b", "c"))
)
dict <- dictionary(list(country = c("United+States"),
institution = c("Congress", "feder*+gov*")),
separator = "+")
expect_equivalent(
phrase(dict),
list(c("united", "states"), c("congress"), c("feder*", "gov*"))
)
})

test_that("helper functions for phrase() work", {
Expand Down

0 comments on commit 1533fca

Please sign in to comment.