cfo*_*ter 14 r text-analysis tf-idf tm
我正在尝试使用tm包进行一些非常基本的文本分析并得到一些tf-idf分数; 我正在运行OS X(虽然我在Debian Squeeze上试过这个但结果相同); 我有一个目录(这是我的工作目录),里面有几个文本文件(第一集包含尤利西斯的前三集,第二集包含第二集,如果你必须知道的话).
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"
Run Code Online (Sandbox Code Playgroud)
你会注意到,加权似乎仍然是默认的术语频率(tf),而不是我想要的加权tf-idf分数.
抱歉,如果我遗漏了一些明显的东西,但根据我读过的文档,这应该有效.毫无疑问,错误不在于星星......
jub*_*uba 23
如果查看DocumentTermMatrix
帮助页面,在示例中,您将看到以control
这种方式指定参数:
data(crude)
dtm <- DocumentTermMatrix(crude,
control = list(weighting = function(x) weightTfIdf(x, normalize = FALSE),
stopwords = TRUE))
Run Code Online (Sandbox Code Playgroud)
因此,使用名为的list元素指定加权weighting
,而不是weight
.您可以通过传递函数名称或自定义函数来指定此权重,如示例中所示.但是以下工作也是如此:
data(crude)
dtm <- DocumentTermMatrix(crude, control = list(weighting = weightTfIdf))
Run Code Online (Sandbox Code Playgroud)