feat: fuzzy matching for select someone

This commit is contained in:
anavoi 2025-09-03 21:18:42 +02:00
commit fe5e9613f2
2 changed files with 16 additions and 1 deletions

16
main.py
View file

@ -3,6 +3,7 @@ from typing import Dict, List, Optional
from dataclasses import dataclass
import sys
from logger import log
from fuzzywuzzy import process
@dataclass
class Player:
@ -97,9 +98,22 @@ class Game:
prompt = prompt or "Enter the name of the player: "
while True:
selected = input(prompt).strip()
# exact match
if selected in self.players:
return selected
log.info("This player doesn't exist.")
# fuzzy matching
match = process.extract(selected, self.players, limit=1)
log.debug(match)
if match:
fuzz_player, fuzz_score = match[0][0], match[0][1] # player name, score
log.debug(fuzz_player)
log.debug(fuzz_score)
if fuzz_score >= 60:
log.info("You meant %s!", fuzz_player.name)
log.debug("Fuzz score: %s" % fuzz_score)
return fuzz_player
# -------------------------
# game actions

1
requirements.txt Normal file
View file

@ -0,0 +1 @@
fuzzywuzzy==0.18.0