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-gramsOutputhe, el, ll, loCharacter bigrams
Common errors
- Too-large N causes sparsity and overfitting
- Choose N based on corpus size (usually 2-3)
Related terms
Tokenization
Tokenization splits text into a sequence of semantic units (words, subwords, characters); it is a foundational step in NLP and search engines. GLOSSARY
TF-IDF
TF-IDF (term frequency-inverse document frequency) measures how important a word is to a document: words frequent in the document but rare in the corpus receive high weights. GLOSSARY
Cosine Similarity
Cosine similarity measures the cosine of the angle between two vectors, ignoring vector length; it is widely used for text-vector similarity computation. GLOSSARY