0

I was trying to run my program but came across an error:

    Traceback (most recent call last):
  File "C:\myquestions\questansgen-script.py", line 11, in <module>
    load_entry_point('questansgen==0.1', 'console_scripts', 'questansgen')()
  File "c:\Python36\lib\site-packages\click\core.py", line 610, in __call__
    return self.main(*args, **kwargs)
  File "c:\Python36\lib\site-packages\click\core.py", line 590, in main
    rv = self.invoke(ctx)
  File "c:\Python36\lib\site-packages\click\core.py", line 782, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\Python36\lib\site-packages\click\core.py", line 416, in invoke
    return callback(*args, **kwargs)
  File "C:\myquestions\questansgen\scripts\questansgen.py", line 22, in generate_trivia
    questions = questions + article.generate_trivia_sentences()
  File "C:\myquestions\questansgen\article.py", line 22, in generate_trivia_sentences
    trivia = self.evaluate_sentence(sentence)
  File "C:\myquestions\questansgen\article.py", line 100, in evaluate_sentence
    trivia['similar_words'] = self.get_similar_words(replace_nouns[0])
  File "C:\myquestions\questansgen\article.py", line 39, in get_similar_words
    hypernym = synset.hypernyms()[0]
IndexError: list index out of range

Here is the code, that I am implementing, where the error is occurring:
Code file

Kindly, let me know what I can do to avoid this error and input any amount of text for processing.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • The end of the traceback is missing (which contains the ultimate exception). – lenz Apr 18 '17 at 10:48
  • oh yes.. let me edit it – Jaffer Wilson Apr 18 '17 at 10:59
  • @lenz done..please check it now – Jaffer Wilson Apr 18 '17 at 11:00
  • This has nothing to do with the amount of input text. It simply means there is no hyperonym for a word (for whatever reason). – lenz Apr 18 '17 at 11:17
  • @lenz If I want to avoid it then what I can do. As I am not able to take the larger files. please suggest. – Jaffer Wilson Apr 18 '17 at 11:22
  • 2
    Manually built resources like the WordNet never have complete coverage. You have to come up with some “backoff plan”, which depends on what you want to do. – lenz Apr 18 '17 at 11:27
  • @lenz Thank you for your advise. But is there any tweek that I can do in the code currently, so that I can avoid this error. Please can you let me know. I am currently working with the english language. – Jaffer Wilson Apr 18 '17 at 11:35
  • 1
    If by a "tweak" to "avoid this error" you mean that you don't know how to catch an exception, then you definitely should read through the [Python tutorial](https://docs.python.org/3/tutorial/index.html) – it's really well-written. – lenz Apr 18 '17 at 15:45

1 Answers1

1

Crude way: try-except

def first_hypernym(ss):
    try:
        x = ss.hypernyms()[0]
    except IndexError:
        x = None
    return x

One-liner: if-else

first_hypernym = None if not ss.hypernyms() else ss.hypernyms()[0]

To prove the point:

>>> x = []
>>> y = None if not x else x[0]
>>> y
>>> 

See Best way to check if a list is empty


In a painstakingly heuristics/linguistics way: Look at https://github.com/alvations/pywsd/blob/master/pywsd/allwords_wsd.py#L29 (Disclaimer: shameless plug)

Community
  • 1
  • 1
alvas
  • 115,346
  • 109
  • 446
  • 738