It is nice to be able to convert between data structures that are common in the text world.
As the dfm becomes one of the preferred structures it'd be nice to convert from another structure that is common the DocumentTermMatrix & TermDocumentMatrix . Granted, I'm sure you'd prefer people just go from Corpus to dfm in quanteda but sometimes you have one of thes tm structures already because of preference or some pakage output or just to give the user flexibility.
quanteda already provides the as.dfm conversion for matrices and data.frames. I'd like to request extensions of as.dfm.DocumentTermMatrix & as.dfm.TermDocumentMatrix that allow for easy conversion from these data forms. Below I provide the current code I use for such conversion though I realize you're using more sophisticated methods with as_dfm_constructor under the hood. I also get that this flexibility may be in antithesis of the quanteda in which case I'll accept "This is out of scope" as a response. But I figure I might ask and see.
Also, I may have overlooked the fact that quanteda already does this. If this is the case kindly tell me I'm a bone head and the next time we meet up your first beer is on me :-)
Crude code for converting to dfm
as.dfm.DocumentTermMatrix <- function(x, ...){
as.dfm(
Matrix::sparseMatrix(
i = x$i,
j = dtm$j,
x = x$v,
dimnames = list(seq_len(nrow(x)), colnames(x))
)
)
}
It is nice to be able to convert between data structures that are common in the text world.
As the
dfmbecomes one of the preferred structures it'd be nice to convert from another structure that is common theDocumentTermMatrix&TermDocumentMatrix. Granted, I'm sure you'd prefer people just go from Corpus todfmin quanteda but sometimes you have one of thes tm structures already because of preference or some pakage output or just to give the user flexibility.quanteda already provides the
as.dfmconversion for matrices and data.frames. I'd like to request extensions ofas.dfm.DocumentTermMatrix&as.dfm.TermDocumentMatrixthat allow for easy conversion from these data forms. Below I provide the current code I use for such conversion though I realize you're using more sophisticated methods withas_dfm_constructorunder the hood. I also get that this flexibility may be in antithesis of the quanteda in which case I'll accept "This is out of scope" as a response. But I figure I might ask and see.Also, I may have overlooked the fact that quanteda already does this. If this is the case kindly tell me I'm a bone head and the next time we meet up your first beer is on me :-)
Crude code for converting to
dfm