Skip to content

Commit

Permalink
enh: fuzz search (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
notdodo authored Sep 24, 2024
1 parent f685cce commit 77d4d1e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
19 changes: 15 additions & 4 deletions app/erfiume/tgbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion app/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 77d4d1e

Please sign in to comment.