Skip to content

Commit

Permalink
Lex specialis: Fallback to less specific result with unique score if …
Browse files Browse the repository at this point in the history
…more specific results were tied
  • Loading branch information
eriksson-daniel committed Dec 9, 2024
1 parent 2c3d122 commit 88ce3e6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
39 changes: 37 additions & 2 deletions frontend/src/plate/functions/lex-specialis/lex-specialis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);

Expand Down
20 changes: 16 additions & 4 deletions frontend/src/plate/functions/lex-specialis/lex-specialis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,29 @@ export const lexSpecialis = <T extends IConsumerRichText | IMaltekstseksjon>(
.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 = <T extends IConsumerRichText | IMaltekstseksjon>(
Expand Down

0 comments on commit 88ce3e6

Please sign in to comment.