From 1dab6466e88786c65de200e3afa9d67931accc86 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 18 Nov 2024 09:15:15 +0000 Subject: [PATCH] - Move to a ranked match for fuzzy matching --- ui/workers/gather_evolutions_worker.py | 37 +++++++++++++++++++------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/ui/workers/gather_evolutions_worker.py b/ui/workers/gather_evolutions_worker.py index c17b21a..1d12f47 100644 --- a/ui/workers/gather_evolutions_worker.py +++ b/ui/workers/gather_evolutions_worker.py @@ -439,14 +439,29 @@ class GatherEvolutions(QRunnable): stripped_form = self.strip_pokemon_name(name, form) + best_match = None + best_score = -1 for entry in results: stripped_db_form = self.strip_pokemon_name(entry["name"], entry["form_name"]) - if self.fuzzy_match_form(stripped_form, stripped_db_form, threshold): - return entry["pfic"] + stripped_db_form_genderless = self.strip_gender_from_form(stripped_db_form) + #if self.fuzzy_match_form(stripped_form, stripped_db_form, threshold): + # return entry["pfic"] + match_score = self.fuzzy_match_form(stripped_form, stripped_db_form) + genderless_score = self.fuzzy_match_form(stripped_form, stripped_db_form_genderless) - stripped_db_form = self.strip_gender_from_form(stripped_db_form) - if self.fuzzy_match_form(stripped_form, stripped_db_form, threshold): - return entry["pfic"] + + #if self.fuzzy_match_form(stripped_form, stripped_db_form, threshold): + # return entry["pfic"] + + if match_score > best_score: + best_match = entry["pfic"] + best_score = match_score + + if genderless_score > best_score: + best_match = entry["pfic"] + best_score = genderless_score + + return best_match if best_score >= threshold else None # Some times we get a form for a pokemon that doesn't really have one. #if len(results) > 1 and form != None and gender and threshold != 100: @@ -468,7 +483,11 @@ class GatherEvolutions(QRunnable): form_name = form_name.replace("Male", "").replace("male", "").strip() return form_name - def fuzzy_match_form(self, form1: str, form2: str, threshold: int = 80) -> bool: - if form1 is None or form2 is None: - return form1 == form2 - return fuzz.ratio(form1.lower(), form2.lower()) >= threshold \ No newline at end of file + def fuzzy_match_form(self, form1: str, form2: str) -> bool: + if form1 is None: + form1 = "" + + if form2 is None: + form2 = "" + + return fuzz.ratio(form1.lower(), form2.lower()) \ No newline at end of file