【发布时间】:2016-02-06 00:22:28
【问题描述】:
我使用以下 R 代码来计算 tf-idf:
library(tm)
library(SnowballC)
docs <- c(D1 = "The sky is blue", D2 = "The sun is bright", D3 = "The sun in the sky is bright.")
dd <- Corpus(VectorSource(docs)) #Make a corpus object from a text vector
#Clean the text
dd <- tm_map(dd, stripWhitespace)
dd <- tm_map(dd, content_transformer(tolower))
dd <- tm_map(dd, removeWords, stopwords("english"))
dtm <- TermDocumentMatrix(dd, control = list(weighting = weightTfIdf))
as.matrix(dtm);
我得到的结果如下:
Terms
Docs blue bright sky sun
1 0.7924813 0.0000000 0.2924813 0.0000000
2 0.0000000 0.2924813 0.0000000 0.2924813
3 0.0000000 0.1949875 0.1949875 0.1949875
但是,如果我执行手动计算,结果会不匹配。 我注意到的是,在 R IDF 中计算为 log2(文档总数/其中包含术语 t 的文档数)。
有没有办法在 R 中覆盖从 2 到 10 的对数底? 请推荐
【问题讨论】: