A wrapper on hunspell for use in Python
Project description
CyHunspell
Cython wrapper on Hunspell Dictionary
Description
This repository provides a wrapper on Hunspell to be used natively in Python. The module uses cython to link between the C++ and Python code, with some additional features. There's very little Python overhead as all the heavy lifting is done on the C++ side of the module interface, which gives optimal performance.
The hunspell library will cache any corrections, you can use persistent caching by
adding the use_disk_cache
argument to a Hunspell constructor. Otherwise it uses
in-memory caching.
Installing
For the simplest install simply run:
pip install cyhunspell
This will install the hunspell 1.7.0 C++ bindings on your behalf for your platform.
Dependencies
cacheman -- for (optionally asynchronous) persistent caching
Non-Python Dependencies
hunspell
The library installs hunspell version 1.7.0. As new version of hunspell become available this library will provide new versions to match.
Features
Spell checking & spell suggestions
How to use
Below are some simple examples for how to use the repository.
Creating a Hunspell object
from hunspell import Hunspell
h = Hunspell()
You now have a usable hunspell object that can make basic queries for you.
h.spell('test') # True
Spelling
It's a simple task to ask if a particular word is in the dictionary.
h.spell('correct') # True
h.spell('incorect') # False
This will only ever return True or False, and won't give suggestions about why it might be wrong. It also depends on your choice of dictionary.
Suggestions
If you want to get a suggestion from Hunspell, it can provide a corrected label given a basestring input.
h.suggest('incorect') # ('incorrect', 'correction', corrector', 'correct', 'injector')
The suggestions are in sorted order, where the lower the index the closer to the input string.
Suffix Match
h.suffix_suggest('do') # ('doing', 'doth', 'doer', 'doings', 'doers', 'doest')
Stemming
The module can also stem words, providing the stems for pluralization and other inflections.
h.stem('testers') # ('tester', 'test')
h.stem('saves') # ('save',)
Analyze
Like stemming but return morphological analysis of the input instead.
h.analyze('permanently') # (' st:permanent fl:Y',)
Generate
Generate methods are NOT provided at this time due to the 1.7.0 build not producing any results for any inputs, included the documented one. If this is fixed or someone identifies the issue in the call pattern this will be added to the library in the future.
Bulk Requests
You can also request bulk actions against Hunspell. This will trigger a threaded (without a gil) request to perform the action requested. Currently just 'suggest' and 'stem' are bulk requestable.
h.bulk_suggest(['correct', 'incorect'])
# {'incorect': ('incorrect', 'correction', 'corrector', 'correct', 'injector'), 'correct': ('correct',)}
h.bulk_suffix_suggest(['cat', 'do'])
# {'do': ('doing', 'doth', 'doer', 'doings', 'doers', 'doest'), 'cat': ('cater', 'cats', "cat's", 'caters')}
h.bulk_stem(['stems', 'currencies'])
# {'currencies': ('currency',), 'stems': ('stem',)}
h.bulk_analyze(['dog', 'permanently'])
# {'permanently': (' st:permanent fl:Y',), 'dog': (' st:dog',)}
By default it spawns number of CPUs threads to perform the operation. You can overwrite the concurrency as well.
h.set_concurrency(4) # Four threads will now be used for bulk requests
Dictionaries
You can also specify the language or dictionary you wish to use.
h = Hunspell('en_CA') # Canadian English
By default you have the following dictionaries available
- en_AU
- en_CA
- en_GB
- en_NZ
- en_US
- en_ZA
However you can download your own and point Hunspell to your custom dictionaries.
h = Hunspell('en_GB-large', hunspell_data_dir='/custom/dicts/dir')
Adding Dictionaries
You can also add new dictionaries at runtime by calling the add_dic method.
h.add_dic(os.path.join(PATH_TO, 'special.dic'))
Adding words
You can add individual words to a dictionary at runtime.
h.add('sillly')
Furthermore you can attach an affix to the word when doing this by providing a second argument
h.add('silllies', "is:plural")
Removing words
Much like adding, you can remove words.
h.remove(word)
Asynchronous Caching
If you want to have Hunspell cache suggestions and stems you can pass it a directory to house such caches.
h = Hunspell(disk_cache_dir='/tmp/hunspell/cache/dir')
This will save all suggestion and stem requests periodically and in the background. The cache will fork after a number of new requests over particular time ranges and save the cache contents while the rest of the program continues onward. Yo'll never have to explicitly save your caches to disk, but you can if you so choose.
h.save_cache()
Otherwise the Hunspell object will cache such requests locally in memory and not persist that memory.
Language Preferences
- Google Style Guide
- Object Oriented (with a few exceptions)
Known Workarounds
- On Windows very long file paths, or paths saved in a different encoding than the system require special handling by Hunspell to load dictionary files. To circumvent this on Windows setups, either set
system_encoding='UTF-8'
in theHunspell
constructor or set the environment variableHUNSPELL_PATH_ENCODING=UTF-8
. Then you must re-encode yourhunspell_data_dir
in UTF-8 by passing that argument name to theHunspell
constructor or setting theHUNSPELL_DATA
environment variable. This is a restriction of Hunspell / Windows operations.
Author
Author(s): Tim Rodriguez and Matthew Seal
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for cyhunspell-2.0.1a0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca639b70e1c3ac00e434da39eadc4865f64b79c0c924501d57c0f1d74fc14c46 |
|
MD5 | f86973f71aa8c1dc5aaebfd38b2fe0bd |
|
BLAKE2b-256 | 67e9b122516d8082ae24f21d13a575ec246c75f2085e816e608971c1123f14b8 |
Hashes for cyhunspell-2.0.1a0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c418f8d25507cd563afabe3574d895b26797830d0ec5f3d723a1a25ae3b15d1 |
|
MD5 | 9134cb2f975c5195e96c0e3bb887cfef |
|
BLAKE2b-256 | c7784b9041259980f1155c8c0d26091c86cbcf570707baa9a7edf7946170dc75 |
Hashes for cyhunspell-2.0.1a0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 832323cd7f636b552504fd4e73cdc0b83318a3e9ecc8e88c50c801f40e209e7b |
|
MD5 | 855777210704901a343fff7fddb8e8e2 |
|
BLAKE2b-256 | 227c827e3e23718048ae343062a16df02cac6021f43aa49dbcd9f65b2b1ec72f |
Hashes for cyhunspell-2.0.1a0-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c134d0d789a189c9cde4d09857f2599e9c0c799377378b109f7e0b3794fdc08 |
|
MD5 | 859d4fe596bb796ee2fe09ada9a44ba3 |
|
BLAKE2b-256 | ee78b7c196207edfc97cd3fdd4b25c13cbaa29d7a814ac2444b862707dc35d18 |
Hashes for cyhunspell-2.0.1a0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51d4e2fe44bca8ce1532b09fb259b0fe3ed1e81c68f440bbada43488d1e22c49 |
|
MD5 | 73dd68a066a8de6405d90eea12897fe1 |
|
BLAKE2b-256 | 7354ba856d1c34cffb81a65eca483b690adc631b14676afd119b0fed7a14d070 |
Hashes for cyhunspell-2.0.1a0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcc7bf32449e29f48b3afd1ca1a0cba3c7290e411860d4d3da06f2bbb77adf05 |
|
MD5 | d038f9d2063b8d99d4c7cea79300364f |
|
BLAKE2b-256 | 8bd20494d3f241b88735f2146d629fb6a553bf00e270a00ccd0be9004bea3e97 |
Hashes for cyhunspell-2.0.1a0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a22b5548854cf095dfa61a6af35540f5cf58805ac6a49b8da4b795f76a2b2876 |
|
MD5 | be0e20f6530fce899c8321647509bf92 |
|
BLAKE2b-256 | 05b6c0b1254a5a253402c9baa1b90da100229119b67dd85aab6583344897963e |
Hashes for cyhunspell-2.0.1a0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6e894e0957d8c618cd3f3e9939ef9cb66edb9090c43acf7ac1ba2661548b14f |
|
MD5 | 318c28901013ae96a41ffcf59529f76f |
|
BLAKE2b-256 | 9d545377f76692dbe27d646798f2f5d88833f9b004bccc0fa0fbad3b317b5693 |
Hashes for cyhunspell-2.0.1a0-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99f1a427f7a50769a73b1629178fb089787f30f9b727895be783973b0621b58a |
|
MD5 | a0b8443fbc5535ddc964f6ba4163f06c |
|
BLAKE2b-256 | 1986dc5e6fd7e04fe07e99617ccbd3fcaa7d4e3fb1852d439db7aaafabb9f332 |
Hashes for cyhunspell-2.0.1a0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9af0823f6db5ea787177b2cd873c107d9e631742a5ef2040171761ff066a087 |
|
MD5 | d301c6dc3b8f8403cfc89cd3be93091d |
|
BLAKE2b-256 | c6f4ab074ee51400716652dee565968d393966684fea3c1db1b24986b16d67c6 |
Hashes for cyhunspell-2.0.1a0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 010aac6cbf597584cbaa3e1b4ef2fde1decfcc474bc3c064a6644e681361fe7c |
|
MD5 | 95cf023ac0423dde15ad0545e1a385cb |
|
BLAKE2b-256 | 2f5349bdc14c9a17f661bdabab6c190e63a2e2e667c0c5ebba029d7801e0b791 |
Hashes for cyhunspell-2.0.1a0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1902ed7d0cda4a28b454eefefe89fb8f0e5f834e4c7d61b9034c42e5e1a11e8d |
|
MD5 | 293c2fe27e7437632a7d399a0524efee |
|
BLAKE2b-256 | 6cd02846667855005cded11d8276f208213cfbbf7692d0409117a7683b2367b4 |
Hashes for cyhunspell-2.0.1a0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e1998968332a3f04186500060d0969a89b4ab442d51985c900b9d9325bb2fd1 |
|
MD5 | 1d90f87926d06dc127ee38605fab8530 |
|
BLAKE2b-256 | e97d1b01deb59c9307ad5ce7b50ccc3fccd4f937e228cf480484e798312b99c3 |
Hashes for cyhunspell-2.0.1a0-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5be77cc7558a75ee05a4178285b06f5708a8c6b1f28e53bcb9d937ef4fe9046d |
|
MD5 | 3eccba65a690e6b7f533221e9559a564 |
|
BLAKE2b-256 | 6843aaf0a05d2d1c932733e9cb175c1fb82cf09132722794fbbe66536c2c1015 |
Hashes for cyhunspell-2.0.1a0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9a9a8235aa72b3915befc0d7c18b4840ed20c746cacc79b6459406f1dc27170 |
|
MD5 | e56e6cd93a173f12e4f23958c0c018df |
|
BLAKE2b-256 | c1e223b9120cb0b1032a8f6210eaba4b13dcf6da738f5f54b85cb7f4282ce017 |
Hashes for cyhunspell-2.0.1a0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b961d044285056c0647aff813ace0b87c67a6044247374dd204e6d3fab3e7b05 |
|
MD5 | 4b6ea143cc3b522b0081aac287914e75 |
|
BLAKE2b-256 | 859fd82af3a95b2a13e5f2a2f50937904c3848b5c58dafc648fef9482a880145 |
Hashes for cyhunspell-2.0.1a0-cp35-cp35m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db8746c0c685a30b10eeadfc16a30514a0f621b09109da5d657b7c215aeb4936 |
|
MD5 | 08b4c7acce61e882b10607b09a867cd0 |
|
BLAKE2b-256 | 8f468cebc82f5d04f8a8307c1600aa734c2032505f57edbab98f6ce5d1add062 |
Hashes for cyhunspell-2.0.1a0-cp35-cp35m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d933944e5677f79c3217bf8f0e39fc5d9237b3c5aaaeff7b0ac386e29c32899b |
|
MD5 | 1713ff8a25e3ecb7e0df5e3cd8ccdbc3 |
|
BLAKE2b-256 | 4a56a771127dc162c3d9c274e8a346835fa5993b9bf658a3157cafdd11b1fa4b |