【问题标题】:Try-Except issue with lists in PythonPython中列表的Try-Except问题
【发布时间】:2014-08-29 20:44:41
【问题描述】:

我一直在尝试使用 gensim 在 Python 中使用 word2vec。当我使用 try/except 来检查不在 Google 模型中的单词时,我遇到了问题。每次出现异常时,我都可以打印单词,但模型会停止并且不会计算列表中剩余的单词。

在代码之后,我显示了词汇表的内容,模型停在单词 travellers 之后,没有转换单词 travellers 之后的其余单词。我真的被困住了,我可以在这个问题上使用一些帮助。有什么想法吗?

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'travellers'

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

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

词汇[2]:这是最近记忆中中亚最贫穷国家发生的最激动人心的事情。

提前谢谢你。

【问题讨论】:

    标签: python list try-catch


    【解决方案1】:

    因为你把for y in range(95,96): 循环只运行一次

    【讨论】:

    • 它是用于测试目的的伙伴。循环运行一次并将 3 个值加载到词汇表中。无需多次运行即可产生设计的结果。例如,第二个循环运行 3 次,因为词汇表中有 3 个值。即使您是正确的,这是计算应该在旅行者一词之前停止的问题,而不是完全在那里。
    【解决方案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
    

    希望它有效。

    【讨论】:

    • 这是一个很好的方法,是的,但我仍然得到:AttributeError: 'Word2Vec' object has no attribute 'has_key'
    • 当您执行 temp1.append(np.sum(item)) 时出现错误,因为项目不是数字。我做了:对于行中的项目:尝试:打印项目 res = np.sum( model[item]) print res temp.append(res) except KeyError, e: continue 我的最后一个问题是我需要正确追加才能使每个单词数组的总和位于它们来自的数组中。例如,句子 [Asia, global, travel, boom] 有类似 [-1.2136844, -1.4799396, -0.75583267, -1.0718837]
    • 抱歉,我上次评论中的代码格式不正确。希望你明白我的意思。
    • @Swan87 好的。物品的类型是什么?如何将“亚洲”更改为 1.2136844?有关系列表之类的吗?
    • 不,model[item] 是这样做的。我需要做 2 次总和。第一个总和是 np.sum(model[item]) 我总结了向量的所有值。我需要的是为每个词汇条目分别设置这些值。我需要在临时列表中有 3 个数组,例如 [[-1.2136844, -1.4799396, -0.75583267, -1.0718837],[-0.5,...],[-1.7,...]] 然后添加每个元素都得到类似 [[0,5],[0,3],[1,7]] 的东西,并且能够说第一段与标题更相似,因为这个总和更接近标题的总和。我需要维护在 vocab.append 阶段创建的数组。
    猜你喜欢
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2013-11-21
    • 2013-11-13
    • 2021-04-04
    • 1970-01-01
    相关资源
    最近更新 更多