compute tf-idf weights from a dfm

Description

Compute tf-idf, inverse document frequency, and relative term frequency on document-feature matrices. See also weight.

Usage

1
2
3
4
tfidf(x, ...)

## S3 method for class 'dfm'
tfidf(x, normalize = FALSE, scheme = "inverse", ...)

Arguments

x

object for which idf or tf-idf will be computed (a document-feature matrix)

...

additional arguments passed to docfreq when calling tfidf

normalize

if TRUE, use relative term frequency

scheme

scheme for docfreq

Details

tfidf computes term frequency-inverse document frequency weighting. The default is not to normalize term frequency (by computing relative term frequency within document) but this will be performed if normalize = TRUE.

References

Manning, C. D., Raghavan, P., & Schutze, H. (2008). Introduction to Information Retrieval. Cambridge University Press.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
head(LBGexample[, 5:10])
head(tfidf(LBGexample)[, 5:10])
docfreq(LBGexample)[5:15]
head(tf(LBGexample)[, 5:10])

# replication of worked example from
# https://en.wikipedia.org/wiki/Tf-idf#Example_of_tf.E2.80.93idf
(wikiDfm <- new("dfmSparse", 
                Matrix::Matrix(c(1,1,2,1,0,0, 1,1,0,0,2,3),
                   byrow = TRUE, nrow = 2,  
                   dimnames = list(docs = c("document1", "document2"), 
                     features = c("this", "is", "a", "sample", "another",
                                  "example")), sparse = TRUE)))
docfreq(wikiDfm)
tfidf(wikiDfm)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.