mirror of
https://github.com/Benexl/FastAnime.git
synced 2026-02-05 03:16:50 -08:00
👌 Make finding best_match_title more robust
This commit is contained in:
36
viu_cli/cli/utils/search.py
Normal file
36
viu_cli/cli/utils/search.py
Normal file
@@ -0,0 +1,36 @@
|
||||
"""Search functionality."""
|
||||
|
||||
from viu_cli.core.utils.fuzzy import fuzz
|
||||
from viu_cli.core.utils.normalizer import normalize_title
|
||||
from viu_cli.libs.provider.anime.types import SearchResult, ProviderName
|
||||
from viu_cli.libs.media_api.types import MediaItem
|
||||
|
||||
|
||||
def find_best_match_title(
|
||||
provider_results_map: dict[str, SearchResult],
|
||||
provider: ProviderName,
|
||||
media_item: MediaItem,
|
||||
) -> str:
|
||||
"""Find the best match title using fuzzy matching for both the english AND romaji title.
|
||||
|
||||
Parameters:
|
||||
provider_results_map (dict[str, SearchResult]): The map of provider results.
|
||||
provider (ProviderName): The provider name from the config.
|
||||
media_item (MediaItem): The media item to match.
|
||||
|
||||
Returns:
|
||||
str: The best match title.
|
||||
"""
|
||||
return max(
|
||||
provider_results_map.keys(),
|
||||
key=lambda p_title: max(
|
||||
fuzz.ratio(
|
||||
normalize_title(p_title, provider.value).lower(),
|
||||
(media_item.title.romaji or "").lower(),
|
||||
),
|
||||
fuzz.ratio(
|
||||
normalize_title(p_title, provider.value).lower(),
|
||||
(media_item.title.english or "").lower(),
|
||||
),
|
||||
),
|
||||
)
|
||||
Reference in New Issue
Block a user