diff --git a/app/erfiume/tgbot.py b/app/erfiume/tgbot.py index 1839161..5d83039 100644 --- a/app/erfiume/tgbot.py +++ b/app/erfiume/tgbot.py @@ -17,6 +17,7 @@ MessageHandler, filters, ) +from thefuzz import process # type: ignore[import-untyped] if TYPE_CHECKING: from aws_lambda_powertools.utilities.typing import LambdaContext @@ -175,11 +176,15 @@ async def handle_private_message( 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: - stazione = await dynamo.get_matching_station( - update.message.text.replace("/", "").strip() - ) + 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." + ) await context.bot.send_message( chat_id=update.effective_chat.id, text=message, @@ -203,11 +208,17 @@ async def handle_group_message( 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: - stazione = await dynamo.get_matching_station( + 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." + ) await context.bot.send_message( chat_id=update.effective_chat.id, text=message, diff --git a/app/poetry.lock b/app/poetry.lock index f84d17a..6b430a4 100644 --- a/app/poetry.lock +++ b/app/poetry.lock @@ -2333,6 +2333,20 @@ files = [ {file = "sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc"}, ] +[[package]] +name = "thefuzz" +version = "0.22.1" +description = "Fuzzy string matching in python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "thefuzz-0.22.1-py3-none-any.whl", hash = "sha256:59729b33556850b90e1093c4cf9e618af6f2e4c985df193fdf3c5b5cf02ca481"}, + {file = "thefuzz-0.22.1.tar.gz", hash = "sha256:7138039a7ecf540da323792d8592ef9902b1d79eb78c147d4f20664de79f3680"}, +] + +[package.dependencies] +rapidfuzz = ">=3.0.0,<4.0.0" + [[package]] name = "tomlkit" version = "0.13.2" @@ -3520,4 +3534,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "bafd51aacf0a900831407dee4da7a10e7469a7ddfb30c41fa7beaafc9420fac9" +content-hash = "9bb664df4cccdb34bb5af6322b7302ec83748dc5b62911e61894efce2f60e550" diff --git a/app/pyproject.toml b/app/pyproject.toml index 8c30c01..57b2b17 100644 --- a/app/pyproject.toml +++ b/app/pyproject.toml @@ -13,6 +13,7 @@ aioboto3 = "^13.1.1" poetry-dotenv-plugin = "^0.2.0" python = "^3.12" python-telegram-bot = "^21.5" +thefuzz = "^0.22.1" [tool.poetry.group.dev.dependencies] awscli-local = "^0.22.0"