Skip to content
uencoder

N-gram

An N-gram is a sequence of N consecutive units (characters or words) segmented from text, used in language models, similarity, and text analysis.

Purpose

Decomposes text into fixed-length segments, making statistics and comparison easier.

Mechanism

Slide a window of size N with step 1 over characters or words. For example, the character 2-grams of "hello" are he, el, ll, lo. Used in N-gram language models (predicting the probability of the next unit) and Jaccard/Dice similarity.

Use cases

  • N-gram language models
  • Spelling correction (character n-gram similarity)
  • Text classification and language detection
  • Keyword extraction

Examples

  • Input
    "hello" 2-grams
    Output
    he, el, ll, lo

    Character bigrams

Common errors

Too-large N causes sparsity and overfitting
Choose N based on corpus size (usually 2-3)

Related terms

References