MuSP-Bench / evaluation /normalization-guards.mjs
milan477's picture
Add offline MuSP scorer and usage instructions
a97edac verified
Raw History Blame Contribute Delete
1.78 kB
// Diagnostic only: do not import this into production evaluation.
// A loss-only experiment isolates false positives caused by the named bugs.
// Existing matches remain necessary: no synonyms or tolerances are added.
import {matchesAcceptedAnswer, stripResponseWrapperResidue} from './answer-normalization.mjs';
export const numericFormats=new Set([
'Positive integer','Signed integer','Integer','Bar number (digits)',
'Bar number (digits) or NA','Bar range (N-N)','Bar and beat',
'Beat number (digits)','Beat number (fraction)',
'Fractional beat number with optional beat prefix','Fraction written as N/N',
'Count in quarter notes','Count of eighth-note durations','Count and ornamentation',
'Count followed by note value','Count in number of beats (according to applicable meter)',
'Ratio','Section label followed by start-bar number','Bar number, yes-or-no',
]);
export function romanSignature(value){
return [...stripResponseWrapperResidue(value).normalize('NFKC').matchAll(/(?<![A-Za-z])[#b♯♭]?[IViv]+(?![A-Za-z])/gu)].map(m=>m[0]);
}
export function numberSignature(value){
return [...stripResponseWrapperResidue(value).normalize('NFKC').matchAll(/[+-]?\d+(?:\.\d+)?/gu)].map(m=>String(Number(m[0])));
}
export function diagnosticMatch(answer, reference, question, {roman=true,numeric=true}={}){
if(!matchesAcceptedAnswer(answer,reference,{fullQuestion:question.full_question}))return false;
if(roman&&question.answer_format==='Roman-numeral chord symbol'
&&JSON.stringify(romanSignature(answer))!==JSON.stringify(romanSignature(reference)))return false;
if(numeric&&numericFormats.has(question.answer_format)
&&JSON.stringify(numberSignature(answer))!==JSON.stringify(numberSignature(reference)))return false;
return true;
}