Search code examples
pythonpyenchant

Error in PyEnchant when certain letter/numbers are used


PyEnchant seems to have weird behavior on certain letter/number combinations:

>>> import enchant
>>> d=enchant.Dict("en_US")
>>> d.add("def")
>>> d.add("abc")
>>> d.suggest("P92")

** (python.exe:15036): CRITICAL **: enchant_is_all_caps: assertion `word && *word' failed
['ᾈ\t_us', 'Def', 'Abc']

Not every letter/number combination yields this issue. More examples are:

>>> d.suggest("A92")
['Abc']
>>> d.suggest("92P")

** (python.exe:15036): CRITICAL **: enchant_is_all_caps: assertion `word && *word' failed

** (python.exe:15036): CRITICAL **: enchant_is_title_case: assertion `word && *word' failed

** (python.exe:15036): CRITICAL **: enchant_is_all_caps: assertion `word && *word' failed
['', 'DEF', 'ABC']

A92 yielded something, 92P gave 3 critical responses.

In PyEnchant, the critical errors (are they errors?) print to the screen, but there does not seem to be a mechanism to capture this. I unsuccessfully tried the try/except block

Is there a way to test when the "critical" message would be displayed and eliminate the message by not asking for a spelling suggestion?


Solution

  • From http://pythonhosted.org/pyenchant/api/enchant.html

    add(word)

    Add a word to the associated personal word list.
    

    Therefore my understanding is that you need a personal word list (PWL).

    Pyenchant is a ctypes-based wrapper for enchant C library. My understanding is that ctypes caches objects for reuse. So starting with a fresh terminal or if on Windows whatever necessary to clear anything cached by ctypes (maybe restart Windows if in doubt?):

    Then use a personal word list like this:

    import enchant
    d = enchant.DictWithPWL("en_US","mywords.txt")
    d.add("def")
    d.add("abc")
    print d.suggest("P92")
    print d.suggest("92P")
    print d.suggest("Helo")
    

    Outputs:

    ['Abc', 'Def']
    ['ABC', 'DEF']
    ['He lo', 'He-lo', 'Hole', 'Help', 'Helot', 'Hello', 'Halo', 'Hero', 'Hell', 'Held', 'Helm', 'Heel', 'Loathe', 'Def']
    

    if you find blank lines in mywords.txt (you have not cleaned down the ctypes caching correctly) then delete the contents close your terminal or whatever you need to do on Widows and try again.

    If you want to use the a PWL in memory delete or truncate (definitely remove any blank lines you created previously) the default PWL file (~/.config/enchant/en_US.dic on Linux) and use:

    d=enchant.DictWithPWL("en_US", None)
    

    I strongly suspect the error messages you are seeing are being thrown by the underlying C library (enchant) not pyenchant directly, so I do not know of a way to catch them or prevent them being shown. But if you use DictWithPWL() they never get thrown in the first place.