You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fluffychat/lib/pangea/choreographer/models/igc_text_data_model.dart

284 lines
8.7 KiB
Dart

2 years ago
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:collection/collection.dart';
import 'package:fluffychat/pangea/choreographer/models/choreo_record.dart';
import 'package:fluffychat/pangea/choreographer/models/pangea_match_model.dart';
import 'package:fluffychat/pangea/choreographer/models/span_data.dart';
import 'package:fluffychat/pangea/common/constants/model_keys.dart';
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
import 'package:fluffychat/pangea/events/event_wrappers/pangea_representation_event.dart';
import 'package:fluffychat/pangea/events/models/representation_content_model.dart';
import 'package:fluffychat/widgets/matrix.dart';
2 years ago
2 years ago
// import 'package:language_tool/language_tool.dart';
2 years ago
class IGCTextData {
String originalInput;
String? fullTextCorrection;
List<PangeaMatch> matches;
String userL1;
String userL2;
bool enableIT;
bool enableIGC;
bool loading = false;
IGCTextData({
required this.originalInput,
required this.fullTextCorrection,
required this.matches,
required this.userL1,
required this.userL2,
required this.enableIT,
required this.enableIGC,
});
factory IGCTextData.fromJson(Map<String, dynamic> json) {
return IGCTextData(
matches: json[_matchesKey] != null
? (json[_matchesKey] as Iterable)
.map<PangeaMatch>(
(e) {
return PangeaMatch.fromJson(e as Map<String, dynamic>);
},
)
.toList()
.cast<PangeaMatch>()
: [],
originalInput: json["original_input"],
fullTextCorrection: json["full_text_correction"],
userL1: json[ModelKey.userL1],
userL2: json[ModelKey.userL2],
enableIT: json["enable_it"],
enableIGC: json["enable_igc"],
);
}
factory IGCTextData.fromRepresentationEvent(
RepresentationEvent event,
String userL1,
String userL2,
) {
final PangeaRepresentation content = event.content;
final List<PangeaMatch> matches = event.choreo?.choreoSteps
.map((step) => step.acceptedOrIgnoredMatch)
.whereType<PangeaMatch>()
.toList() ??
[];
String originalInput = content.text;
if (matches.isNotEmpty) {
originalInput = matches.first.match.fullText;
}
return IGCTextData(
originalInput: originalInput,
fullTextCorrection: content.text,
matches: matches,
userL1: userL1,
userL2: userL2,
enableIT: true,
enableIGC: true,
);
}
2 years ago
static const String _matchesKey = "matches";
Map<String, dynamic> toJson() => {
"original_input": originalInput,
"full_text_correction": fullTextCorrection,
_matchesKey: matches.map((e) => e.toJson()).toList(),
ModelKey.userL1: userL1,
ModelKey.userL2: userL2,
"enable_it": enableIT,
"enable_igc": enableIGC,
};
// reconstruct fullText based on accepted match
//update offsets in existing matches to reflect the change
//if existing matches overlap with the accepted one, remove them??
void acceptReplacement(
int matchIndex,
int choiceIndex,
) async {
//should be already added to choreoRecord
//TODO - that should be done in the same function to avoid error potential
2 years ago
final PangeaMatch pangeaMatch = matches[matchIndex];
if (pangeaMatch.match.choices == null) {
debugger(when: kDebugMode);
ErrorHandler.logError(
m: "pangeaMatch.match.choices is null in acceptReplacement",
data: {
"match": pangeaMatch.match.toJson(),
},
2 years ago
);
return;
}
final SpanChoice replacement = pangeaMatch.match.choices![choiceIndex];
2 years ago
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();
2 years ago
// update offsets in existing matches to reflect the change
// Question - remove matches that overlap with the accepted one?
2 years ago
// see case of "quiero ver un fix"
matches.removeAt(matchIndex);
for (final match in matches) {
match.match.fullText = originalInput;
if (match.match.offset > pangeaMatch.match.offset) {
match.match.offset +=
replacement.value.length - pangeaMatch.match.length;
2 years ago
}
}
}
List<int> matchIndicesByOffset(int offset) {
final List<int> matchesForOffset = [];
for (final (index, match) in matches.indexed) {
if (match.isOffsetInMatchSpan(offset)) {
matchesForOffset.add(index);
}
}
return matchesForOffset;
}
2 years ago
int getTopMatchIndexForOffset(int offset) {
final List<int> matchesForToken = matchIndicesByOffset(offset);
final int matchIndex = matchesForToken.indexWhere((matchIndex) {
2 years ago
final match = matches[matchIndex];
return (enableIT && (match.isITStart || match.isl1SpanMatch)) ||
(enableIGC && match.isGrammarMatch);
});
if (matchIndex == -1) return -1;
return matchesForToken[matchIndex];
2 years ago
}
static TextStyle underlineStyle(Color color) => TextStyle(
decoration: TextDecoration.underline,
decorationColor: color,
decorationThickness: 5,
);
TextSpan getSpanItem({
required int start,
required int end,
TextStyle? style,
}) {
return TextSpan(
text: originalInput.characters.getRange(start, end).toString(),
style: style,
);
}
int? get _openMatchIndex {
final RegExp pattern = RegExp(r'span_card_overlay_\d+');
final String? matchingKeys =
MatrixState.pAnyState.getMatchingOverlayKeys(pattern).firstOrNull;
if (matchingKeys == null) return null;
final int? index = int.tryParse(matchingKeys.split("_").last);
if (index == null ||
matches.length <= index ||
matches[index].status != PangeaMatchStatus.open) {
return null;
}
return index;
}
2 years ago
//PTODO - handle multitoken spans
/// Returns a list of [TextSpan]s used to display the text in the input field
/// with the appropriate styling for each error match.
2 years ago
List<TextSpan> constructTokenSpan({
ChoreoRecordStep? choreoStep,
2 years ago
TextStyle? defaultStyle,
}) {
final stepMatch = choreoStep?.acceptedOrIgnoredMatch;
final List<PangeaMatch> textSpanMatches = List.from(matches);
if (stepMatch != null && stepMatch.status == PangeaMatchStatus.automatic) {
textSpanMatches.add(stepMatch);
}
2 years ago
final List<TextSpan> items = [];
if (loading) {
return [
TextSpan(
text: originalInput,
style: defaultStyle,
),
];
}
textSpanMatches.sort((a, b) => a.match.offset.compareTo(b.match.offset));
final List<List<int>> matchRanges = textSpanMatches
.map(
(match) => [
match.match.offset,
match.match.length + match.match.offset,
],
)
.toList();
// create a pointer to the current index in the original input
// and iterate until the pointer has reached the end of the input
int currentIndex = 0;
while (currentIndex < originalInput.characters.length) {
// check if the pointer is at a match, and if so, get the index of the match
final int matchIndex = matchRanges.indexWhere(
(range) => currentIndex >= range[0] && currentIndex < range[1],
);
final bool inMatch = matchIndex != -1;
if (inMatch) {
// if the pointer is in a match, then add that match to items
// and then move the pointer to the end of the match range
final PangeaMatch match = textSpanMatches[matchIndex];
items.add(
getSpanItem(
start: match.match.offset,
end: match.match.offset + match.match.length,
style: match.textStyle(
matchIndex,
_openMatchIndex,
defaultStyle,
),
2 years ago
),
);
currentIndex = match.match.offset + match.match.length;
} else {
// otherwise, if the pointer is not at a match, then add all the text
// until the next match (or, if there is not next match, the end of the
// text) to items and move the pointer to the start of the next match
final int nextIndex = matchRanges
.firstWhereOrNull(
(range) => range[0] > currentIndex,
)
?.first ??
originalInput.characters.length;
items.add(
getSpanItem(
start: currentIndex,
end: nextIndex,
style: defaultStyle,
),
);
currentIndex = nextIndex;
}
}
2 years ago
return items;
}
}