FuzzyWuzzy 错误:StringProcessor.replace_non_letters_non_numbers_with_whitespace(s)

Error with FuzzyWuzzy: StringProcessor.replace_non_letters_non_numbers_with_whitespace(s)

我无法获得以下功能 运行:

match, match_score = process.extractOne(score, pct_dict.keys())

我收到一个似乎无法解决的空白错误。知道是什么原因造成的吗?

它应该做什么:如果分数是 15 它应该 return 0.026

错误:

Error: output = self.func(*resolved_args, **resolved_kwargs) wnas1
| File "/code/cleveland/templatetags/percentiles_ratings.py", line 32, in get_percentile_standard wnas1 | match, match_score = process.extractOne(score, pct_dict.keys()) wnas1 | File "/usr/local/lib/python3.7/site-packages/fuzzywuzzy/process.py", line 220, in extractOne wnas1 | return max(best_list, key=lambda i: i[1]) wnas1 | File "/usr/local/lib/python3.7/site-packages/fuzzywuzzy/process.py", line 78, in extractWithoutOrder wnas1 | processed_query = processor(query) wnas1 | File "/usr/local/lib/python3.7/site-packages/fuzzywuzzy/utils.py", line 95, in full_process wnas1 | string_out = StringProcessor.replace_non_letters_non_numbers_with_whitespace(s) wnas1 | File "/usr/local/lib/python3.7/site-packages/fuzzywuzzy/string_processing.py", line 26, in replace_non_letters_non_numbers_with_whitespace wnas1
| return cls.regex.sub(" ", a_string)

代码:

from __future__ import unicode_literals
from django import template
from fuzzywuzzy import fuzz
from fuzzywuzzy import process


register = template.Library()


@register.simple_tag
def get_perc(score):
    MATCH_THRESHOLD = 80
    pct_dict = {14: 0.016, 14.7: 0.021, 15.3: 0.026, 16: 0.034, 16.7: 0.04, 17.3: 0.05, 18: 0.07, 18.7: 0.09,
                    19.3: 0.11, 20: 0.13, 20.7: 0.17, 21.3: 0.21, 22: 0.26, 22.7: 0.31, 23.3: 0.38, 24: 0.47}
    if not score:
        return '--'
    elif score < 26.7:
        return '<1'

    match, match_score = process.extractOne(score, pct_dict.keys())

    if match_score >= MATCH_THRESHOLD:
        return pct_dict[match]
    else:
        return '--'

根据 fuzzywuzzy 文档,您需要比较两个字符串。这意味着您需要将字符串中的值转换为比较它们。然后你需要这样做:

match, match_score = process.extractOne(str(score), pct_dict.keys())

我不推荐这种方法,因为它不准确。

>>> x = ['1','2','3']
>>> y='2'
>>> process.extractOne(y,x)
('2', 100)
>>> y='2.2'
>>> process.extractOne(y,x)
('2', 90)
>>> y = '2.9'
>>> process.extractOne(y,x)
('2', 90)

在最后 2 个条目中,您将看到 2.2 和 2.9 的得分 90,其中 2.9 更接近 3。

因为你有数字,我建议你像这样简单地比较它们:

value = min(pct_dict, key=lambda x:abs(x - score))
# then some logics to see if value is close to score or put some static threshold value like `abs(value-score) < .3`

很少有 SO answers 可以帮助您解决这个问题。

谢谢,成功了。感谢您对字符串值的澄清。这允许我设置 str(score) 以将分数值转换为字符串。

这是功能代码:

@register.simple_tag
def get_perc(score):
    MATCH_THRESHOLD = 80
    pct_dict = {'14': '0.016', '14.7': '0.021', '15.3': '0.026', '16': '0.034', '16.7': '0.04', '17.3': '0.05', '18': '0.07', '18.7': '0.09', '19.3': '0.11', '20': '0.13', '20.7': '0.17', '21.3': '0.21', '22': '0.26', '22.7': '0.31', '23.3': '0.38', '24': '0.47'}
    if not score:
        return '--'
    elif score < 24:
        return '<1'

    match, match_score = process.extractOne(str(score), pct_dict.keys())

    if match_score >= MATCH_THRESHOLD:
        return pct_dict[match]
    else:
        return '--'