15

tm パッケージを使用して非常に基本的なテキスト分析を行い、tf-idf スコアを取得しようとしています。OS X を実行しています (ただし、Debian Squeeze でこれを試してみましたが、同じ結果が得られました)。いくつかのテキスト ファイルを含むディレクトリ (作業ディレクトリ) があります (最初のファイルにはUlyssesの最初の 3 つのエピソードが含まれ、2 つ目のファイルには 2 番目の 3 つのエピソードが含まれます)。

R バージョン: 2.15.1 SessionInfo() tm について次のように報告します: [1] tm_0.5-8.3

関連するコードのビット:

library('tm')
corpus <- Corpus(DirSource('.'))
dtm <- DocumentTermMatrix(corpus,control=list(weight=weightTfIdf))

str(dtm)
List of 6
 $ i       : int [1:12456] 1 1 1 1 1 1 1 1 1 1 ...
 $ j       : int [1:12456] 2 10 12 17 20 24 29 30 32 34 ...
 $ v       : num [1:12456] 1 1 1 1 1 1 1 1 1 1 ...
 $ nrow    : int 2
 $ ncol    : int 10646
 $ dimnames:List of 2
  ..$ Docs : chr [1:2] "bloom.txt" "telemachiad.txt"
  ..$ Terms: chr [1:10646] "_--c'est" "_--et" "_--for" "_--goodbye," ...
 - attr(*, "class")= chr [1:2] "DocumentTermMatrix" "simple_triplet_matrix"
 - attr(*, "Weighting")= chr [1:2] "term frequency" "tf"

重み付けは、私が望む重み付けされた tf-idf スコアではなく、デフォルトの用語頻度 (tf) のままであるように見えることに注意してください。

明らかな何かが欠けている場合はお詫びしますが、私が読んだドキュメントに基づいて、これはうまくいくはずです。過ちは、間違いなく、星にあるのではありません...

4

1 に答える 1

24

DocumentTermMatrixヘルプ ページの例を見ると、引数controlが次のように指定されていることがわかります。

data(crude)
dtm <- DocumentTermMatrix(crude,
           control = list(weighting = function(x) weightTfIdf(x, normalize = FALSE),
                          stopwords = TRUE))

したがって、重みはweightingではなくという名前のリスト要素で指定されますweight。例のように、関数名またはカスタム関数を渡すことで、この重み付けを指定できます。しかし、以下も機能します:

data(crude)
dtm <- DocumentTermMatrix(crude, control = list(weighting = weightTfIdf))
于 2013-02-11T21:00:22.687 に答える