create_ttm: Create Term-Term Matrix (Term-Cooccurrence Matrix)

Description Usage Arguments Examples

View source: R/create_ttm.R

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

1
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 x is a matrix, this argument tells whether it is a DTM or a TDM; for the former, a character starting with "D/d", and for the latter, starting with "T/t".

tomatrix

should be logical, whether to output a matrix result. If TRUE, a matrix representing a TTM is returned. If FALSE (default), a list is returned: the first element is a sparse matrix created by package Matrix, with no words, the second element is a character vector of these words.

checks

if x is a matrix, whether to check its validity, that is, whether it is numeric, all values are 0 or positive, there is no NA.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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.

Example output

CHECKING ARGUMENTS
PROCESSING CHARACTER VECTOR
GENERATING CORPUS
PROCESSING CORPUS
MAKING DTM/TDM
DONE
Warning messages:
1: In Sys.setlocale(category = "LC_COLLATE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
2: In Sys.setlocale(category = "LC_CTYPE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
Warning messages:
1: In Sys.setlocale(category = "LC_COLLATE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
2: In Sys.setlocale(category = "LC_CTYPE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
Warning messages:
1: In Sys.setlocale(category = "LC_COLLATE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
2: In Sys.setlocale(category = "LC_CTYPE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
Warning messages:
1: In Sys.setlocale(category = "LC_COLLATE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
2: In Sys.setlocale(category = "LC_CTYPE", s_right_locale) :
  OS reports request to set locale to "zh_CN.UTF-8" cannot be honored
Warning message:
system call failed: Cannot allocate memory 

chinese.misc documentation built on Sept. 13, 2020, 5:13 p.m.