The Kenmei manga entry to match.
The list of AniList manga entries to compare against.
Optional partial match engine configuration.
A MangaMatchResult containing the best matches and status.
export function findBestMatches(
kenmeiManga: KenmeiManga,
anilistMangaList: AniListManga[],
config: Partial<MatchEngineConfig> = {},
): MangaMatchResult {
const matchConfig = { ...DEFAULT_MATCH_CONFIG, ...config };
// Calculate match scores for each AniList manga and sort descending
const matchResults = anilistMangaList
.map((manga) => {
const matchScore = scoreMatch(kenmeiManga, manga, matchConfig);
return {
manga,
confidence: matchScore.confidence,
isExactMatch: matchScore.isExactMatch,
matchedField: matchScore.matchedField,
} as const;
})
.sort((a, b) => b.confidence - a.confidence);
// Take only the top matches and exclude zero-confidence entries
const topMatches = matchResults
.slice(0, matchConfig.maxMatches)
.filter((m) => m.confidence > 0);
if (topMatches.length === 0) {
return {
kenmeiManga,
anilistMatches: [],
status: "pending",
selectedMatch: undefined,
matchDate: new Date(),
};
}
if (topMatches[0].isExactMatch) {
return {
kenmeiManga,
anilistMatches: topMatches.map(({ manga, confidence }) => ({
manga,
confidence,
})),
status: "matched",
selectedMatch: topMatches[0].manga,
matchDate: new Date(),
};
}
const hasHighConfidence =
topMatches[0].confidence >= matchConfig.confidenceThreshold &&
(topMatches.length === 1 ||
topMatches[0].confidence - topMatches[1].confidence > 20);
if (hasHighConfidence) {
return {
kenmeiManga,
anilistMatches: topMatches.map(({ manga, confidence }) => ({
manga,
confidence,
})),
status: "matched",
selectedMatch: topMatches[0].manga,
matchDate: new Date(),
};
}
// Multiple potential matches or low confidence => pending
return {
kenmeiManga,
anilistMatches: topMatches.map(({ manga, confidence }) => ({
manga,
confidence,
})),
status: "pending",
selectedMatch: undefined,
matchDate: new Date(),
};
}
Finds the best matches for a Kenmei manga entry from a list of AniList entries.