feat(aniwatch): Improves title matching logic

- Enhances title matching accuracy when fetching Aniwatch IDs.
- Prioritizes user-preferred titles and falls back to English titles.
- Ensures only one fetch call is made per title if both english and userPreferred title are same.
- Adds a score threshold to filter low-quality matches.
- Fixes a bug where the episode list was not being returned.
This commit is contained in:
2025-04-23 09:32:52 -04:00
parent 00720565b4
commit b8ae211956
3 changed files with 96 additions and 21 deletions

View File

@@ -1,17 +1,40 @@
function findBestMatch(mainString: string, targets: string[]): string | null {
if (targets.length === 0) return null;
if (targets.length === 0) {
return null;
}
if (targets.length === 1) {
return targets[0];
}
let bestMatch = targets[0];
let highestScore = stringMatchingAlgorithm(mainString, bestMatch);
for (let i = 1; i < targets.length; i++) {
const currentScore = stringMatchingAlgorithm(mainString, targets[i]);
if (currentScore === 1) {
bestMatch = targets[i];
break;
}
console.log(
"searching best match",
`'${mainString}'`,
`'${targets[i]}'`,
currentScore,
highestScore,
);
if (currentScore > highestScore) {
highestScore = currentScore;
bestMatch = targets[i];
}
}
console.log(
"findBestMatch",
`'${mainString}'`,
`'${bestMatch}'`,
highestScore,
);
return bestMatch;
}
@@ -23,23 +46,39 @@ type Title = {
export const findBestMatchingTitle = (
title: Partial<Title>,
titlesToSearch: Partial<Title>[],
): string | null => {
): { title: string | null; score: number } => {
const { english, userPreferred } = title;
let match;
let expectedSuffix = "";
if ((match = english?.match(/my hero academia.+([0-9])$/))) {
expectedSuffix = match[1];
}
console.log("searching best user preferred title", userPreferred);
const userPreferredBestMatch = userPreferred
? findBestMatch(
userPreferred,
titlesToSearch
.map((title) => title.userPreferred)
.filter((title) => title !== undefined),
.map((title) => title.userPreferred?.toLowerCase())
.filter(
(title) =>
title !== undefined &&
(!expectedSuffix || title.endsWith(expectedSuffix)),
),
)
: null;
console.log("searching best english title", english);
const englishBestMatch = english
? findBestMatch(
english,
titlesToSearch
.map((title) => title.english)
.filter((title) => title !== undefined),
.map((title) => title.english?.toLowerCase())
.filter(
(title) =>
title !== undefined &&
(!expectedSuffix || title.endsWith(expectedSuffix)),
),
)
: null;
@@ -54,10 +93,10 @@ export const findBestMatchingTitle = (
console.log(title.userPreferred, userPreferredScore);
if (userPreferredScore >= englishScore) {
console.log("User preferred", userPreferredBestMatch);
return userPreferredBestMatch;
return { title: userPreferredBestMatch, score: userPreferredScore };
} else {
console.log("English", englishBestMatch);
return englishBestMatch;
return { title: englishBestMatch, score: englishScore };
}
};