feat: fuzzy matching for select someone
This commit is contained in:
parent
9ee5e4383f
commit
fe5e9613f2
2 changed files with 16 additions and 1 deletions
16
main.py
16
main.py
|
|
@ -3,6 +3,7 @@ from typing import Dict, List, Optional
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import sys
|
import sys
|
||||||
from logger import log
|
from logger import log
|
||||||
|
from fuzzywuzzy import process
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Player:
|
class Player:
|
||||||
|
|
@ -97,9 +98,22 @@ class Game:
|
||||||
prompt = prompt or "Enter the name of the player: "
|
prompt = prompt or "Enter the name of the player: "
|
||||||
while True:
|
while True:
|
||||||
selected = input(prompt).strip()
|
selected = input(prompt).strip()
|
||||||
|
|
||||||
|
# exact match
|
||||||
if selected in self.players:
|
if selected in self.players:
|
||||||
return selected
|
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
|
# 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