From 6ddb18b6ec523a1604b0c94741ddd8a227db46b5 Mon Sep 17 00:00:00 2001 From: ggurdin <46800240+ggurdin@users.noreply.github.com> Date: Wed, 26 Feb 2025 15:51:09 -0500 Subject: [PATCH] fix: added fix to remove matches if they don't align with tokens and to use characters when replacing originalInput (#1948) --- .../controllers/igc_controller.dart | 33 ++++++++++++++++++- .../models/igc_text_data_model.dart | 10 +++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/pangea/choreographer/controllers/igc_controller.dart b/lib/pangea/choreographer/controllers/igc_controller.dart index 29d2f6521..51f3b01d2 100644 --- a/lib/pangea/choreographer/controllers/igc_controller.dart +++ b/lib/pangea/choreographer/controllers/igc_controller.dart @@ -140,7 +140,38 @@ class IgcController { filteredMatches.remove(match); } } - igcTextData!.matches = filteredMatches; + + // If there are any matches that don't match up with token offsets, + // this indicates and choreographer bug. Remove them. + final tokens = igcTextData!.tokens; + final List confirmedMatches = List.from(filteredMatches); + for (final match in filteredMatches) { + final substring = match.match.fullText.characters + .skip(match.match.offset) + .take(match.match.length); + final trimmed = substring.toString().trim().characters; + final matchOffset = (match.match.offset + match.match.length) - + (substring.length - trimmed.length); + final hasStartMatch = tokens.any( + (token) => token.text.offset == match.match.offset, + ); + final hasEndMatch = tokens.any( + (token) => token.text.offset + token.text.length == matchOffset, + ); + if (!hasStartMatch || !hasEndMatch) { + confirmedMatches.clear(); + ErrorHandler.logError( + m: "Match offset and/or length do not tokens with matching offset and length. This is a choreographer bug.", + data: { + "match": match.toJson(), + "tokens": tokens.map((e) => e.toJson()).toList(), + }, + ); + break; + } + } + + igcTextData!.matches = confirmedMatches; // TODO - for each new match, // check if existing igcTextData has one and only one match with the same error text and correction diff --git a/lib/pangea/choreographer/models/igc_text_data_model.dart b/lib/pangea/choreographer/models/igc_text_data_model.dart index ba1574554..04592d245 100644 --- a/lib/pangea/choreographer/models/igc_text_data_model.dart +++ b/lib/pangea/choreographer/models/igc_text_data_model.dart @@ -175,11 +175,11 @@ class IGCTextData { final SpanChoice replacement = pangeaMatch.match.choices![choiceIndex]; - originalInput = originalInput.replaceRange( - pangeaMatch.match.offset, - pangeaMatch.match.offset + pangeaMatch.match.length, - replacement.value, - ); + final newStart = originalInput.characters.take(pangeaMatch.match.offset); + final newEnd = originalInput.characters + .skip(pangeaMatch.match.offset + pangeaMatch.match.length); + final fullText = newStart + replacement.value.characters + newEnd; + originalInput = fullText.toString(); int startIndex; int endIndex;