首页 > 解决方案 > 旅行推销员的实施未按预期循环

问题描述

我有一个句子列表,我想对它们进行排序,以便每个新句子一次只引入一个新词。我想我已经接近了,但我看到的错误是每次新单词的数量只会增加。例如,第一次循环时,带有一个新单词的句子被添加到列表中。在下一个循环中,将带有两个新单词的句子添加到列表中,等等。但是,这不是我想要的。相反,我希望每个新循环都添加一个新词的句子,而不是已经增加到两个新词。我已经调试了几个小时,重构和重新运行,但没有发现任何东西。有什么帮助吗?

commonSentenceList = list()
alreadySeenWordList = list()
sentenceAndNewWordCountDict = {}

import operator as op

def internalComp(sortedDic, sentenceList, wordList):
    toDelete = list()
    newOutputDict = {}
    secondaryOutputDict = {}
    #this loop is the part that is not working -> each new call should re order and find a new 1 and a new 0, not increase the number every time
    for x in sortedDic:
        sentenceString = x[0]
        if sentenceString not in sentenceList:
            tempCounter = 0
            for word in sentenceString.split():
                if word not in wordList:
                    tempCounter += 1

            newOutputDict[sentenceString] = tempCounter #this dictionary stores sentence and number of new words compared to known word list

    counter = 0
    for key, value in newOutputDict.items():
        if value is 0:
            #here should first check to make sure not dup sentence
            #print(key)
            #print(value)
            toDelete.append(key)
            sentenceList.append(key)
            counter +=1
            for x in key.split():
                if x not in wordList:
                    wordList.append(x)
    if counter is 0:
        for key, value in newOutputDict.items():
            if value is 1:
                toDelete.append(key)
                sentenceList.append(key)
                counter +=1
                for x in key.split():
                    if x not in wordList:
                        wordList.append(x)


    for x in toDelete:
        newOutputDict.pop(x)

    return sorted(newOutputDict.items(), key=op.itemgetter(1))


def travelingSalesman(ListOfSortedSentenceAndScore, sentencesOnlyList, wordList, outputDict):

    #sentencesOnlyList is preloaded with one sentence
    #wordList is preloaded with each word in the sentencesOnlyList
    while any(ListOfSortedSentenceAndScore):
        #return: each element includes sentence and number of new words
        ListOfSortedSentenceAndScore = internalComp(ListOfSortedSentenceAndScore, sentencesOnlyList, wordList) 

sorted_sentenceDataRelativeScoreDict = [('यह बहुत है।', 0), ('यह एक महानदी है।', 6.738544474393532e-05), ('यह मुमकिन है।', 6.738544474393532e-05), ('यह तस्करों का अड्डा है।', 0.00026954177897574127), ('मिशन कामयाब रहा', 0.00097574127), ('ज़ोकर बहुत बौना था', 0.00026954177897574127), ('यह एक टेढ़ा विचार था', 0.00026954177897574127), ('यह निराली हरकत थी।', 0.00026954177897574127), ('पर्यटक टूर पर था।', 0.000269897574127), ('पहिया ढीला था।', 0.00026954177897574127), ('प्रदर्शनी हाउसफुल थी।', 0.00026954177897574127), ('वह फुरसत में खेलेंगे।', 0.00026954177897574127), ('मेट्रो भूमिगत है।', 0.000227), ('कढ़ी में बहुत मसाला था।', 0.00026954177897574127), ('मीनार बहुत ऊँची थी।', 0.00026954177897574127), ('यह एक रेतीला तुफान था।', 0.00026954177897574127), ('यह एक कोरा चेक है', 0.000636119), ('इस उत्पाद में एक खराबी है', 0.0004043126684636119), ('यह एक खोटा सिक्का है', 0.0004043126684636119), ('चरवाहा बहुत चालाक था', 0.0004043126684636119), ('छत पर एक कौआ था', 0.000684636119), ('झाड़ी में एक झींगुर था', 0.000404312668463)]

travelingSalesman(sorted_sentenceDataRelativeScoreDict, commonSentenceList, alreadySeenWordList, sentenceAndNewWordCountDict)

print(commonSentenceList)


在我看来,在 internalComp 中,第一个循环将根据之前的每个句子迭代重新创建 newOutputDict。但情况似乎并非如此。相反,tempCounter 似乎没有重置,而是保存了来自循环的第一个实例的新词​​数的计数。

标签: python

解决方案


您的循环依赖于“internalComp”在其终止条件方面取得进展。

while condition:
    internalComp()

为了向您证明它在此数据集上没有取得进展,请将其添加到您现有的“for x in toDelete”中

if toDelete:
    for x in toDelete:
        newOutputDict.pop(x)
else:
    print ('toDelete was empty')
    raise Exception('internalComp made no progress. infinite loop may result')

或者也许将其添加到 travelSalesman 循环中:

print (len(ListOfSortedSentenceAndScore))

虽然这可能在您早期的一些测试用例中有效,但您很幸运。如果你不保证进度,你最终会找到一个触发无限循环的数据集。唯一安全的做法是确保 internalComp 始终消除至少一个。

我相信您在 wordList 上有一个实现错误,您在其中检查是否所有单词都在一个空列表中(它们不是),然后您构建一个单词列表。相反,当你发现一个新单词时,你应该立即将它添加到 wordList 中。但是,如果您不能确保 internalComp 取得进展,那么修复错误只会减少您找到给您带来无限循环的数据集的机会。


推荐阅读