R_文書ターム行列作成
日本語テキストを解析した結果からターム文書行列を作成する.
lsa パッケージでは正しく解析できないので,自作する.
ちなみに lsa パッケージの textmatrix はこんな感じ.オプションを省けば,やっていることは,指定されたディレクトリ内の文書の一覧を dir() 関数で取り出して,それぞれの文書に lappy() 関数で,textvector 関数を適用している.textvector 関数とは,要するに,ある文書の語彙頻度表を作る関数である.
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 )
}
Link: R_old_tips2(2010d)
Rの備忘録(4047d)
Last-modified: 2008-03-26 (水) 17:07:52 (6140d)