tfidf: compute tf-idf weights from a dfm

Description Usage Arguments Details References See Also Examples

View source: R/dfm_weight.R

Description

Weight a dfm by term frequency-inverse document frequency (tf-idf) using fully sparse methods.

Usage

1
tfidf(x, scheme_tf = "count", scheme_df = "inverse", base = 10, ...)

Arguments

x

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

scheme_tf

scheme for tf; defaults to "count"

scheme_df

scheme for docfreq; defaults to "inverse". Other options to docfreq can be passed through the ellipsis (...).

base

the base for the logarithms in the tf and docfreq calls; default is 10

...

additional arguments passed to docfreq when calling tfidf; these can be used to fix smoothing constants (default values are 0).

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 scheme_tf = "prop".

References

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

See Also

tf, docfreq

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
mydfm <- as.dfm(data_dfm_lbgexample)
head(mydfm[, 5:10])
head(tfidf(mydfm)[, 5:10])
docfreq(mydfm)[5:15]
head(tf(mydfm)[, 5:10])

# replication of worked example from
# https://en.wikipedia.org/wiki/Tf-idf#Example_of_tf.E2.80.93idf
wiki_dfm <- 
    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"))) %>%
    as.dfm()
docfreq(wiki_dfm)
tfidf(wiki_dfm, scheme_tf = "prop") %>% round(digits = 2)

## Not run: 
# comparison with tm
if (requireNamespace("tm")) {
    convert(wiki_dfm, to = "tm") %>% weightTfIdf() %>% as.matrix()
    # same as:
    tfidf(wiki_dfm, base = 2, scheme_tf = "prop")
}

## End(Not run)

quanteda documentation built on Nov. 17, 2017, 7:46 a.m.