ML Models & Algorithms
N-gram Matching
In shortA fuzzy matching method that splits strings into overlapping chunks of n characters or words and scores similarity by how many chunks they share.
N-gram matching is a fuzzy matching method that breaks strings into overlapping chunks of n characters or n words and scores similarity by how many chunks two strings share. It tolerates typos and partial matches, scales well to large lists, and is often the first step before more sophisticated matching.
How it works
An n-gram is a run of n consecutive units. Split “what is string matching in machine learning” into word bigrams and you get “what is”, “is string”, “string matching” and so on; trigrams give “what is string”, “is string matching” and so on. Character n-grams work the same way on letters, which is what makes the method typo-tolerant.
Once each string is a set of n-grams, similarity can be scored as the share of n-grams they have in common, for example Jaccard similarity (shared n-grams divided by all distinct n-grams), or by feeding character n-grams into TF-IDF with cosine similarity. PolyFuzz uses character trigrams with TF-IDF as its default model. Indexing n-grams means you only compare strings that share at least one chunk, which is why the method is so efficient.
How it’s used in SEO and marketing
- Checking which titles or meta descriptions contain a key phrase, or part of it.
- Finding articles that follow a pattern, such as how-to or ultimate-guide pages.
- Near-duplicate and plagiarism detection.
- Normalising hashtags, product names or campaign names.
- Comparing your titles with a competitor’s to see the shared bigrams, trigrams and four-grams.
Example
MLforSEO’s fuzzy matching lesson splits “elephant” into character trigrams: ele, lep, eph, pha, han and ant. The misspelling “elefant” gives ele, lef, efa, fan and ant. They share two of nine distinct trigrams, a Jaccard similarity of about 0.22, while an exact match would score 0. Run the same comparison against a whole product catalogue and the misspelling still finds its closest candidates quickly.
Limitations and things to watch
- No semantics: “cheap flights” and “low-cost airfare” share almost no n-grams.
- The choice of n matters: small n is tolerant but noisy, large n is precise but brittle.
- Very short strings produce few n-grams, so scores jump around.
- Long strings produce many n-grams and become computationally heavy.
Learn it on MLforSEO
Where N-gram Matching shows up on MLforSEO
Learn it in a course
Read about it on the blog
Sources and further reading
Other terms from ML Models & Algorithms
- BERT (Bidirectional Encoder Representations from Transformers)Google's 2018 transformer language model that reads text in both directions to produce context-aware embedding…
- BERTopicA modular topic modelling technique that clusters transformer embeddings and labels each cluster with class-ba…
- BIRCH (Balanced Iterative Reducing and Clustering using Hierarchies)BIRCH (Balanced Iterative Reducing and Clustering using Hierarchies) clusters very large numeric datasets by s…
- BM25A classic ranking function that scores documents by how often query words appear, adjusted for word rarity and…
- Boyer-MooreA fast exact string-search algorithm that compares a pattern from right to left and skips ahead after mismatch…
- c-TF-IDFClass-based TF-IDF: treats each cluster as one document to find the words that best describe it, used by BERTo…
- Centroid-based ClusteringA clustering family that groups data around central points (centroids), each the average of its cluster's memb…
- Convolutional Neural Network (CNN)A deep learning architecture that scans data with small learned filters to detect local patterns; the classic…
- DBSCANDensity-based clustering that groups closely packed points, finds the number of clusters itself and labels iso…
