Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiCorrector returns (sug, score) instead of (score, sug) #21

Open
CodeOptimist opened this issue Jan 16, 2022 · 0 comments
Open

MultiCorrector returns (sug, score) instead of (score, sug) #21

CodeOptimist opened this issue Jan 16, 2022 · 0 comments

Comments

@CodeOptimist
Copy link

CodeOptimist commented Jan 16, 2022

File "C:\Users\Chris\AppData\Local\Python\venv\document-search\Lib\site-packages\whoosh\spelling.py", line 327, in correct_query
sugs = c.suggest(token.text, prefix=prefix, maxdist=maxdist)
File "C:\Users\Chris\AppData\Local\Python\venv\document-search\Lib\site-packages\whoosh\spelling.py", line 73, in suggest
sugs = sorted(heap, key=lambda x: (0 - x[0], x[1]))
File "C:\Users\Chris\AppData\Local\Python\venv\document-search\Lib\site-packages\whoosh\spelling.py", line 73, in
sugs = sorted(heap, key=lambda x: (0 - x[0], x[1]))
TypeError: unsupported operand type(s) for -: 'int' and 'str'

Happens because Corrector expects _suggestions() to return (score, sug) like the other implementations, but MultiCorrector uses iteritems(seen) on a {sug: score} dict, which gives us reversed tuples.

whoosh\spelling.py line 173

class MultiCorrector(Corrector):
    """
    Merges suggestions from a list of sub-correctors.
    """

    def __init__(self, correctors, op):
        self.correctors = correctors
        self.op = op

    def _suggestions(self, text, maxdist, prefix):
        op = self.op
        seen = {}
        for corr in self.correctors:
            for score, sug in corr._suggestions(text, maxdist, prefix):
                if sug in seen:
                    seen[sug] = op(seen[sug], score)
                else:
                    seen[sug] = score
        return iteritems(seen)  # <----- (sug, score) tuples instead of (score, sug)

Proposed fix:

        return ((v, k) for k, v in seen.items())  # generator
@CodeOptimist CodeOptimist changed the title MultiCorrector returns (sug, score) instead of (score, sug) MultiCorrector returns (sug, score) instead of (score, sug) Dec 5, 2023
cclauss pushed a commit to cclauss/whoosh-1 that referenced this issue Jan 4, 2024
Update Python version in setup.py classifiers & fix indent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant