R_文書ターム行列作成 のバックアップ(No.1) - アールメカブ

アールメカブ


R_文書ターム行列作成 のバックアップ(No.1)


Rの備忘録

日本語テキストを解析した結果からターム文書行列を作成する.

lsa パッケージでは正しく解析できないので,自作する.

ちなみに lsa パッケージの textmatrix はこんな感じ

textmatrix <- function( mydir, stemming=FALSE, language="german",
    minWordLength=2, minDocFreq=1, stopwords=NULL, 
   vocabulary=NULL ) {
   
   dummy = lapply( dir(mydir, full.names=TRUE), textvector, 
      stemming, language, minWordLength, minDocFreq, stopwords, 
      vocabulary)
   if (!is.null(vocabulary)) {
       dtm = t(xtabs(Freq ~ ., data = do.call("rbind", dummy)))
       result = matrix(0, nrow=length(vocabulary), ncol=ncol(dtm))
       rownames(result) = vocabulary
       result[rownames(dtm),] = dtm[rownames(dtm),]
       colnames(result) = colnames(dtm)
       dtm = result
       gc()
   } else {
       dtm = t(xtabs(Freq ~ ., data = do.call("rbind", dummy)))
   }
   
   environment(dtm) = new.env()
   class(dtm) = "textmatrix"
   
   return ( dtm )
   

}