| |
| |
| |
|
|
| export interface Sample { |
| text: string; |
| score: number; |
| } |
|
|
| const STOP = new Set([ |
| 'the','a','an','is','are','was','were','of','in','to','and','or','it','its', |
| 'this','that','with','for','on','as','by','be','at','from','but','not','if', |
| ]); |
|
|
| export function rerankSamples( |
| samples: Sample[], |
| context: string, |
| query: string, |
| ): { best: Sample | null; bestScore: number } { |
| if (!samples.length) return { best: null, bestScore: 0 }; |
|
|
| const terms = query.toLowerCase().match(/[a-z]{3,}/g) ?? []; |
| const keywords = terms.filter(t => !STOP.has(t)); |
|
|
| let best = samples[0]; |
| let bestScore = -Infinity; |
|
|
| for (const s of samples) { |
| let score = s.score; |
| const lower = s.text.toLowerCase(); |
| for (const kw of keywords) { |
| if (lower.includes(kw)) score += 0.3; |
| } |
| |
| const words = s.text.split(/\s+/).filter(Boolean).length; |
| if (words < 5) score -= 2; |
| if (score > bestScore) { |
| bestScore = score; |
| best = s; |
| } |
| } |
|
|
| return { best, bestScore }; |
| } |
|
|