Search code examples
pythonnlpnltksummarization

TypeError: unhashable type: 'list' for text summarization


from nltk.corpus import indian

sentence_score={}
#word=nltk.word_tokenize(text)
for sent in sentences:
    word_count_in_sentence = (len(nltk.word_tokenize(sentence)))
    if word in wordfreq.keys():
        if sent not in sentence_score.keys():
            sentence_score[sent]=wordfreq[word]
        else:
            senetence_score[sent]+=wordfreq[word]

I m getting this error what do i do

TypeError: unhashable type: 'list' for line 7 i.e    if word in wordfreq.keys():

Solution

  • Use a defaultdict

    from nltk.corpus import indian
    from collections import defaultdict
    
    sentence_score=defaultdict(int)
    #word=nltk.word_tokenize(text)
    for sent in sentences:
        word_count_in_sentence = (len(nltk.word_tokenize(sentence)))
        if word in wordfreq:
            senetence_score[sent]+=wordfreq[word]