fuzzywuzzy icon indicating copy to clipboard operation
fuzzywuzzy copied to clipboard

NameError: name 'ratio' is not defined

Open DivanshuTak opened this issue 2 years ago • 3 comments

While running the Fuzzy Wuzzy process.extract() method the following error is thrown :-

    matches = fw_process.extract(
../../.local/share/virtualenvs/proj-NKfiPrkj/lib/python3.8/site-packages/fuzzywuzzy/process.py:168: in extract
    return heapq.nlargest(limit, sl, key=lambda i: i[1]) if limit is not None else \
/usr/lib/python3.8/heapq.py:563: in nlargest
    result = [(key(elem), i, elem) for i, elem in zip(range(0, -n, -1), it)]
/usr/lib/python3.8/heapq.py:563: in <listcomp>
    result = [(key(elem), i, elem) for i, elem in zip(range(0, -n, -1), it)]
../../.local/share/virtualenvs/proj-NKfiPrkj/lib/python3.8/site-packages/fuzzywuzzy/process.py:117: in extractWithoutOrder
    score = scorer(processed_query, processed)
../../.local/share/virtualenvs/proj-NKfiPrkj/lib/python3.8/site-packages/fuzzywuzzy/fuzz.py:276: in WRatio
    base = ratio(p1, p2)
../../.local/share/virtualenvs/proj-NKfiPrkj/lib/python3.8/site-packages/fuzzywuzzy/utils.py:38: in decorator
    return func(*args, **kwargs)
../../.local/share/virtualenvs/proj-NKfiPrkj/lib/python3.8/site-packages/fuzzywuzzy/utils.py:29: in decorator
    return func(*args, **kwargs)
../../.local/share/virtualenvs/proj-NKfiPrkj/lib/python3.8/site-packages/fuzzywuzzy/utils.py:47: in decorator
    return func(*args, **kwargs)
../../.local/share/virtualenvs/proj-NKfiPrkj/lib/python3.8/site-packages/fuzzywuzzy/fuzz.py:28: in ratio
    return utils.intr(100 * m.ratio())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <fuzzywuzzy.StringMatcher.StringMatcher object at 0x7f115ab32160>

    def ratio(self):
        if not self._ratio:
>           self._ratio = ratio(self._str1, self._str2)
E           NameError: name 'ratio' is not defined


I found that pinning the python-Levenshtein version to 0.12.2 solve the issue.

DivanshuTak avatar Oct 12 '22 16:10 DivanshuTak

This should be the same issue as https://github.com/seatgeek/thefuzz/issues/35 and should be fixed by uninstalling python-Levenshtein/Levenshtein and installing it again.

maxbachmann avatar Oct 12 '22 16:10 maxbachmann

I found that pinning the python-Levenshtein version to 0.12.2 solve the issue.

This worked for me too. Uninstalling and reinstalling python-Levenshtein didn't solve the issue on my system.

chrisolof avatar Feb 28 '23 19:02 chrisolof

This should be solved by running

pip uninstall Levenshtein python-Levenshtein
pip install python-Levenshtein

since this is a bug in pip in regards to updating. So it would be interesting to know, why this still fails for you even after performing these steps.

maxbachmann avatar Feb 28 '23 20:02 maxbachmann