当以下代码运行时,其获取类型错误如下:
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)
答案 0 :(得分:0)
问题是clusters
值为None
。您必须首先验证它是否不是None
:
for cluster in range(nclusters):
clusterSentence = []
if clusters:
for i,sentence in enumerate(clusters[cluster]):