liveBook · Manning

Term frequency-inverse document frequency

As explained in AI-Powered Search

TF-IDF is a text-weighting metric that combines term frequency and inverse document frequency to estimate a term's importance within a document collection.

TF-IDF weights terms for text-based relevance ranking. Term frequency (TF) measures how well a term describes a document, while inverse document frequency (IDF) measures how important that term is across the collection. Combining them gives keyword matches different weights instead of treating every occurrence as equally informative. section 3.1.6

A search pipeline can represent the query and each document as numerical vectors, then compare those vectors to produce a relevance score. In the worked calculation, the query “the cat in the hat” becomes repeated term entries, each document receives TF-IDF values for those terms, and the scores are combined with a dot product. section 3.1.6 section 3.4

Ranking “the cat in the hat”

Three documents produce TF-IDF relevance scores of 1.4215 for doc1, 18.9633 for doc2, and 14.2099 for doc3. Doc2 ranks first because it matches the important terms most strongly; doc3 contains all the words but less often, while doc1 contains mostly insignificant words. A Python implementation can tokenize the query, create a vector of ones, calculate each document’s TF-IDF vector, and take the dot product. section 3.1.6

Listing 3.8 Calculating TF-IDF for the query the cat in the hat
def tf_idf(term, doc):
  return TF(term, doc) * IDF(term)**2

query = "the cat in the hat"
terms = tokenize(query)

query_vector = list(numpy.repeat(1, len(terms)))
doc_vectors = [[tf_idf(doc, term) for term in terms] for doc in docs]
doc_scores = [[dot(query_vector, dv)] for dv in doc_vectors]

print_tf_idf_scores(terms, doc_vectors, doc_scores)

Use the executable listing to inspect the TF-IDF function, query vector, document vectors, and score calculation. section 3.1.6

TF-IDF is a foundation for keyword-based search, but many engines use BM25, which retains TF-IDF at its core and adds term-frequency saturation and document-length normalization. These controls often improve ranking over standard TF-IDF cosine similarity. section 3.2.1 section 3.4

How AI-Powered Search covers it

How deep it goes
Defines TF and IDF, works through vector scoring, and stops short of detailed derivation or production tuning.
Examples
Uses the query “the cat in the hat,” three documents, Python vectors, dot products, and comparative relevance scores.
What it assumes
Assumes basic search-ranking and vector-similarity ideas while explaining TF, IDF, tokenization, and scoring progressively.
What you can do after
Apply TF-IDF by constructing query and document vectors and calculating similarity-based relevance scores.
How it connects
Connects term weighting with vector similarity, cosine similarity, keyword ranking, and BM25.

Also explained in

Compare how 10 books explain term frequency-inverse document frequency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Search. The section links point to the parts of the book it draws on.