1

I have the following code to check if a word is in the dictionary. If the word does not exist the call to dictionary.meaning returns None. The problem is that it also spits out an error message "Error: The Following Error occured: list index out of range". I did some research and it appeared that I could use a combination of try:, except: but no matter what I tried the error message is still printed out. Here is a test case that shows the problem. How can I make this code work without displaying the index error?

Code:

    def is_word(word):
        from PyDictionary import PyDictionary
        dictionary=PyDictionary()
        rtn = (dictionary.meaning(word))
        if rtn == None:
           return(False)
        else:
           return (True)

    my_list = ["no", "act", "amp", "xibber", "xyz"]

    for word in my_list:
        result = is_word(word)
        if result == True:       
           print(word, "is in the dictionary")
        else:
           print(word, "is NOT in the dictionary")

Output:

no is in the dictionary
act is in the dictionary
amp is in the dictionary
Error: The Following Error occured: list index out of range
xibber is NOT in the dictionary
Error: The Following Error occured: list index out of range
xyz is NOT in the dictionary
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Dave Mathews
  • 137
  • 3
  • 12

1 Answers1

4

I'm guessing your try/except block was around the wrong block, or you weren't catching it properly, but it's tough to tell without your code.

Try putting the try/except around the section of code that would be erroring (the dictionary check in this case).

EDIT:

My mistake. The error is getting printed by the PyDictionary library. You should be able to silence it by doing meaning(word, disable_errors=True).

def is_word(word):
    from PyDictionary import PyDictionary

    dictionary = PyDictionary()

    try:
        output = dictionary.meaning(word, disable_errors=True)
    except:
        return False
    else:
        return bool(output)

my_list = ["no", "act", "amp", "xibber", "xyz"]

for word in my_list:
    result = is_word(word)
    if result:       
       print("{} is in the dictionary".format(word))
    else:
       print("{} is NOT in the dictionary".format(word))

Second Edit: Using https://github.com/tasdikrahman/vocabulary.

from vocabulary.vocabulary import Vocabulary
vb = Vocabulary()

my_list = ["no", "act", "amp", "xibber", "xyz"]

for word in my_list:
    if vb.meaning(word):
       print("{} is in the dictionary".format(word))
    else:
       print("{} is NOT in the dictionary".format(word))
Jenner Felton
  • 787
  • 1
  • 9
  • 18
  • Thanks, I did not see the disable_errors option in the doc. There appears to be a logic problem as the output to this code says that each word was NOT found in the dictionary so it appears only the else clause is being run. This should be enough to get me going though. – Dave Mathews Sep 29 '18 at 03:18
  • Hello Jenner. Did the above code work correctly for you (real words say they are in the dictionary and fake words say they're not)? I've played around with this and found that if I run output = dictionary.meaning(word, disable_errors=True) outside of a try: except: block I get an "invalid argument" error. So I think the try: except: is just masking a real error. It appears that the version of PyDictionary I am running does not consider disable_errors=True to be a valid argument to dictionary.meaning. Can you point me to where you found this argument? Perhaps I have a typo or something? – Dave Mathews Sep 29 '18 at 15:49
  • Hmm, reading your post again I saw the link that you added to the PyDictionary code and see where you got the argument, if disable_errors == False: I'm perplexed as to why setting this value to True is not working for me. Do you have any ideas? – Dave Mathews Sep 29 '18 at 16:30
  • Hmm I think the issue is that that library added that code, so it's present in GitHub, but hasn't pushed a new release yet (so what you're installing won't have that feature flag). I think the solution is to either capture that print output, use a different library, or change that code in your local install. Capturing the print statement could use something like: https://stackoverflow.com/questions/8447185/to-prevent-a-function-from-printing-in-the-batch-console-in-python – Jenner Felton Sep 30 '18 at 03:42
  • I don't know if you have the option but maybe use https://github.com/tasdikrahman/vocabulary. I'll edit my answer. – Jenner Felton Sep 30 '18 at 03:52
  • Thanks Jenner. Yesterday, I resorted to writing the output to a file and just letting the errors spew out to stdout . The Vocabulary solution looks promising. I'll take a look. – Dave Mathews Sep 30 '18 at 15:19