import { INTEGRATION_METHOD_WEIGHTS, IntegrationMethodWeight, } from "../constants/integration-method.js"; import { Result } from "./toss-payments-bm25-calculator.js"; import { TossPaymentsDocument } from "./toss-payments-document.js"; const DEFAULT_WEIGHT = 1.0; export class IntegrationMethodWeightCalculator { // 문서 link 기반 가중치를 생성자에서 1회 precompute private readonly weightByDocId: Map; constructor( documents: TossPaymentsDocument[], config: IntegrationMethodWeight[] = INTEGRATION_METHOD_WEIGHTS ) { this.weightByDocId = new Map( documents.map((doc) => [doc.id, this.resolveWeight(doc.link, config)]) ); } /** * BM25 결과에 연동방식별 가중치를 적용하고 재정렬 */ apply(results: Result[]): Result[] { return results .map((result) => ({ ...result, score: result.score * (this.weightByDocId.get(result.id) ?? DEFAULT_WEIGHT), })) .sort((a, b) => b.score - a.score); } /** * link의 소문자 부분문자열을 config 순서대로 검사하여 first-match-wins로 가중치 확정 */ private resolveWeight( link: string, config: IntegrationMethodWeight[] ): number { const url = link.toLowerCase(); for (const group of config) { if (group.keywords.some((keyword) => url.includes(keyword))) { return group.weight; } } return DEFAULT_WEIGHT; } }