diff --git a/main.py b/main.py index a2adb4d..9ab617f 100644 --- a/main.py +++ b/main.py @@ -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 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..516af7e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +fuzzywuzzy==0.18.0 \ No newline at end of file