Convert text to vector: N-grams and TF-IDF method

Nlp tutorial · PySpark.in

What are N-grams?

Screenshot 2025-09-14 153728

Applying Bigrams

Vocabulary (extended with bigrams):

Vectors:

 Now the vectors are more different, so ML models can distinguish them better.

Trigrams (3-word combinations)

All possible trigrams in vocabulary:
["boy girl good"]


 Advantages of N-grams

  1. Captures word order & context (unlike plain Bag of Words).

  2. Helps detect semantic differences (e.g., “good” vs. “not good”).

  3. Reduces the risk of treating opposite sentences as similar.

Limitations

  1. Vocabulary explosion – more n-grams → higher dimensionality.

  2. Still doesn’t fully capture meaning (semantics) like word embeddings (Word2Vec, GloVe). In short:

TF–IDF (Term Frequency – Inverse Document Frequency)

Screenshot 2025-09-14 153448

TF–IDF is a method to convert text into numerical vectors that give importance to words based on how often they appear in a document and how unique they are across documents.

 1. Components

(a) Term Frequency (TF)

Measures how often a word appears in a document.

T F ( t , d ) = Number of times word t appears in document d Total words in document d TF(t,d) = \frac{\text{Number of times word t appears in document d}}{\text{Total words in document d}}

(b) Inverse Document Frequency (IDF)

Measures how rare a word is across all documents.

I D F ( t ) = log ⁡ Total number of documents Number of documents containing word t IDF(t) = \log \frac{\text{Total number of documents}}{\text{Number of documents containing word t}}

(c) TF–IDF Score

T F – I D F ( t , d ) = T F ( t , d ) × I D F ( t )

This balances local importance (inside a document) and global rarity (across documents).

 2. Example 

Sentences after preprocessing (lowercased, stopwords removed):

  1. good boy

  2. good girl

  3. boy girl good

Step 1: Vocabulary

[good, boy, girl]

Step 2: Term Frequency (TF)

WordS1 ("good boy")S2 ("good girl")S3 ("boy girl good")
good1/2 = 0.51/2 = 0.51/3 ≈ 0.333
boy1/2 = 0.50/2 = 01/3 ≈ 0.333
girl0/2 = 01/2 = 0.51/3 ≈ 0.333

Step 3: Inverse Document Frequency (IDF)

Step 4: TF–IDF (TF × IDF)

WordS1 VectorS2 VectorS3 Vector
good0.5 × 0 = 00.5 × 0 = 00.333 × 0 = 0
boy0.5 × 0.176 ≈ 0.0880 × 0.176 = 00.333 × 0.176 ≈ 0.059
girl0 × 0.176 = 00.5 × 0.176 ≈ 0.0880.333 × 0.176 ≈ 0.059

Final Sentence Vectors

Why TF–IDF is Better than Bag of Words

 Assigns low weight to common words (e.g., “good”)
 Assigns high weight to rare words (more meaningful)
 Reduces the impact of stopwords / frequent words
 Creates more informative vectors for ML models

 Still, TF–IDF doesn’t capture word meaning or sequence (solved by embeddings like Word2Vec, GloVe, BERT). So in short:

TF–IDF:

 Advantages

  1. Intuitive & easy to implement

    • Works similarly to Bag of Words in terms of vectorization.

    • Input size is fixed, based on vocabulary.

  2. Fixed-size input representation

    • Like Bag of Words, vector length depends only on vocabulary size, not on sentence length.

  3. Captures word importance 

    • Unlike Bag of Words (where all words are equal, just 0/1 or counts), TF–IDF gives different weights to words.

    • Example:

      • Sentences:

        1. good boy

        2. good girl

        3. boy girl good

      • Word good is present in all sentences → IDF = 0 → weight = 0 → considered unimportant.

      • Word boy appears in only some sentences → higher weight → considered important.

      • Word girl is important in its specific context (sentence 2, sentence 3).

    This ensures that rare, meaningful words are emphasized, while common words are ignored.
     Models trained on TF–IDF vectors get better accuracy than with Bag of Words.

 Disadvantages

  1. Sparsity still exists

    • Many values are still zero, because most words don’t appear in every sentence.

    • High-dimensional sparse vectors → harder for some ML models.

  2. OOV (Out-of-Vocabulary) problem

    • Vocabulary is fixed from training data.

    • If a new word appears in test data (not seen in training), it gets ignored (vector position = 0).

 Key Takeaway

More Nlp tutorials

All tutorials · Try the free PySpark compiler · Practice challenges