Skip to content

Commit

Permalink
Fix poor fuzzy results due to changes in rapidfuzz 3.0 (#6224)
Browse files Browse the repository at this point in the history
Co-authored-by: aikaterna <20862007+aikaterna@users.noreply.github.com>
  • Loading branch information
Jackenmen and aikaterna committed Aug 10, 2023
1 parent 3ac2512 commit dbb91df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 4 additions & 2 deletions redbot/cogs/audio/core/utilities/local_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lavalink
from red_commons.logging import getLogger

from rapidfuzz import process
import rapidfuzz
from redbot.core import commands
from redbot.core.i18n import Translator
from redbot.core.utils import AsyncIter
Expand Down Expand Up @@ -116,7 +116,9 @@ async def _build_local_search_list(
to_search_string = {
i.local_track_path.name for i in to_search if i.local_track_path is not None
}
search_results = process.extract(search_words, to_search_string, limit=50)
search_results = rapidfuzz.process.extract(
search_words, to_search_string, limit=50, processor=rapidfuzz.utils.default_process
)
search_list = []
async for track_match, percent_match, __ in AsyncIter(search_results):
if percent_match > 85:
Expand Down
6 changes: 4 additions & 2 deletions redbot/cogs/audio/core/utilities/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lavalink
from red_commons.logging import getLogger

from rapidfuzz import process
import rapidfuzz
from redbot.core import commands
from redbot.core.i18n import Translator
from redbot.core.utils import AsyncIter
Expand Down Expand Up @@ -132,7 +132,9 @@ async def _build_queue_search_list(
track_title = track.title

tracks[queue_idx] = track_title
search_results = process.extract(search_words, tracks, limit=50)
search_results = rapidfuzz.process.extract(
search_words, tracks, limit=50, processor=rapidfuzz.utils.default_process
)
search_list = []
async for title, percent_match, queue_position in AsyncIter(search_results):
if percent_match > 89:
Expand Down
6 changes: 4 additions & 2 deletions redbot/cogs/customcom/customcom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from urllib.parse import quote_plus

import discord
from rapidfuzz import process
import rapidfuzz

from redbot.core import Config, commands
from redbot.core.commands import Parameter
Expand Down Expand Up @@ -324,7 +324,9 @@ async def cc_search(self, ctx: commands.Context, *, query):
- `<query>` The query to search for. Can be multiple words.
"""
cc_commands = await CommandObj.get_commands(self.config.guild(ctx.guild))
extracted = process.extract(query, list(cc_commands.keys()))
extracted = rapidfuzz.process.extract(
query, list(cc_commands.keys()), processor=rapidfuzz.utils.default_process
)
accepted = []
for key, score, __ in extracted:
if score > 60:
Expand Down
10 changes: 8 additions & 2 deletions redbot/core/utils/_internal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import aiohttp
import discord
from packaging.requirements import Requirement
from rapidfuzz import fuzz, process
import rapidfuzz
from rich.progress import ProgressColumn
from rich.progress_bar import ProgressBar
from red_commons.logging import VERBOSE, TRACE
Expand Down Expand Up @@ -154,7 +154,13 @@ async def fuzzy_command_search(
choices = {c: c.qualified_name for c in commands}

# Do the scoring. `extracted` is a list of tuples in the form `(cmd_name, score, cmd)`
extracted = process.extract(term, choices, limit=5, scorer=fuzz.QRatio)
extracted = rapidfuzz.process.extract(
term,
choices,
limit=5,
scorer=rapidfuzz.fuzz.QRatio,
processor=rapidfuzz.utils.default_process,
)
if not extracted:
return None

Expand Down

0 comments on commit dbb91df

Please sign in to comment.