Search code examples
pythonnltkwordnetlcssentence-similarity

AttributeError: 'list' object has no attribute '_all_hypernyms' what is this error?


This program is to find similarities between the a sentences and words and how they are similar in synonyms I have downloaded the nltk when i first coded it was run and there were no errors but after some days when i run the program ti give me this error AttributeError: 'list' object has no attribute '_all_hypernyms'
the error is because of this wn.wup_similarity

import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.corpus import wordnet as wn

database=[]
main_sen=[]
words=[]
range_is=[0.78]
word_check=[0.1]

main_sentence="the world in ending with the time is called hello"
database_word=["known","complete"]

stopwords = stopwords.words('english')
words = word_tokenize(main_sentence)
filtered_sentences = []
for word in words:
    if word not in stopwords:      
        filtered_sentences.append(word)
        
print (filtered_sentences)
for databasewords in database_word:
    database.append(wn.synsets(databasewords)[0])
    
    print(database)
for sentences in filtered_sentences:
   main_sen.append(wn.synsets(sentences))
   print(main_sen)
    #   Error is in below lines   
    for data in database: 
       for sen in main_sen :
           word_check.append(wn.wup_similarity(data,sen))
           if word_check >range_is:
                   count = +1
                    
                   print(count)




Solution

  • so I am not sure of what you are trying to acheive with this code but I see why the code is breaking, what is happening is that in the line that is breaking:

    word_check.append(wn.wup_similarity(data[0], sen[0]))
    

    You are comparing two lists of synsets, this is problematic because the function cannot handle list of synsets.

    So if you want to compare all the synsets that are created you will need to use an additional for loop to extract all the synset in the list.

    However, if you want to compare the first synset of the first word with the first synset of the secon word you can simply do this

    import nltk
    from nltk.tokenize import word_tokenize
    from nltk.corpus import stopwords
    from nltk.corpus import wordnet as wn
    
    database=[]
    main_sen=[]
    words=[]
    range_is=[0.78]
    word_check=[0.1]
    
    main_sentence="the world in ending with the time is called hello"
    database_word=["known","complete"]
    
    stopwords = stopwords.words('english')
    words = word_tokenize(main_sentence)
    filtered_sentences = []
    for word in words:
        if word not in stopwords:      
            filtered_sentences.append(word)
            
    print (filtered_sentences)
    for databasewords in database_word:
        database.append(wn.synsets(databasewords)[0])
        
        print(database)
    for sentences in filtered_sentences:
       main_sen.append(wn.synsets(sentences)[0])
       print(main_sen)
        #   Error is in below lines   
        for data in database: 
           for sen in main_sen :
               word_check.append(wn.wup_similarity(data,sen))
               if word_check >range_is:
                       count = +1
                        
                       print(count)
    
    

    Also you need to be careful in the if statement of the word_check > range_is becasue you are comparing a list with a value, while you should check the similaryty outcome itself

                    if wn.wup_similarity(data, sen) > range_is:
                        count = +1