首页 > 解决方案 > TypeError: unhashable type: 'list' 用于文本摘要

问题描述

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]

我收到此错误我该怎么办

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

标签: pythonnlpnltksummarization

解决方案


用一个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]

推荐阅读