"R TermDocumentMatrix 행렬 용어 병합"의 두 판 사이의 차이

16번째 줄: 16번째 줄:
docs <- c("안녕, 안녕.", "반갑다, 친구들.", "반갑다, 친구야, 친구야")
docs <- c("안녕, 안녕.", "반갑다, 친구들.", "반갑다, 친구야, 친구야")
docs <- iconv(docs, "CP949", "UTF-8")
docs <- iconv(docs, "CP949", "UTF-8")
c <- Corpus(VectorSource(docs))
tdm <- TermDocumentMatrix(Corpus(VectorSource(docs)),control=list(
tdm <- TermDocumentMatrix(c,control=list(
   removePunctuation = T,
   removePunctuation = T,
   stopwords = F)
   stopwords = F)
46번째 줄: 45번째 줄:
##  친구 0 1 2
##  친구 0 1 2
</source>
</source>


==같이 보기==
==같이 보기==

2019년 12월 8일 (일) 19:15 판

1 개요

R TermDocumentMatrix 행렬 단어 병합
myTdmMatrixMergeTerms <- function(m, from, to) {
  if(!(from %in% rownames(m))) return(m)
  if(!(to %in% rownames(m))) {
    names <- names(dimnames(m))
    m <- rbind(m, rep(0,ncol(m)))
    rownames(m)[length(rownames(m))] <- to
    names(dimnames(m)) <- names 
  }
  m[to,] <- m[from,] + m[to,]
  m[!rownames(m) %in% from,]
}
docs <- c("안녕, 안녕.", "반갑다, 친구들.", "반갑다, 친구야, 친구야")
docs <- iconv(docs, "CP949", "UTF-8")
tdm <- TermDocumentMatrix(Corpus(VectorSource(docs)),control=list(
  removePunctuation = T,
  stopwords = F)
)
mat <- as.matrix(tdm)
mat
##         Docs
## Terms    1 2 3
##   안녕   2 0 0
##   반갑다 0 1 1
##   친구들 0 1 0
##   친구야 0 0 2

mat <- myTdmMatrixMergeTerms(mat, "반갑다", "안녕")
mat
##         Docs
## Terms    1 2 3
##   안녕   2 1 1
##   친구들 0 1 0
##   친구야 0 0 2

mat <- myTdmMatrixMergeTerms(mat, "친구야", "친구")
mat <- myTdmMatrixMergeTerms(mat, "친구들", "친구")
mat
##       Docs
## Terms  1 2 3
##   안녕 2 1 1
##   친구 0 1 2

2 같이 보기

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