R TermDocumentMatrix 단어 병합 편집하기

경고: 로그인하지 않았습니다. 편집을 하면 IP 주소가 공개되게 됩니다. 로그인하거나 계정을 생성하면 편집자가 사용자 이름으로 기록되고, 다른 장점도 있습니다.

편집을 취소할 수 있습니다. 이 편집을 되돌리려면 아래의 바뀐 내용을 확인한 후 게시해주세요.

최신판 당신의 편집
1번째 줄: 1번째 줄:
==개요==
==개요==
;R TermDocumentMatrix 용어 병합
;R TermDocumentMatrix 용어 병합
* zTdmMergeTerms()
** 1개 또는 여러 개의 기존용어(froms)를 하나의 신규용어(to)로 병합(합산)한다.
** 새로운 용어(to)가 tdm 내에 있어도 되고 없어도 된다.
** 기존용어(froms)에 to가 들어있는 경우 무시된다.
* zTdmMergeTermsGrep()
** 정규식으로 잡히는 용어들을 하나로 병합(합산)한다.


<syntaxhighlight lang='r' notebook hideerr>
==zTdmMergeTerms()==
* 1개 또는 여러 개의 기존용어(froms)를 하나의 신규용어(to)로 병합(합산)한다.
* 새로운 용어(to)가 tdm 내에 있어도 되고 없어도 된다.
* 기존용어(froms)에 to가 들어있는 경우 무시된다.
 
<source lang='r'>
library(tm) # TermDocumentMatrix()
library(tm) # TermDocumentMatrix()


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


#### tdm1
#### tdm1
docs1 = c("안녕, 안녕.", "반갑다, 친구들.", "반갑다, 친구야, 친구야")
docs1 <- c("안녕, 안녕.", "반갑다, 친구들.", "반갑다, 친구야, 친구야")
## docs1 = iconv(docs1, "CP949", "UTF-8") ## 윈도우 R스튜디오 하드코딩 데이터입력시 인코딩 변환
docs1 <- iconv(docs1, "CP949", "UTF-8") # 윈도우 R스튜디오 하드코딩 데이터입력시 인코딩 변환
tdm1 = TermDocumentMatrix(Corpus(VectorSource(docs1)),control=list(removePunctuation=T,stopwords=F))
tdm1 <- TermDocumentMatrix(Corpus(VectorSource(docs1)),control=list(removePunctuation=T,stopwords=F))
inspect(tdm1)
inspect(tdm1)
</syntaxhighlight>
## <<TermDocumentMatrix (terms: 4, documents: 3)>>
<syntaxhighlight lang='r' notebook>
## 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,"반갑다","안녕"))
inspect(zTdmMergeTerms(tdm1,"반갑다","안녕"))
</syntaxhighlight>
## <<TermDocumentMatrix (terms: 3, documents: 3)>>
<syntaxhighlight lang='r' notebook>
## 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("반갑다","안녕","친구야"),"인사"))
inspect(zTdmMergeTerms(tdm1,c("반갑다","안녕","친구야"),"인사"))
</syntaxhighlight>
## <<TermDocumentMatrix (terms: 2, documents: 3)>>
<syntaxhighlight lang='r' notebook>
## Non-/sparse entries: 4/2
# '반갑다', '안녕', '친구야'를 '안녕'으로 간주한다.
## Sparsity          : 33%
inspect(zTdmMergeTerms(tdm1,c("반갑다","안녕","친구야"),"안녕"))
## Maximal term length: 3
</syntaxhighlight>
## Weighting          : term frequency (tf)
<syntaxhighlight lang='r' notebook>
## Sample            :
# '친구'로 시작하는 것들은 '친구'로 간주한다.
##        Docs
inspect(zTdmMergeTermsGrep(tdm1, "^친구", "친구"))
## Terms    1 2 3
</syntaxhighlight>
##  인사  2 1 3
##  친구들 0 1 0
</source>


==같이 보기==
==같이 보기==
69번째 줄: 85번째 줄:


[[분류: R tm]]
[[분류: R tm]]
[[분류: 단어-문서 행렬]]

제타위키에서의 모든 기여는 크리에이티브 커먼즈 저작자표시-동일조건변경허락 3.0 라이선스로 배포된다는 점을 유의해 주세요(자세한 내용에 대해서는 제타위키:저작권 문서를 읽어주세요). 만약 여기에 동의하지 않는다면 문서를 저장하지 말아 주세요.
또한, 직접 작성했거나 퍼블릭 도메인과 같은 자유 문서에서 가져왔다는 것을 보증해야 합니다. 저작권이 있는 내용을 허가 없이 저장하지 마세요!

취소 편집 도움말 (새 창에서 열림)