Karan Adhikari commited on
Fix fuzzy match
Browse files- Backend/helper/metadata.py +19 -10
Backend/helper/metadata.py
CHANGED
|
@@ -16,11 +16,19 @@ from rapidfuzz import fuzz
|
|
| 16 |
from guessit import guessit as _guessit
|
| 17 |
from difflib import SequenceMatcher
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
return
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
return SequenceMatcher(None, a, b).ratio()
|
| 25 |
|
| 26 |
|
|
@@ -126,12 +134,13 @@ def _score_candidate(query_title: str, query_year: Optional[int], result_title:
|
|
| 126 |
score = _title_similarity(query_title, result_title)
|
| 127 |
if query_year and result_year:
|
| 128 |
diff = abs(int(query_year) - result_year)
|
| 129 |
-
if diff
|
| 130 |
-
score = min(1.0, score + 0.20)
|
| 131 |
-
elif diff == 1:
|
| 132 |
-
score = min(1.0, score + 0.07)
|
| 133 |
-
elif diff > 2:
|
| 134 |
score = max(0.0, score - 0.10 * (diff - 2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
return score
|
| 136 |
|
| 137 |
def _build_query_variants(title: str, year: Optional[int] = None) -> List[str]:
|
|
|
|
| 16 |
from guessit import guessit as _guessit
|
| 17 |
from difflib import SequenceMatcher
|
| 18 |
|
| 19 |
+
def _fuzzy_ratio(a: str, b: str) -> float:
|
| 20 |
+
if not a or not b:
|
| 21 |
+
return 0.0
|
| 22 |
+
try:
|
| 23 |
+
set_ratio = fuzz.token_set_ratio(a, b) / 100.0
|
| 24 |
+
sort_ratio = fuzz.token_sort_ratio(a, b) / 100.0
|
| 25 |
+
a_tokens, b_tokens = a.split(), b.split()
|
| 26 |
+
if a_tokens and b_tokens:
|
| 27 |
+
coverage = min(len(a_tokens), len(b_tokens)) / max(len(a_tokens), len(b_tokens))
|
| 28 |
+
else:
|
| 29 |
+
coverage = 0.0
|
| 30 |
+
return max(sort_ratio, set_ratio * coverage)
|
| 31 |
+
except Exception:
|
| 32 |
return SequenceMatcher(None, a, b).ratio()
|
| 33 |
|
| 34 |
|
|
|
|
| 134 |
score = _title_similarity(query_title, result_title)
|
| 135 |
if query_year and result_year:
|
| 136 |
diff = abs(int(query_year) - result_year)
|
| 137 |
+
if diff > 2:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
score = max(0.0, score - 0.10 * (diff - 2))
|
| 139 |
+
elif score >= 0.5:
|
| 140 |
+
if diff == 0:
|
| 141 |
+
score = min(1.0, score + 0.20)
|
| 142 |
+
elif diff == 1:
|
| 143 |
+
score = min(1.0, score + 0.07)
|
| 144 |
return score
|
| 145 |
|
| 146 |
def _build_query_variants(title: str, year: Optional[int] = None) -> List[str]:
|