R TermDocumentMatrix 단어 병합

Jmnote (토론 | 기여)님의 2019년 12월 15일 (일) 00:35 판 (새 문서: ==개요== ;R TermDocumentMatrix 용어 병합 <source lang='r'> library(tm) # TermDocumentMatrix() zTdmMergeTerms <- function(tdm, froms, to) { if(!to %in% rownames(tdm)) { t...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

R TermDocumentMatrix 용어 병합
library(tm) # TermDocumentMatrix()

zTdmMergeTerms <- function(tdm, froms, to) {
  if(!to %in% rownames(tdm)) {
    tdm$nrow <- as.integer(tdm$nrow + 1)
    rownames(tdm) <- append(rownames(tdm),to)
  }
  for(from in froms) {
    nms <- rownames(tdm)
    t1 <- tdm[which(nms==from),] + tdm[which(nms==to),]
    rownames(t) <- to
    t2 <- tdm[which(nms!=from & nms !=to),]
    t1$i <- t1$i + t2$nrow
    t2$i <- c(t2$i, t1$i)
    t2$j <- c(t2$j, t1$j)
    t2$v <- c(t2$v, t1$v)
    t2$nrow <- as.integer(t2$nrow + 1)
    rownames(t2) <- append(rownames(t2),to)
    tdm <- t2
  }
  return(tdm)
}

# nms <- rownames(tdm)
# if( length(nms[nms==newterm]) < 1 ) {
#   tdm$nrow <- as.integer(tdm$nrow + 1)
#   rownames(tdm) <- append(rownames(tdm),newterm)
# }
# terms <- terms[!terms %in% rownames(tdm)]
# colnames(tdm2) <- as.numeric(colnames(tdm2)) + ncol(tdm1)
# return(c(tdm1,tdm2))

#### tdm1
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(zTdmMergeTerms(tdm1,"반갑다","안녕"))
## <<TermDocumentMatrix (terms: 3, documents: 3)>>
## Non-/sparse entries: 5/4
## Sparsity           : 44%
## Maximal term length: 3
## Weighting          : term frequency (tf)
## Sample             :
##         Docs
## Terms    1 2 3
##   안녕   2 1 1
##   친구들 0 1 0
##   친구야 0 0 2

inspect(zTdmMergeTerms(tdm1,c("반갑다","안녕","친구야"),"인사"))
## <<TermDocumentMatrix (terms: 2, documents: 3)>>
## Non-/sparse entries: 4/2
## Sparsity           : 33%
## Maximal term length: 3
## Weighting          : term frequency (tf)
## Sample             :
##         Docs
## Terms    1 2 3
##   인사   2 1 3
##   친구들 0 1 0

2 같이 보기

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