diff options
| author | Fuwn <[email protected]> | 2024-02-06 05:14:21 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-02-06 05:14:21 -0800 |
| commit | e6086a3e91b5e0d3be4f86d27a0dba31198c55bd (patch) | |
| tree | 3d8aef65cd1d92246ef4afd7e562277e75ddae75 /src/lib | |
| parent | fix(badge): broken imgur links (diff) | |
| download | due.moe-e6086a3e91b5e0d3be4f86d27a0dba31198c55bd.tar.xz due.moe-e6086a3e91b5e0d3be4f86d27a0dba31198c55bd.zip | |
fix(match): revert to old matcher
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/Media/Anime/Airing/Subtitled/match.ts | 46 |
1 files changed, 17 insertions, 29 deletions
diff --git a/src/lib/Media/Anime/Airing/Subtitled/match.ts b/src/lib/Media/Anime/Airing/Subtitled/match.ts index 82dc5e7d..b4176080 100644 --- a/src/lib/Media/Anime/Airing/Subtitled/match.ts +++ b/src/lib/Media/Anime/Airing/Subtitled/match.ts @@ -30,39 +30,27 @@ const secondsUntil = (targetTime: string, targetDay: string) => { return secondsDifference > 0 ? secondsDifference : secondsDifference + 7 * 24 * 60 * 60; }; -const normalizeTitle = (title: string | null) => { - return (title || '') +const normalizeTitle = (title: string | null) => + (title || '') .toLowerCase() - .replace(/season \d+|s\d+/g, '') - .replace(/\b(\d)(st|nd|rd|th)\b/g, '$1') - .replace(/\b(part|pt)\b/gi, '') + .replace(/\b(s|season|part|cour)\s*\d+/g, '') + .replace(/[\W_]+/g, ' ') .trim(); -}; -const findClosestMatch = (times: Time[], titles: string[]) => { - const normalizedTitles = titles - .map((title) => title?.toLowerCase().trim()) - .filter((title) => title); - const timesWithNormalizedTitles = times - .filter((time) => time.title !== null && time.title.trim() !== '') - .map((time) => ({ ...time, title: (time.title as string).toLowerCase().trim() })); - - for (const time of timesWithNormalizedTitles) - if (normalizedTitles.includes(time.title)) return time; - - let bestMatch = null; - let highestMatchScore = 0; - - for (const time of timesWithNormalizedTitles) - for (const title of normalizedTitles) - if (time.title.includes(title) || title.includes(time.title)) { - const matchScore = Math.min(title.length, time.title.length); - - if (matchScore > highestMatchScore) { - highestMatchScore = matchScore; - bestMatch = time; - } +const findClosestMatch = (times: Time[], titles: string[]): Time | null => { + let bestMatch: Time | null = null; + let highestScore = 0; + + times.forEach((time) => { + titles.map(normalizeTitle).forEach((title) => { + const similarityScore = stringSimilarity.compareTwoStrings(normalizeTitle(time.title), title); + + if (similarityScore > highestScore) { + highestScore = similarityScore; + bestMatch = time; } + }); + }); return bestMatch; }; |