An N-gram is a contiguous sequence of N items from a given text or speech sample, where each item is typically a word, character, or token. The "N" represents the number of items in the sequence: a unigram (1-gram) is a single word, a bigram (2-gram) is a pair of consecutive words, a trigram (3-gram) is a set of three consecutive words, and so on. N-grams are a core concept in natural language processing (NLP) and computational linguistics, providing a simple yet effective way to capture local context and patterns in text data.
For example, given the sentence "machine learning is powerful," the bigrams would be: "machine learning," "learning is," and "is powerful." N-gram models work on the assumption that the probability of a word depends primarily on the preceding N-1 words, a simplification known as the Markov assumption. Despite their simplicity, N-gram models have been widely used in language modeling, text prediction, spell checking, machine translation, and speech recognition. Before the deep learning era, N-gram-based statistical models were the dominant approach for many NLP tasks.
In modern applications, N-grams remain valuable in several areas. Search engines use N-gram analysis to improve query understanding and suggest completions. Text classification systems use N-gram features (often combined with TF-IDF weighting) to represent documents as numerical vectors. Plagiarism detection tools compare N-gram overlap between documents. Character-level N-grams are particularly useful for language identification, fuzzy matching, and handling misspellings. Many information retrieval and text mining pipelines still rely on N-gram features alongside or within neural models.
While transformer-based models like GPT and BERT have largely surpassed pure N-gram approaches for complex NLP tasks, understanding N-grams is still useful for text preprocessing, feature engineering, and building efficient lightweight models where full neural approaches would be overkill.