zverok / spylls

Pure Python spell-checker, (almost) full port of Hunspell
https://spylls.readthedocs.io
Mozilla Public License 2.0
284 stars 21 forks source link

Feature request : Adding words to the dictionary at runtime #29

Open gweltou opened 6 months ago

gweltou commented 6 months ago

Hi,

I used to work with pyhunspell (which doesn't seem to be maintained anymore), then with cyhunspell (which is currently broken on Python >=3.10).

I'm now trying to switch to Spylls but there is one critical feature that I miss : the ability to add words to the loaded dictionary at runtime.

There doesn't seem to be any function for that in the documentation or the source code. Is there a plan to add this feature in the future ?

Thanks a lot !

zverok commented 6 months ago

@gweltou Truth be told, I didn’t really expect to see spylls much used in production; I built it as educational material about how the classic spellchecker works. This is why it lacks some of the more “pragmatic” features that don’t affect the base principles.

I don’t expect to actively develop it further, for reasons both professional (what is said above) and personal (I am serving in the Ukrainian army currently); but I probably have enough capacity to accept PRs.

This being said, adding word to the dictionary can be achieved by exploiting internal APIs, though I can’t say it is convenient:

from spylls.hunspell import Dictionary

dictionary = Dictionary.from_files('internal/dictionaries/en_US')

from spylls.hunspell.data.dic import Word
from spylls.hunspell.algo.capitalization import Type

print(dictionary.lookup('Hogwarts')) #=> False

word = Word('Hogwarts', flags=set(), data={}, alt_spellings=[], captype=Type.INIT)
dictionary.dic.append(word, lower='hogwarts')

print(dictionary.lookup('Hogwarts')) #=> True

As APIs are internal, they expect a lot of arguments pre-calculated by the corresponding processes, but I assume that reusing those APIs, a reasonable public Dictionary.append('word', like='other_word') (common for various Hunspell wrappers) can be achieved.

gweltou commented 6 months ago

Thank you for your answer ! I can definitely work with that !