15

我正在尝试使用 tm 包进行一些非常基本的文本分析并获得一些 tf-idf 分数;我正在运行 OS X(尽管我在 Debian Squeeze 上尝试过,结果相同);我有一个目录(这是我的工作目录),里面有几个文本文件(第一个包含Ulysses的前三集,第二个包含后三集,如果你必须知道的话)。

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),而不是我想要的加权 tf-idf 分数。

如果我遗漏了一些明显的东西,我深表歉意,但根据我读过的文档,这应该可行。毫无疑问,错误不在于星星......

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 回答