0

当下面的代码运行时,它得到的类型错误如下:

Traceback (most recent call last):
File "Example.py", line 174, in <module>
for i,sentence in enumerate(clusters[cluster]):
TypeError: 'NoneType' object has no attribute '__getitem__'

如何解决这个问题?

if __name__ == "__main__":
sent=open('/Desktop/word2vec_original/input.txt', 'r').read()
sentences=sent_tokenize(sent)
print
nclusters= 3
clusters = cluster_sentences(sentences, nclusters)
for cluster in range(nclusters):
    clusterSentence = []
    for i,sentence in enumerate(clusters[cluster]):
        posTaggedSentence = pos_tag(word_tokenize(sentences[sentence]))
        posTaggedSentenceStr = (' '.join(word + '/' + pos for word, pos in posTaggedSentence))
        posTaggedSentence.append(posTaggedSentenceStr.strip())
        clusterSentence.append(posTaggedSentenceStr)
4

1 回答 1

0

问题是clusters值是None。您必须首先验证它是否不是None

for cluster in range(nclusters):
    clusterSentence = []
    if clusters:
         for i,sentence in enumerate(clusters[cluster]):
于 2018-03-21T16:19:28.483 回答