Python GTK Spellcheck¶
PyGtkSpellCheck is a spellchecking library written in pure Python for Gtk based on Enchant. It supports both Gtk’s Python bindings, PyGObject and PyGtk, and for both Python 2 and 3 with automatic switching and binding autodetection. For automatic translation of the user interface it can use GEdit’s translation files.
Features¶
- Localized names of the available languages.
- Supports word, line and multiline ignore regexes.
- Supports ignore custom tags on Gtk’s TextBuffer.
- Enable and disable of spellchecking with preferences memory.
- Supports hotswap of Gtk’s TextBuffers.
- PyGObject and PyGtk compatible with automatic detection.
- Python 2 and 3 support.
- As Enchant, support for Hunspell (LibreOffice) and Aspell (GNU) dictionaries.
Download¶
Source distribution¶
PyPI package available at: http://pypi.python.org/pypi/pygtkspellcheck/
pip install pygtkspellcheck
Ubuntu/Debian¶
Install packages:
- Python 3:
sudo apt-get install python3-gtkspellcheck
- https://github.com/downloads/koehlma/pygtkspellcheck/python3-gtkspellcheck_3.0-1_all.deb
- Python 2:
sudo apt-get install python-gtkspellcheck
- https://github.com/downloads/koehlma/pygtkspellcheck/python-gtkspellcheck_3.0-1_all.deb
- Documentation:
sudo apt-get install python-gtkspellcheck-doc
- https://github.com/downloads/koehlma/pygtkspellcheck/python-gtkspellcheck-doc_3.0-1_all.deb
Hacking¶
Development repository is available at: https://github.com/koehlma/pygtkspellcheck
git clone git://github.com/koehlma/pygtkspellcheck.git
API Reference¶
The main object is called Spellchecker and can be associated with any GtkTextView:
SpellChecker class reference¶
-
class
gtkspellcheck.spellcheck.
SpellChecker
(view, language='en', prefix='gtkspellchecker', collapse=True, params={})¶ Main spellchecking class, everything important happens here.
Parameters: - view – GtkTextView the SpellChecker should be attached to.
- language – the language which should be used for spellchecking. Use a combination of two letter lower-case ISO 639 language code with a two letter upper-case ISO 3166 country code, for example en_US or de_DE.
- prefix – a prefix for some internal GtkTextMarks.
- collapse – enclose suggestions in its own menu.
- params – dictionary with Enchant broker parameters that should be set e.g. enchant.myspell.dictionary.path.
-
languages
¶ A list of supported languages.
-
exists
(language)¶ checks if a language exists
Parameters: language – language to check
-
-
add_to_dictionary
(word)¶ Adds a word to user’s dictionary.
Parameters: word – the word to add
-
append_filter
(regex, filter_type)¶ Append a new filter to the filter list. Filters are useful to ignore some misspelled words based on regular expressions.
Parameters: - regex – the regex used for filtering
- filter_type – the type of the filter
Filter Types:
SpellChecker.FILTER_WORD
: The regex must match the whole word you want to filter. The word separation is done by Pango’s word separation algorythm so, for example, urls won’t work here because they are split in many words.SpellChecker.FILTER_LINE
: If the expression you want to match is a single line expression use this type. It should not be an open end expression because then the rest of the line with the text you want to filter will become correct.SpellChecker.FILTER_TEXT
: Use this if you want to filter multiline expressions. The regex will be compiled with the MULTILINE flag. Same with open end expressions apply here.
-
append_ignore_tag
(tag)¶ Appends a tag to the list of ignored tags. A string will be automatic resolved into a tag object.
Parameters: tag – tag object or tag name
-
buffer_initialize
()¶ Initialize the GtkTextBuffer associated with the GtkTextView. If you associate a new GtkTextBuffer with the GtkTextView call this method.
-
check_range
(start, end, force_all=False)¶ Checks a specified range between two GtkTextIters.
Parameters: - start – start iter - checking starts here
- end – end iter - checking ends here
-
disable
()¶ Disable spellchecking.
-
enable
()¶ Enable spellchecking.
-
enabled
¶ Enable or disable spellchecking
-
ignore_all
(word)¶ Ignores a word for the current session.
Parameters: word – the word to ignore
-
language
¶ The language used for spellchecking
-
recheck
()¶ Rechecks the spelling of the whole text.
-
remove_filter
(regex, filter_type)¶ Remove a filter from the filter list.
Parameters: - regex – the regex which used for filtering
- filter_type – the type of the filter
-
remove_ignore_tag
(tag)¶ Removes a tag from the list of ignored tags. A string will be automatic resolved into a tag object.
Parameters: tag – tag object or tag name
This library also includes a utility module to unpack LibreOffice .oxt
extension dictionaries (Hunspell). This is especially useful for MS Windows
users to include dictionaries for this library. Use this to extract the
Hunspell dictionaries out of the extension and then pass to the Spellchecker
the path to the location of the extraction in the params argument with the key
enchant.myspell.dictionary.path
.
oxt_import module reference¶
-
gtkspellcheck.oxt_import.
deflate_oxt
(oxt_path, extract_path, override=False, move_path=None)¶ Uncompress, read and install LibreOffice
.oxt
dictionaries extensions.Parameters: - oxt_path – path to a directory containing the
.oxt
extensions. - extract_path – path to extract Hunspell dictionaries files.
- override – override files.
- move_path – Optional path to move the
.oxt
files after processing.
Return type: None
This function extracts the Hunspell dictionaries (
.dic
and.aff
files) from all the.oxt
extensions found onoxt_path
directory to theextract_path
directory.Extensions like the ones found here:
In detail, this functions does the following:
- Find all the
.oxt
extension files withinoxt_path
- Open (unzip) each extension.
- Find the dictionary definition file within (dictionaries.xcu)
- Parse the dictionary definition file and locate the dictionaries files.
- Uncompress those files to
extract_path
.
By default file overriding is disabled, set
override
parameter to True if you want to enable it. As and additional option, each processed extension can be moved tomove_path
.- oxt_path – path to a directory containing the
License¶
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.