From 48df76c64efcd3c83dc33d3f58e695f6bfa8d4de Mon Sep 17 00:00:00 2001 From: Edoardo Rosa <6991986+notdodo@users.noreply.github.com> Date: Tue, 24 Sep 2024 17:48:14 +0200 Subject: [PATCH] hotfix: cutoff fuzz search --- app/erfiume/tgbot.py | 39 +++++++++++++++++++++------------------ app/erfiume_bot.py | 1 - 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/app/erfiume/tgbot.py b/app/erfiume/tgbot.py index 5d83039..11fc8b6 100644 --- a/app/erfiume/tgbot.py +++ b/app/erfiume/tgbot.py @@ -29,6 +29,7 @@ from .storage import AsyncDynamoDB RANDOM_SEND_LINK = 10 +FUZZ_SCORE_CUTOFF = 80 # UTILS @@ -174,17 +175,18 @@ async def handle_private_message( Se non sai quale cercare prova con /stazioni""" ) if update.message and update.effective_chat and update.message.text: - logger.info("Received private message: %s", update.message.text) async with AsyncDynamoDB(table_name="Stazioni") as dynamo: query = update.message.text.replace("/", "").strip() - fuzzy_query = process.extractOne(query, KNOWN_STATIONS)[0] - stazione = await dynamo.get_matching_station(fuzzy_query) - if stazione and update.message: - message = stazione.create_station_message() - if query != fuzzy_query: - message += ( - "\nSe non é la stazione corretta prova ad affinare la ricerca." - ) + fuzzy_query = process.extractOne( + query, KNOWN_STATIONS, score_cutoff=FUZZ_SCORE_CUTOFF + ) + logger.info(query) + if fuzzy_query: + stazione = await dynamo.get_matching_station(fuzzy_query[0]) + if stazione and update.message: + message = stazione.create_station_message() + if query != fuzzy_query[0]: + message += "\nSe non è la stazione corretta prova ad affinare la ricerca." await context.bot.send_message( chat_id=update.effective_chat.id, text=message, @@ -206,19 +208,20 @@ async def handle_group_message( Se non sai quale cercare prova con /stazioni""" ) if update.message and update.effective_chat and update.message.text: - logger.info("Received group message: %s", update.message.text) async with AsyncDynamoDB(table_name="Stazioni") as dynamo: query = ( update.message.text.replace("/", "").replace("erfiume_bot", "").strip() ) - fuzzy_query = process.extractOne(query, KNOWN_STATIONS)[0] - stazione = await dynamo.get_matching_station(fuzzy_query) - if stazione and update.message: - message = stazione.create_station_message() - if query != fuzzy_query: - message += ( - "\nSe non é la stazione corretta prova ad affinare la ricerca." - ) + fuzzy_query = process.extractOne( + query, KNOWN_STATIONS, score_cutoff=FUZZ_SCORE_CUTOFF + ) + logger.info(query) + if fuzzy_query: + stazione = await dynamo.get_matching_station(fuzzy_query[0]) + if stazione and update.message: + message = stazione.create_station_message() + if query != fuzzy_query[0]: + message += "\nSe non é la stazione corretta prova ad affinare la ricerca." await context.bot.send_message( chat_id=update.effective_chat.id, text=message, diff --git a/app/erfiume_bot.py b/app/erfiume_bot.py index 100edb2..8f9d4d6 100644 --- a/app/erfiume_bot.py +++ b/app/erfiume_bot.py @@ -25,5 +25,4 @@ def handler(event: dict[str, Any], context: LambdaContext) -> dict[str, Any]: logger.exception(traceback.format_exc()) return {"statusCode": 501} - logger.info("Successfully processed event") return {"statusCode": 200}