Skip to content

Commit

Permalink
Fix dimnames labels for convert(x, to = "lsa")
Browse files Browse the repository at this point in the history
Add tests for convert(x, to = "lsa")

Solves #526
  • Loading branch information
kbenoit committed Jan 29, 2017
1 parent b82b61b commit 5fdf386
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion R/convert.R
Expand Up @@ -301,8 +301,10 @@ ijv.to.doc <- function(i, j, v) {
#' @keywords internal
dfm2lsa <- function(x) {
x <- as.matrix(x)
# convert to integer
x <- apply(x, c(1,2), as.integer)
class(x) = "textmatrix"
names(dimnames(x)) <- c("terms", "docs")
names(dimnames(x)) <- c("docs", "terms")
t(x)
}

Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-convert.R
Expand Up @@ -43,3 +43,25 @@ test_that("test topicmodels package converter", {
test_that("test austin package converter", {
expect_identical(convert(d, to = "austin"), quanteda::as.wfm(d))
})

test_that("test lsa converter", {
skip_if_not_installed("lsa")
# create some files
td = tempfile()
dir.create(td)
write( c("cat", "dog", "mouse"), file=paste(td, "D1", sep="/") )
write( c("hamster", "mouse", "sushi"), file=paste(td, "D2", sep="/") )
write( c("dog", "monster", "monster"), file=paste(td, "D3", sep="/") )
# read them, create a document-term matrix
lsamat <- lsa::textmatrix(td)

lsamat2 <- convert(dfm(tokens(c(D1 = c("cat dog mouse"),
D2 = c("hamster mouse sushi"),
D3 = c("dog monster monster")))),
to = "lsa")
expect_equivalent(lsamat, lsamat2)

})



0 comments on commit 5fdf386

Please sign in to comment.