R TermDocumentMatrix 단어 추가

1 개요[ | ]

R TermDocumentMatrix 용어 추가
  • 1개 또는 여러 개의 용어를 추가할 수 있다.
  • 단순히 용어(term)를 추가해줄뿐, 문서에서 개수를 세주는 것은 아니다.
  • 따라서 모든 문서에 없는 것(0개가 있는 것)으로 추가된다.
  • 추가하려는 용어들 중 이미 tdm 내에 있는 것은 무시된다.
library(tm) # TermDocumentMatrix()

zTdmAddTerms <- function(tdm, terms) {
  terms <- terms[!terms %in% rownames(tdm)]
  tdm$nrow <- as.integer(tdm$nrow + length(terms))
  rownames(tdm) <- append(rownames(tdm),terms)
  return(tdm)
}

docs1 <- c("안녕, 안녕.", "반갑다, 친구들.", "반갑다, 친구야, 친구야")
docs1 <- iconv(docs1, "CP949", "UTF-8") # 윈도우 R스튜디오 하드코딩 데이터입력시 인코딩 변환
tdm1 <- TermDocumentMatrix(Corpus(VectorSource(docs1)),control=list(removePunctuation=T,stopwords=F))
inspect(tdm1)
## <<TermDocumentMatrix (terms: 4, documents: 3)>>
## Non-/sparse entries: 5/7
## Sparsity           : 58%
## Maximal term length: 3
## Weighting          : term frequency (tf)
## Sample             :
##         Docs
## Terms    1 2 3
##   반갑다 0 1 1
##   안녕   2 0 0
##   친구들 0 1 0
##   친구야 0 0 2

inspect(zTdmAddTerms(tdm1,"아메리카노"))
## <<TermDocumentMatrix (terms: 5, documents: 3)>>
## Non-/sparse entries: 5/10
## Sparsity           : 67%
## Maximal term length: 5
## Weighting          : term frequency (tf)
## Sample             :
##             Docs
## Terms        1 2 3
##   반갑다     0 1 1
##   아메리카노 0 0 0
##   안녕       2 0 0
##   친구들     0 1 0
##   친구야     0 0 2

inspect(zTdmAddTerms(tdm1,c("가가","나나","반갑다","안녕")))
## <<TermDocumentMatrix (terms: 6, documents: 3)>>
## Non-/sparse entries: 5/13
## Sparsity           : 72%
## Maximal term length: 3
## Weighting          : term frequency (tf)
## Sample             :
##         Docs
## Terms    1 2 3
##   가가   0 0 0
##   나나   0 0 0
##   반갑다 0 1 1
##   안녕   2 0 0
##   친구들 0 1 0
##   친구야 0 0 2

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}