diff --git a/src/classes/osudle.py b/src/classes/osudle.py index 4c84f12..7787e44 100644 --- a/src/classes/osudle.py +++ b/src/classes/osudle.py @@ -42,10 +42,9 @@ def check_guess(self, message: str) -> bool: return ( message.channel is self.interaction.channel and not message.author.bot - and humanizer.fuzzy_string_match( + and humanizer.song_title_match( message.content, self.current_beatmapset.title, - permit_low_match=False, ) ) diff --git a/src/common/humanizer.py b/src/common/humanizer.py index 4413941..738aada 100644 --- a/src/common/humanizer.py +++ b/src/common/humanizer.py @@ -5,6 +5,7 @@ from datetime import timedelta +from common.regex import alphanumeric_rx from thefuzz import fuzz FUZZY_MATCH_THRESHOLD = 80 @@ -87,3 +88,14 @@ def milliseconds_to_duration(position: float) -> str: def fuzzy_string_match(str1: str, str2: str, permit_low_match: bool = False) -> bool: THRESHOLD = FUZZY_MATCH_THRESHOLD_LOW if permit_low_match else FUZZY_MATCH_THRESHOLD return fuzz.ratio(str1.casefold(), str2.casefold()) >= THRESHOLD + + +def song_title_match(guess: str, answer: str) -> bool: + alphanumeric = alphanumeric_rx.split(answer)[0] + guess_length = len(guess.split(" ")) + partial_words = " ".join(answer.split(" ")[:guess_length]) + return ( + fuzzy_string_match(guess, answer) + or fuzzy_string_match(guess, alphanumeric) + or fuzzy_string_match(guess, partial_words) + ) diff --git a/src/common/regex.py b/src/common/regex.py index e71f97a..d48c769 100644 --- a/src/common/regex.py +++ b/src/common/regex.py @@ -12,3 +12,4 @@ user_link_rx = re.compile( r"(https?)://(?Posu|lazer)\.ppy\.sh/u(sers)?/(?P[0-9]+)", ) +alphanumeric_rx = re.compile(r"[^a-zA-Z0-9]")