旅行商不按预期循环的实现

2024-04-18 15:11:27 发布

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

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

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似乎没有重置,而是保存循环第一个实例中的新词数。你知道吗


Tags: keyinnewforifisvaluenot
1条回答
网友
1楼 · 发布于 2024-04-18 15:11:27

您的循环依赖于“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')

或者将此添加到travelingSalesman循环中:

print (len(ListOfSortedSentenceAndScore))

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

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

相关问题 更多 >