ngram
A `set` subclass providing fuzzy search based on N-grams.
What it is and what it does
NGram is a Python set subclass that indexes items by their character-based N-gram representation (default N=3), enabling fuzzy search by string similarity. When you add items to an NGram set, it pads each item's string representation, splits it into overlapping N-character substrings, and stores associations between those N-grams and the items. To find similar items, you query with a string, and the class ranks results by the ratio of shared to unshared N-grams, returning matches even when the query doesn't exactly match any stored item.
The package is designed for non-string items too—you provide a key function (like `str`) to extract or normalize the string representation before indexing. It does not implement a language model; it is purely a character-level similarity index. The library has been in production use since 2007 but is no longer actively maintained, with the last release in 2021-09-15.
Use it for:
- Implement a typo-tolerant search in a dataset where exact matches fail but similar strings should be found.
- Build a duplicate-detection system that identifies near-duplicate strings by N-gram overlap.
- Create a spell-checker or autocorrect feature that ranks candidate corrections by string similarity.
- Index and search user-provided text where minor spelling variations are common.
- Perform fuzzy matching between datasets to link records that refer to the same entity with slightly different names.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Extends Python's set class to perform fuzzy string matching using N-gram similarity, allowing efficient searches for similar items in a collection.
Yes, if you need lightweight fuzzy string matching in a standalone Python project and can accept that the package is no longer maintained. The library is stable, has no dependencies, and works with current Python versions. However, do not adopt it for security-sensitive applications or if you require ongoing maintenance and updates.
Install
ngram on PyPI
pip
pip install ngramuv
uv add ngrampoetry
poetry add ngramInstalling ngram
Before you install
Low install friction with no runtime dependencies. However, the package is abandoned—last commit was 2021-09-15, over 1794 days ago. While marked Production/Stable and supporting current Python versions, no active maintenance means security or compatibility issues will not be addressed.
License in practice
Licensed under LGPLv3 (copyleft). You may use and modify the package freely, but any derivative work must also be released under a compatible copyleft license. Proprietary or closed-source projects should review copyleft obligations before adopting.
Quickstart
pip install ngram
from ngram import NGram
ng = NGram(items=['apple', 'application', 'apply'])
results = ng.search('aple')
Verify before relying
- Whether the package handles Unicode or non-ASCII strings correctly in modern Python environments.
- Performance characteristics on large datasets or with very long strings.
- Compatibility with recent Python minor versions despite the 'supports_current' classification.
Package facts
| License | LGPL3 (copyleft) |
| Python support | supports the current Python release (>=3.0) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | abandoned — 1,794 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 180,831/month — #10,139 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: ngram-4.0.3-py3-none-any.whl
Keywords: ngram, set, string, text, similarity
Tags
More Text Processing packages
A drop-in replacement for Python's standard…
permissive · top 100 on PyPI
pyparsingpyparsing provides a library for building text…
permissive · top 1,000 on PyPI
docutilsDocutils converts plaintext documentation in…
unclear · top 1,000 on PyPI
RapidFuzzRapidFuzz provides fast fuzzy string matching…
permissive · top 1,000 on PyPI
tinycss2tinycss2 parses CSS strings into token and…
permissive · top 1,000 on PyPI
llama-parseLlamaParse parses complex documents (PDFs,…
permissive · top 1,000 on PyPI
fuzzyset2Performs fuzzy string matching and approximate…
permissive · top 15,000 on PyPI
pysimstringProvides fast approximate string matching and…
unclear · top 15,000 on PyPI
fuzzysearchFinds approximate substring matches in text or…
permissive · top 15,000 on PyPI
strsimpyImplements a dozen string similarity and…
permissive · top 15,000 on PyPI
tfidf-matcherPerforms fast fuzzy string matching on large…
permissive · top 15,000 on PyPI
thefuzzTheFuzz performs fuzzy string matching using…
permissive · top 5,000 on PyPI
fuzzywuzzyFuzzyWuzzy performs fuzzy string matching using…
copyleft · top 5,000 on PyPI
string-grouperString Grouper finds groups of similar strings…
permissive · top 15,000 on PyPI
ppdeepppdeep computes fuzzy hashes (CTPH/ssdeep) to…
permissive · top 15,000 on PyPI
py-tlshGenerates locality-sensitive hashes for fuzzy…
permissive · top 15,000 on PyPI