Files
AIOStreams/packages/core/src/parser/utils.ts
T
2025-09-13 17:32:20 +01:00

38 lines
884 B
TypeScript

import { extract, FuzzballExtractOptions } from 'fuzzball';
import { createLogger } from '../utils';
const logger = createLogger('parser');
export function titleMatch(
parsedTitle: string,
titles: string[],
options: {
threshold: number;
} & Exclude<FuzzballExtractOptions, 'returnObjects'>
) {
const { threshold, ...extractOptions } = options;
const results = extract(parsedTitle, titles, {
...extractOptions,
returnObjects: true,
});
const highestScore =
results.reduce(
(max: number, result: { choice: string; score: number; key: number }) => {
return Math.max(max, result.score);
},
0
) / 100;
return highestScore >= threshold;
}
export function normaliseTitle(title: string) {
return title
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^\p{L}\p{N}+]/gu, '')
.toLowerCase();
}