Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无限循环未脱离嵌套for循环_Python_For Loop_While Loop_Nested_Fuzzy Search - Fatal编程技术网

Python 无限循环未脱离嵌套for循环

Python 无限循环未脱离嵌套for循环,python,for-loop,while-loop,nested,fuzzy-search,Python,For Loop,While Loop,Nested,Fuzzy Search,我很抱歉,我相信这是一个简单的问题 我正在使用fuzzysearch和Pyenchant模块进行单词搜索 我正在尝试将嵌套的for循环方法转换为使用for和while循环的方法。我失败了,得到了一个无限循环 基本方法如下 对于句子中的一个单词,检查该单词是否存在于某个词典中 如果单词不存在,请使用Enchant生成单词建议 对于每个建议,计算生成的单词和原始单词之间的相似性比率 如果比率足够高,停止生成建议,并将生词添加到列表中 对句子中的所有单词都这样做 例如,对于字符串:A='A以mth和h

我很抱歉,我相信这是一个简单的问题

我正在使用fuzzysearch和Pyenchant模块进行单词搜索

我正在尝试将嵌套的for循环方法转换为使用for和while循环的方法。我失败了,得到了一个无限循环

基本方法如下

对于句子中的一个单词,检查该单词是否存在于某个词典中

如果单词不存在,请使用Enchant生成单词建议

对于每个建议,计算生成的单词和原始单词之间的相似性比率

如果比率足够高,停止生成建议,并将生词添加到列表中

对句子中的所有单词都这样做

例如,对于字符串:
A='A以mth和history为单位的lvels

输出将是:
['A','levels','in','math','and','history']

我已通过以下方式实现了这一目标:

# Imports:
from fuzzywuzzy import fuzz
import enchant
from enchant.tokenize import get_tokenizer
tknzr = get_tokenizer("en")
d = enchant.Dict("en")

A = 'A lvels in Mths and Histtory'

B = []

# Loop through all words in A
for word, position in tknzr(A):

    # Does word exist in Dictionary?
    if d.check(word) is False:

        # generate a suggestion and calculate the 'fuzz factor'
        for suggestion in d.suggest(word):
            ratio = fuzz.ratio(suggestion, word)

            # If ratio is high enough, add word to list B and move to next word in sentence
            if ratio > 75:
                B.append(suggestion)
                break

# If word is in dictionary, just add
    else:
        B.append(word)

Out >>> ['A','levels','in','maths','and','history']
到目前为止还不错

我想将上面的代码转换为使用while和for循环的代码。这将是一种形式:生成新词,直到达到某个阈值

我尝试了以下方法:

for word,position in tknzr(A):

    if d.check(word) is False:

        ratio = 0
        while ratio <= 75:

            for suggestion in d.suggest(word):

                print "Suggestion: ", suggestion
                ratio = fuzz.ratio(suggestion, word)

                B.append(word)
    else:
        B.append(word)
问题如下:

d.suggest(word)中用于建议的
循环将始终运行到完成,然后较高的
,而
循环可以检查比率值


这意味着最后检查的
比率
值是建议的最后一个单词的比率,例如
历史
他的故事
直方图
的比率。这就是代码的工作原理吗?->如果是,很好,不要碰它。您做了一些更改后,代码是否停止工作?->哦,还原更改。代码现在起作用了吗?->是的,很好,不要碰它,否则恢复更改并重试?@ForceBru你没有错。。。也许这更像是一个代码审查的问题?@Bouletta:在我有机会回答之前,你删除了你的答案:看起来不错@Charles Morris不是真的,我没有读你问题的结尾,所以我只是再次解释一下。要修复无限循环,可以更改为以下行:
ratio=max(ratio,fuzz.ratio(suggestion,word))
但这不会提高您的性能,据我所知,这正是您需要的want@CharlesMorris我不知道有什么方法可以在不隐式中断for循环的情况下做您想要做的事情,但是您最好使用代码的第一个版本
Out >>> Suggestion:  History
Out >>> Suggestion:  Historicity
Out >>> Suggestion:  Historic
Out >>> Suggestion:  Historian
Out >>> Suggestion:  Sophistry
Out >>> Suggestion:  Histrionic
Out >>> Suggestion:  Histogram
Out >>> The above forever