pythonnlpnltksentiment-analysissynset

How can i get the first content of a python synsets list?


enter image description hereI have a scrapped text stored under the variable "message". I have removed the StopWords and stored the result with the variable "without_stop_words". I want to loop through each words in the 'without_stop_words' and get their MEANINGS AND PRONOUNS.

Currently i am trying to get the MEANINGS but I'm getting an error: "IndexError: list index out of range"

enter image description here

   
 for writeup in writeups:
        message = writeup.text
        #Stop Words
        stop_words = set(stopwords.words('english'))
        #print(stop_words)

        tokenized_words = word_tokenize(message)
        #Filtering Stop Words
        without_stop_words = []
        for word in tokenized_words:
            if word not in stop_words:
                without_stop_words.append(word)            
                #Word Meanings
        word_meanings = []
        for each_word in without_stop_words:
            sync_words = wordnet.synsets(each_word)
            meaning = sync_words[0].definition()
            print(meaning)

I want to get the MEANING of each word in the "without_stop_words".


Solution

  • The error comes from this line

    meaning = sync_words[0].definition()
    

    And it indicates that sync_words is empty

    for each_word in without_stop_words:
        sync_words = wordnet.synsets(each_word)
        if sync_words:
            meaning = sync_words[0].definition()
            word_meanings.append(meaning)
        else:
            # Whatever you want to do if it's empty
    

    This will stop the error, but you should try to find out why sync_words is empty in the first place.