试图在R中获取tf-idf加权

前端之家收集整理的这篇文章主要介绍了试图在R中获取tf-idf加权前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图用tm包来做一些非常基本的文本分析,并得到一些tf-idf分数;我正在运行OS X(尽管我在Debian Squeeze上尝试过相同的结果);我有一个目录(这是我的工作目录),其中有几个文本文件(第一个包含尤利西斯的前三集,第二个包含第三个剧集,如果你必须知道的话).

R版本:2.15.1
SessionInfo()报告关于tm:[1] tm_0.5-8.3

代码相关位

  1. library('tm')
  2. corpus <- Corpus(DirSource('.'))
  3. dtm <- DocumentTermMatrix(corpus,control=list(weight=weightTfIdf))
  4.  
  5. str(dtm)
  6. List of 6
  7. $i : int [1:12456] 1 1 1 1 1 1 1 1 1 1 ...
  8. $j : int [1:12456] 2 10 12 17 20 24 29 30 32 34 ...
  9. $v : num [1:12456] 1 1 1 1 1 1 1 1 1 1 ...
  10. $nrow : int 2
  11. $ncol : int 10646
  12. $dimnames:List of 2
  13. ..$Docs : chr [1:2] "bloom.txt" "telemachiad.txt"
  14. ..$Terms: chr [1:10646] "_--c'est" "_--et" "_--for" "_--goodbye," ...
  15. - attr(*,"class")= chr [1:2] "DocumentTermMatrix" "simple_triplet_matrix"
  16. - attr(*,"Weighting")= chr [1:2] "term frequency" "tf"

你会注意到,加权似乎仍然是默认的术语频率(tf),而不是我想要的加权tf-idf分数.

道歉如果我缺少一些明显的东西,但是基于我阅读的文档,这应该是正常的.毫无疑问,这个错误不在于星星…

解决方法

如果您看到DocumentTermMatrix帮助页面,在本例中,您将看到控件参数是这样指定的:
  1. data(crude)
  2. dtm <- DocumentTermMatrix(crude,control = list(weighting = function(x) weightTfIdf(x,normalize = FALSE),stopwords = TRUE))

因此,使用名称加权的列表元素指定权重,而不是权重.您可以通过传递函数名称自定义函数来指定此权重,如示例所示.但以下工作也是:

  1. data(crude)
  2. dtm <- DocumentTermMatrix(crude,control = list(weighting = weightTfIdf))

猜你在找的HTML相关文章