Cython wrapper on Hunspell Dictionary
This fork is based on https://github.com/MSeal/cython_hunspell and modified to reduce dependencies by removing caching and batch functionalities. Apart from Hunspell itself, there are no other third-party dependencies.
Additional, providing precompiled wheels for multiple platforms.
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.
For the simplest install simply run (For Windows/Linux/MacOS):
pip install chunspell
For FreeBSD/AMD64 (pypi.org
does not accept freebsd wheel):
wget https://github.com/cdhigh/chunspell/releases/download/2.0.4/chunspell-2.0.4-freebsd-amd64.zip && \
unzip -y -d ./chunspell_whl/ chunspell-2.0.4-freebsd-amd64.zip && \
pip install chunspell --no-index --find-links=./chunspell_whl/. && \
rm -rf ./chunspell_whl
This will install the hunspell 1.7.2 C++ bindings on your behalf for your platform.
The library installs hunspell version 1.7.2. As new version of hunspell become available this library will provide new versions to match.
Spell checking & spell suggestions
Below are some simple examples for how to use the repository.
from hunspell import Hunspell
h = Hunspell()
You now have a usable hunspell object that can make basic queries for you.
h.spell('test') # True
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.
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.
h.suffix_suggest('do') # ('doing', 'doth', 'doer', 'doings', 'doers', 'doest')
The module can also stem words, providing the stems for pluralization and other inflections.
h.stem('testers') # ('tester', 'test')
h.stem('saves') # ('save',)
Like stemming but return morphological analysis of the input instead.
h.analyze('permanently') # (' st:permanent fl:Y',)
You can also specify the language or dictionary you wish to use.
h = Hunspell('en_US') # Canadian English
By default you have the only en_US
dictionaries available.
However you can download your own and point Hunspell to your custom dictionaries.
h = Hunspell('en_GB-large', hunspell_data_dir='/custom/dicts/dir')
You can also add new dictionaries at runtime by calling the add_dic method.
h.add_dic(os.path.join(PATH_TO, 'special.dic'))
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")
Much like adding, you can remove words.
h.remove(word)
- Google Style Guide
- Object Oriented (with a few exceptions)
- 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(s): Tim Rodriguez, Matthew Seal, cdhigh
MIT