Python中列表的TryExcept问题

2024-04-26 08:20:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用gensim在Python中使用word2vec。当我使用try/except来检查不在Google模型中的单词时,我遇到了一个问题。但不是每一次电脑都能打印出一个例外的单词。在

在代码之后,我显示了词汇表的内容,模型停止在单词travelers中,而不转换单词travelers后面的其余单词。我真的被困住了,我需要一些帮助。有什么想法吗?在

for x in range(0,len(data)):
    titles.append(data[x]['title'])
    paragraphs.append(data[x]['paragraphs'])

model = gensim.models.Word2Vec.load('/tmp/models/google2')
for y in range(95,96):
    vocabulary.append(titles[y])
    vocabulary.append(paragraphs[y][0])
    vocabulary.append(paragraphs[y+1][0])
    print vocabulary
    for entry in vocabulary:
        try:
            row = tokenizer.tokenize(entry)
            row = [word for word in row if word not in stopwords.words('english')]
            row = [model[item] for item in row]
            row = [np.sum(item) for item in row]
            last.append(row)
        except KeyError,e:
            print "There is a word that does not exist in the vocabulary: ", e

有一个词在词汇中是不存在的:u'travelers'

词汇[0]:亚洲全球旅游热潮

词汇[1]:随着越来越多的亚洲人,尤其是中国人到国外旅游,旅游业和消费能力正在发生大陆性的变化。在

词汇[2]:这是最近记忆中发生在中亚最贫穷国家身上最令人兴奋的事情。在

提前谢谢你。在


Tags: infordataitem单词词汇wordrow
2条回答

你需要给它一个条件。像这样:

for x in range(0,len(data)):
    titles.append(data[x]['title'])
    paragraphs.append(data[x]['paragraphs'])

model = gensim.models.Word2Vec.load('/tmp/models/google2')
for y in range(95,96):
    vocabulary.append(titles[y])
    vocabulary.append(paragraphs[y][0])
    vocabulary.append(paragraphs[y+1][0])
    print vocabulary
    for entry in vocabulary:
        try:
            row = tokenizer.tokenize(entry)
            row = [word for word in row if word not in stopwords.words('english')]
            # in your code, row will be overwritten several times, so I use new variables here
            temp = []
            temp1 = []
            for item in row:
                try:
                    model[item]
                except KeyError, e:
                    continue

                temp.append(model[item])
                temp1.append(np.sum(item))
                last.append(temp1)
        except KeyError,e:
            print "There is a word that does not exist in the vocabulary: ", e

希望它能起作用。在

由于您放置了for y in range(95,96):,所以循环只运行一次

相关问题 更多 >