The NGram class extends the Python ‘set’ class with efficient fuzzy search for members by means of an N-gram similarity measure. It also has static methods to compare a pair of strings.
The N-grams are character based not word-based, and the class does not implement a language model, merely searching for members by string similarity.
See the documentation, which includes a tutorial and release notes.
Use the GitHub issue tracker to report issues.
Installation
To install python-ngram from PyPI:
pip install ngram
How does it work?
The set stores arbitrary items, but for non-string items a key function (such as str) must be specified to provide a string represenation. The key function can also be used to normalise string items (e.g. lower-casing) prior to N-gram indexing.
To index a string it pads the string with a specified dummy character, then splits it into overlapping substrings of N (default N=3) characters in length and associates each N-gram to the items that use it.
To find items similar to a query string, it splits the query into N-grams, collects all items sharing at least one N-gram with the query, and ranks the items by score based on the ratio of shared to unshared N-grams between strings.
History
In 2007, Michel Albert (exhuma) wrote the python-ngram module based on Perl’s String::Trigram module by Tarek Ahmed, and committed the code for 2.0.0b2 to a now-disused Sourceforge subversion repo.
Since late 2008, Graham Poulter has maintained python-ngram, initially refactoring it to build on the set class, and also adding features, documentation, tests, performance improvements and Python 3 support.
Development
Development takes place on Github. On checking out the repo run tox to build the Sphinx documentation and run tests. Run pip install -e . to install the module in editable mode, inside a virtualenv.
Metadata
Release files for ngram 4.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ngram-4.0.3.tar.gz | 28.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ngram-4.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 52.9 kB
Release files / ngram-4.0.3.tar.gz
| Download URL | ngram-4.0.3.tar.gz |
|---|---|
| Size | 28.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
06d1809ee2fedddced6065dcd19826c61318787d47bf4f10b1c011783d419aa6
|
|
BLAKE2b-256 checksum How to use checksums |
dfcce7cc2c12f9a147fedf5dda81b00f6bcbf02a450e9c227949958b2b3e8de2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.11
|
Release files / ngram-4.0.3-py3-none-any.whl
| Download URL | ngram-4.0.3-py3-none-any.whl |
|---|---|
| Size | 24.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7562c74bf4fb658d8368fb6e87116640f90502884920531a5afb6ba42105eb02
|
|
BLAKE2b-256 checksum How to use checksums |
7eae88af04931a13070095e15924430e6e544c29272b5ae377cb13a7c0e48063
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.11
|