From b79faf0126143b01e111e132e6d12b1a66a86c2d Mon Sep 17 00:00:00 2001 From: Motin Date: Mon, 30 Mar 2020 13:38:22 +0300 Subject: [PATCH] Also apply fuzzy matching to answer option matching, but don't disregard characters that may change the nature of the answer option --- .../answerOptionMatchesFactualAnswer.spec.ts | 20 +++++++++++++++++++ src/lib/answerOptionMatchesFactualAnswer.ts | 5 +++-- .../keyNormalizerForSlightlyFuzzyLookups.ts | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/lib/answerOptionMatchesFactualAnswer.spec.ts b/src/lib/answerOptionMatchesFactualAnswer.spec.ts index 7279e01..428aa34 100644 --- a/src/lib/answerOptionMatchesFactualAnswer.spec.ts +++ b/src/lib/answerOptionMatchesFactualAnswer.spec.ts @@ -25,6 +25,26 @@ const testAnswerOptionMatchesFactualAnswer: Macro = ( factualAnswer: "1% ", expectedOutput: true }, + { + answerOption: "1%", + factualAnswer: "1%,", + expectedOutput: true + }, + { + answerOption: "1%,", + factualAnswer: "1%", + expectedOutput: true + }, + { + answerOption: '1%"', + factualAnswer: "1%", + expectedOutput: true + }, + { + answerOption: "1%", + factualAnswer: '1%"', + expectedOutput: true + }, { answerOption: "30-40%", factualAnswer: "24%", diff --git a/src/lib/answerOptionMatchesFactualAnswer.ts b/src/lib/answerOptionMatchesFactualAnswer.ts index 6072134..6658ab4 100644 --- a/src/lib/answerOptionMatchesFactualAnswer.ts +++ b/src/lib/answerOptionMatchesFactualAnswer.ts @@ -1,4 +1,5 @@ import { extractNumericalPartsOfAnswerOption } from "./extractNumericalPartsOfAnswerOption"; +import { keyNormalizerForSlightlyFuzzyLookups } from "./keyNormalizerForSlightlyFuzzyLookups"; /** * @hidden @@ -7,8 +8,8 @@ export function answerOptionMatchesFactualAnswer( answerOption: string, factualAnswer: string ): boolean { - answerOption = answerOption.trim().toLocaleLowerCase(); - factualAnswer = factualAnswer.trim().toLocaleLowerCase(); + answerOption = keyNormalizerForSlightlyFuzzyLookups(answerOption); + factualAnswer = keyNormalizerForSlightlyFuzzyLookups(factualAnswer); if (answerOption === factualAnswer) { return true; } diff --git a/src/lib/keyNormalizerForSlightlyFuzzyLookups.ts b/src/lib/keyNormalizerForSlightlyFuzzyLookups.ts index 119990e..19e15b5 100644 --- a/src/lib/keyNormalizerForSlightlyFuzzyLookups.ts +++ b/src/lib/keyNormalizerForSlightlyFuzzyLookups.ts @@ -10,5 +10,5 @@ export function keyNormalizerForSlightlyFuzzyLookups( const trimmedLowerCasedWithoutDiacritics = removeDiacritics( lookupKey.trim().toLowerCase() ); - return trimmedLowerCasedWithoutDiacritics.replace(/[^a-z0-9 ()]/g, ""); + return trimmedLowerCasedWithoutDiacritics.replace(/[^a-z0-9%\-.,<> ()]/g, ""); }