Kivy - Spelling



Kivy library comes with a spelling module in its "kivy.core" package. It provides abstracted access to a range of spellchecking backends as well as word suggestions. The API is inspired by the "python-enchant" library.

You need to install enchant to be able to use this feature.

pip3 install pyenchant

Example

Create an object of Spelling class (defined in "kivy.core.spelling" module) to call its various methods. For instance, the list_languages() method returns a list of supported languages.

from kivy.core.spelling import Spelling
s = Spelling()
s.list_languages()

Output

It will list down all the supported languages −

['en_BW', 'en_AU', 'en_BZ', 'en_GB', 'en_JM', 'en_DK',
'en_HK', 'en_GH', 'en_US', 'en_ZA', 'en_ZW', 'en_SG', 'en_NZ',
'en_BS', 'en_AG', 'en_PH', 'en_IE', 'en_NA', 'en_TT', 'en_IN',
'en_NG', 'en_CA']

You can select a specific language from the list for subsequent use.

s.select_language('en_US')

The check() method in the Spelling class checks if a given word is valid in the currently active language. If yes, it returns True. If the word shouldn't be checked, returns None (e.g. for ''). If it is not a valid word in self._language, then it returns False.

>>> s.check('this')
   True
>>> s.check('thes')
   False

You can obtain suggestions from the Spelling class for a given word.

s.suggest('wold')

['wild', 'wolf', 'old', 'wolds', 'woald', 'world', 'would',
'weld', 'sold', 'woad', 'word', 'told', 'wood', 'cold', 'gold']

If you try to select a language which is not present in the list of supported language, Kivy raises the following NoSuchLangError exception −

s.select_language('Hindi')
kivy.core.spelling.NoSuchLangError: Enchant Backend: No
language for "Hindi"

When a language-using method is called but no language was selected prior to the call, Kivy raises "NoLanguageSelectedError".

Advertisements