diff --git a/frontend/src/plate/functions/lex-specialis/lex-specialis.test.ts b/frontend/src/plate/functions/lex-specialis/lex-specialis.test.ts index c74325dbc..7f278cd94 100644 --- a/frontend/src/plate/functions/lex-specialis/lex-specialis.test.ts +++ b/frontend/src/plate/functions/lex-specialis/lex-specialis.test.ts @@ -56,7 +56,7 @@ describe('lex specialis', () => { expect(actualResult).toBe(MORE_SPECIFIC_TITLE); }); - it('handle ties', () => { + it('should return tie only if there are no unique scores', () => { expect.assertions(2); const [actualStatus, actualResult] = lexSpecialis( @@ -65,15 +65,50 @@ describe('lex specialis', () => { 'y1', ['h1'], [], - [GENERIC_TITLE, GENERIC_TITLE], + [GENERIC_TITLE, GENERIC_TITLE, SPECIFIC_TITLE, SPECIFIC_TITLE], ); + expect(actualStatus).toBe(LexSpecialisStatus.TIE); expect(actualResult).toStrictEqual([ + { maltekstseksjon: SPECIFIC_TITLE, score: 22 }, + { maltekstseksjon: SPECIFIC_TITLE, score: 22 }, { maltekstseksjon: GENERIC_TITLE, score: 20 }, { maltekstseksjon: GENERIC_TITLE, score: 20 }, ]); }); + it('should fallback to result with unique score if more specific results were ties', () => { + expect.assertions(2); + + const [actualStatus, actualResult] = lexSpecialis( + TemplateIdEnum.KLAGEVEDTAK_V2, + TemplateSections.TITLE, + 'y1', + ['h1'], + [], + [MORE_SPECIFIC_TITLE, SPECIFIC_TITLE, GENERIC_TITLE, MORE_SPECIFIC_TITLE, SPECIFIC_TITLE], + ); + + expect(actualStatus).toBe(LexSpecialisStatus.FOUND); + expect(actualResult).toBe(GENERIC_TITLE); + }); + + it('should fallback to most specific result with unique score if more specific results were ties', () => { + expect.assertions(2); + + const [actualStatus, actualResult] = lexSpecialis( + TemplateIdEnum.KLAGEVEDTAK_V2, + TemplateSections.TITLE, + 'y1', + ['h1'], + [], + [MORE_SPECIFIC_TITLE, MORE_SPECIFIC_TITLE, GENERIC_TITLE, SPECIFIC_TITLE], + ); + + expect(actualStatus).toBe(LexSpecialisStatus.FOUND); + expect(actualResult).toBe(SPECIFIC_TITLE); + }); + it('no utfall does not match texts with utfall', () => { expect.assertions(2); diff --git a/frontend/src/plate/functions/lex-specialis/lex-specialis.ts b/frontend/src/plate/functions/lex-specialis/lex-specialis.ts index 6e4d584bf..956667c64 100644 --- a/frontend/src/plate/functions/lex-specialis/lex-specialis.ts +++ b/frontend/src/plate/functions/lex-specialis/lex-specialis.ts @@ -82,17 +82,29 @@ export const lexSpecialis = ( .filter((st) => st.score > INCLUDE_THRESHOLD) .sort((a, b) => b.score - a.score); - const [first, second] = scoredTexts; + const [first] = scoredTexts; if (first === undefined) { return NONE_RESULT; } - if (first.score === second?.score) { - return getTieResult(scoredTexts.filter((st) => st.score === first.score)); + // biome-ignore lint/suspicious/noEvolvingTypes: Needs to be reassignable. + let firstUntiedText = null; + + for (const scoredText of scoredTexts) { + const hasUniqueScore = scoredTexts.every((st) => scoredText === st || scoredText.score !== st.score); + + if (hasUniqueScore) { + firstUntiedText = scoredText; + break; + } + } + + if (firstUntiedText === null) { + return getTieResult(scoredTexts); } - return [LexSpecialisStatus.FOUND, first.maltekstseksjon]; + return [LexSpecialisStatus.FOUND, firstUntiedText.maltekstseksjon]; }; const getScore = (