fix(providers): match non-Latin titles by letters of every script

This commit is contained in:
imSp4rky
2026-11-28 14:34:15 -07:00
parent daa9caa02b
commit 5167f4f9d9
7 changed files with 94 additions and 22 deletions
+15 -4
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
import logging
import re
import unicodedata
from abc import ABCMeta, abstractmethod
from dataclasses import dataclass, field
from difflib import SequenceMatcher
@@ -13,7 +14,7 @@ log = logging.getLogger("METADATA")
HEADERS = {"User-Agent": "unshackle-tags/1.0"}
STRIP_RE = re.compile(r"[^a-z0-9]+", re.I)
LATIN_ACCENT_RE = re.compile(r"[\u0300-\u036f]")
YEAR_RE = re.compile(r"\s*\(?[12][0-9]{3}\)?$")
@@ -87,14 +88,24 @@ class MetadataProvider(metaclass=ABCMeta):
def clean(s: str) -> str:
return STRIP_RE.sub("", s).lower()
"""Fold case and Latin accents, and keep the letters, digits and combining marks of any script.
Marks outside the Latin accent block stay, so kana voicing and Thai or Indic vowel signs still tell titles apart.
"""
s = LATIN_ACCENT_RE.sub("", unicodedata.normalize("NFKD", s).casefold())
return unicodedata.normalize("NFC", "".join(c for c in s if c.isalnum() or unicodedata.category(c)[0] == "M"))
def strip_year(s: str) -> str:
return YEAR_RE.sub("", s).strip()
def title_ratio(a: str, b: str) -> float:
"""Similarity of two cleaned titles from 0 to 1; a title with no letters or digits scores 0."""
a, b = clean(a), clean(b)
return SequenceMatcher(None, a, b).ratio() if a and b else 0.0
def fuzzy_match(a: str, b: str, threshold: float = 0.8) -> bool:
"""Return True if ``a`` and ``b`` match closely."""
ratio = SequenceMatcher(None, clean(a), clean(b)).ratio()
return ratio >= threshold
return title_ratio(a, b) >= threshold
+2 -4
View File
@@ -1,10 +1,8 @@
from __future__ import annotations
from difflib import SequenceMatcher
import requests
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, clean
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, title_ratio
GRAPHQL_URL = "https://graphql.anilist.co"
@@ -148,4 +146,4 @@ class AniListProvider(MetadataProvider):
def match_ratio(node: dict, title: str) -> float:
names = (node.get("title") or {}).values()
return max((SequenceMatcher(None, clean(title), clean(n)).ratio() for n in names if n), default=0.0)
return max((title_ratio(title, n) for n in names if n), default=0.0)
+2 -3
View File
@@ -2,11 +2,10 @@ from __future__ import annotations
import hashlib
import json
from difflib import SequenceMatcher
import requests
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, clean, fuzzy_match
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, fuzzy_match, title_ratio
GRAPHQL_URL = "https://caching.graphql.imdb.com/"
@@ -144,7 +143,7 @@ class IMDBProvider(MetadataProvider):
def match_ratio(node: dict, title: str) -> float:
names = [(node.get("titleText") or {}).get("text"), (node.get("originalTitleText") or {}).get("text")]
return max((SequenceMatcher(None, clean(title), clean(n)).ratio() for n in names if n), default=0.0)
return max((title_ratio(title, n) for n in names if n), default=0.0)
def persisted_query_missing(body: dict) -> bool:
+2 -4
View File
@@ -1,12 +1,10 @@
from __future__ import annotations
from difflib import SequenceMatcher
import requests
from langcodes import Language
from unshackle.core.config import config
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, clean, fuzzy_match
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, fuzzy_match, title_ratio
KIND_TO_TYPE: dict[str, str] = {
"movie": "movie",
@@ -86,7 +84,7 @@ class OMDBProvider(MetadataProvider):
name = candidate.get("Title") or ""
if not name:
continue
ratio = SequenceMatcher(None, clean(title), clean(name)).ratio()
ratio = title_ratio(title, name)
if ratio > best_ratio:
candidate_year = parse_year(candidate.get("Year"))
if year and candidate_year and abs(year - candidate_year) > 1:
+2 -4
View File
@@ -1,11 +1,9 @@
from __future__ import annotations
from difflib import SequenceMatcher
import requests
from unshackle.core.config import config
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, clean, strip_year
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, strip_year, title_ratio
class TMDBProvider(MetadataProvider):
@@ -55,7 +53,7 @@ class TMDBProvider(MetadataProvider):
candidates = [c for c in candidates if c]
for candidate in candidates:
ratio = SequenceMatcher(None, clean(search_title), clean(candidate)).ratio()
ratio = title_ratio(search_title, candidate)
if ratio > best_ratio:
best_ratio = ratio
best_id = result.get("id")
+2 -3
View File
@@ -1,13 +1,12 @@
from __future__ import annotations
from collections.abc import Collection
from difflib import SequenceMatcher
from typing import Any
import requests
from unshackle.core.config import config
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, clean, strip_year
from unshackle.core.providers._base import ExternalIds, MetadataProvider, MetadataResult, strip_year, title_ratio
def primary_language(data: dict) -> str | None:
@@ -58,7 +57,7 @@ def pick_match(results: list[dict], search_title: str, year: int | None) -> tupl
for candidate in [result.get("name"), *(result.get("aliases") or [])]:
if not candidate:
continue
ratio = SequenceMatcher(None, clean(search_title), clean(candidate)).ratio()
ratio = title_ratio(search_title, candidate)
if ratio > best_ratio:
best, best_title, best_ratio = result, candidate, ratio
return best, best_title, best_ratio