timmahrt / pysle

Python interface to ISLEX, an English IPA pronunciation dictionary with syllable and stress marking.
Other
47 stars 5 forks source link

Fail gracefully when looking up a word that's not in the dictionary #18

Open paulvonhippel opened 1 year ago

paulvonhippel commented 1 year ago

pysle crashes when give a word that's not in its dictionary. It would be better, I think, to return a blank or something and continue running.

To give you an idea what I mean by "crash," I ran this code:

from pysle import isletool
isle = isletool.Isle()
print(isle.lookup('zit')[0]) # Get the first entry's pronunciation

and this is what I saw in the terminal:

Traceback (most recent call last):
  File "test_pysle.py", line 3, in <module>
    print(isle.lookup('zit')[0]) # Get the first entry's pronunciation
          ^^^^^^^^^^^^^^^^^^
  File "<path>\pysle\isletool.py", line 109, in lookup
    return self._lazyLoad(word)
           ^^^^^^^^^^^^^^^^^^^^
  File "<path>\pysle\isletool.py", line 62, in _lazyLoad
    raise errors.WordNotInIsleError(word)
pysle.utilities.errors.WordNotInIsleError: Word 'zit' not in ISLE dictionary.  Please add it to continue.
timmahrt commented 1 year ago

Sorry for the delay!

That's the intended behavior. I don't recall why I wrote it that way and if I were to write it again, maybe I wouldn't write it that way.

Does this cause any problems for you? If you need to prevent crashing you can do something like:

try:
    print(isle.lookup('zit')[0])
except errors.WordNotInIsleError:
    pass

Does that work for you?