diff options
| author | Fuwn <[email protected]> | 2024-02-02 03:53:20 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-02-02 03:53:20 -0800 |
| commit | 803227f3c623f09db679171bb375bcbb4000aeb4 (patch) | |
| tree | aae0f6efe94087862442a3b1e4bc5044af52cc16 /src/lib | |
| parent | feat(likes): show avatar on hover (diff) | |
| download | due.moe-803227f3c623f09db679171bb375bcbb4000aeb4.tar.xz due.moe-803227f3c623f09db679171bb375bcbb4000aeb4.zip | |
fix(match): improve anime to time
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/Media/Anime/Airing/Subtitled/match.ts | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/lib/Media/Anime/Airing/Subtitled/match.ts b/src/lib/Media/Anime/Airing/Subtitled/match.ts index 33271dd6..26782a8b 100644 --- a/src/lib/Media/Anime/Airing/Subtitled/match.ts +++ b/src/lib/Media/Anime/Airing/Subtitled/match.ts @@ -40,27 +40,31 @@ const normalizeTitle = (title: string | null) => { }; const findClosestMatch = (times: Time[], titles: string[]) => { - let closestMatch: Time | null = null; - - titles.forEach((title) => { - const normalisedTitle = normalizeTitle(title); - - times.forEach((time) => { - const normalisedTimeTitle = normalizeTitle(time.title); - const distance = stringSimilarity.compareTwoStrings(normalisedTitle, normalisedTimeTitle); - - if ( - distance > 0.5 && - (closestMatch === null || - distance > - stringSimilarity.compareTwoStrings(normalisedTitle, normalizeTitle(closestMatch.title))) - ) { - closestMatch = time; + 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; + } } - }); - }); - return closestMatch as Time | null; + return bestMatch; }; export const findClosestMedia = (media: Media[], matchFor: string) => { |