From cfb6b634f8330f29576a3e7afc9b1d199c7651bf Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> Date: Tue, 17 Sep 2019 18:33:12 +0200 Subject: Enhance off-topic names search feature https://github.com/python-discord/bot/issues/435 This commit is meant to enhance the search feature in three separate, but related ways: 1. By changing the type annotation of the query to OffTopicName, we will use the same character translation table for the query as we did when storing the off-topic name, leading to better matches. 2. By adding a membership test, `query in name`, we are better able to search for off-topic names using a substring. 3. Given point 1 and point 2, we can increase the cut-off value we use for `difflib.get_close_matches` so we reduce the number of matches that bear little resemblance to the query in our human eyes. This commit closes #435 --- bot/cogs/off_topic_names.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index cb8a03374..1f6ed80b5 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -144,20 +144,21 @@ class OffTopicNames(Cog): @otname_group.command(name='search', aliases=('s',)) @with_role(*MODERATION_ROLES) - async def search_command(self, ctx, *, query: str): + async def search_command(self, ctx, *, query: OffTopicName): """ Search for an off-topic name. """ result = await self.bot.api_client.get('bot/off-topic-channel-names') - matches = difflib.get_close_matches(query, result, n=10, cutoff=0.35) - lines = sorted(f"• {name}" for name in matches) + in_matches = {name for name in result if query in name} + close_matches = difflib.get_close_matches(query, result, n=10, cutoff=0.70) + lines = sorted(f"• {name}" for name in in_matches.union(close_matches)) embed = Embed( title=f"Query results", colour=Colour.blue() ) - if matches: + if lines: await LinePaginator.paginate(lines, ctx, embed, max_size=400, empty=False) else: embed.description = "Nothing found." -- cgit v1.2.3