Global refactoring #1
2 changed files with 16 additions and 1 deletions
feat: fuzzy matching for select someone
commit
fe5e9613f2
16
main.py
16
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
|
||||
|
|
|
|||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
fuzzywuzzy==0.18.0
|
||||
Loading…
Add table
Add a link
Reference in a new issue