create_ttm {chinese.misc} | R Documentation |
Create Term-Term Matrix (Term-Cooccurrence Matrix)
Description
This is a convenient function to create term-term matrix from document-term matrix, term-document matrix, or a matrix that represents one of the two. Sparse matrix is used to speed up computing. The output can be either a matrix or a sparse matrix.
Usage
create_ttm(x, type = "dtm", tomatrix = FALSE, checks = TRUE)
Arguments
x |
an object of class DocumentTermMatrix or TermDocumentMatrix, or a matrix which has its rownames or colnames as terms. |
type |
if |
tomatrix |
should be logical, whether to output a matrix result. If |
checks |
if |
Examples
x <- c(
"Hello, what do you want to drink?",
"drink a bottle of milk",
"drink a cup of coffee",
"drink some water")
dtm <- corp_or_dtm(x, from = "v", type = "dtm")
ttm1 <- create_ttm(dtm)
ttm2 <- create_ttm(dtm, tomatrix = TRUE)
tdm <- t(dtm)
ttm3 <- create_ttm(tdm)
ttm_sparse <- ttm3[[1]]
ttm_ordinary <- as.matrix(ttm_sparse)
colnames(ttm_ordinary) <- ttm3[[2]]
rownames(ttm_ordinary) <- ttm3[[2]]
# You can also use Matrix::writeMM(ttm_sparse, filename)
# to write it on your disk.