ML Playground
NLPIntermediateUnsupervised learning

Text similarity with TF-IDF

Available

Compare two texts and see their vector representation and cosine similarity.

Description

Qué haceTurns each text into a list of numbers that gives more weight to words that are frequent in that text but rare across the rest of the documents.

Para qué sirveCompare or search documents by content similarity (search engines, deduplication, article recommendations) without needing a trained language model.

Represents each text as a vector of per-word weights and compares vectors by cosine similarity. A lexical, deterministic pipeline, with no models or embeddings.

Tokenization, TF, IDF, and similarity

Tokenization

Normalizes (Unicode NFC, lowercase) and extracts sequences of letters and numbers.

TF

count / tokens in the document

What proportion of the document that word is.

IDF

ln((1+N) / (1+df)) + 1

How rare the word is across the corpus. Never reaches 0 or infinity.

Cosine similarity

TF-IDF · TF-IDF / (‖a‖ ‖b‖)

Angle between vectors: 1 if identical, 0 if they share no terms.

Intentional:No stemming, lemmatization, or stopwords: every token counts exactly as written (see “Lexical match vs. meaning”).

What each table shows

Query vector

Query terms present in the vocabulary, with count, TF, IDF, and weight.

Ranking and shared terms

Documents sorted by descending similarity; the shared terms explain what that number measures.

Lexical vs. meaning

Only compares exact words

Synonyms (“doctor” → “physician”) don't share a term, even when they describe the same thing — see the dedicated example.

No embeddings

An embeddings model would bring those synonyms closer together; this demo doesn't implement one.

In exchange: transparency

Every number traces back to a word count, cheap to compute, and ideal for keyword search.

Playground

29 / 200 characters. The query is saved in the URL so the link is shareable: don't type sensitive information here.

recipewithpotatoesandeggs

Query vector (TF, IDF, and weight per term)

Query terms with their count, TF, IDF, and TF-IDF weight, sorted from highest to lowest weight.
TermCountTFIDFTF-IDF weight
eggs10.2001.6930.339
potatoes10.2001.6930.339
recipe10.2001.6930.339
with10.2001.6930.339
and10.2001.2880.258

Ranking by cosine similarity

Documents sorted by descending cosine similarity to the query, with the shared terms that explain each score.
#DocumentSimilarityShared terms
1Doc. 2: Recipe for gazpacho with tomato, cucumber, pepper, garlic, and day-old bread.0.354recipewithand
2Doc. 1: How to make a Spanish potato omelette: you need potatoes, eggs, onion, and olive…0.316eggspotatoesand
3Doc. 3: The internal combustion engine converts thermal energy into mechanical energy th…0.000none

Corpus summary

Documents
3
Vocabulary size
37
Max similarity
0.354

Document 2.

Vocabulary (37 terms)

aandbreadcombustionconvertscucumberdayeggsenergyengineforgarlicgazpachohowinternalintomakemechanicalneedoiloldoliveomeletteonionpepperpistonspotatopotatoesrecipespanishthethermalthroughtotomatowithyou

Text, tokens, and weights per document

  • Document 1

    How to make a Spanish potato omelette: you need potatoes, eggs, onion, and olive oil.

    howtomakeaspanishpotatoomeletteyouneedpotatoeseggsonionandoliveoil
    Document 1 terms with their count, TF, IDF, and TF-IDF weight.
    TermCountTFIDFTF-IDF weight
    a10.0671.6930.113
    eggs10.0671.6930.113
    how10.0671.6930.113
    make10.0671.6930.113
    need10.0671.6930.113
    oil10.0671.6930.113
    olive10.0671.6930.113
    omelette10.0671.6930.113
  • Document 2

    Recipe for gazpacho with tomato, cucumber, pepper, garlic, and day-old bread.

    recipeforgazpachowithtomatocucumberpeppergarlicanddayoldbread
    Document 2 terms with their count, TF, IDF, and TF-IDF weight.
    TermCountTFIDFTF-IDF weight
    bread10.0831.6930.141
    cucumber10.0831.6930.141
    day10.0831.6930.141
    for10.0831.6930.141
    garlic10.0831.6930.141
    gazpacho10.0831.6930.141
    old10.0831.6930.141
    pepper10.0831.6930.141
  • Document 3

    The internal combustion engine converts thermal energy into mechanical energy through pistons.

    theinternalcombustionengineconvertsthermalenergyintomechanicalenergythroughpistons
    Document 3 terms with their count, TF, IDF, and TF-IDF weight.
    TermCountTFIDFTF-IDF weight
    energy20.1671.6930.282
    combustion10.0831.6930.141
    converts10.0831.6930.141
    engine10.0831.6930.141
    internal10.0831.6930.141
    into10.0831.6930.141
    mechanical10.0831.6930.141
    pistons10.0831.6930.141

English examples

Each example replaces the current query and documents with a complete collection and its starting query.

Edit documents

3 / 6 documents.

  • 85 / 240 characters

  • 77 / 240 characters

  • 94 / 240 characters

Shortcut: Ctrl/Cmd+Z undoes the last confirmed edit (if focus isn't in a text field).

Related concepts

References