(chore) save parse from json language detections (#2119)

Co-authored-by: ggurdin <ggurdin@gmail.com>
Co-authored-by: ggurdin <46800240+ggurdin@users.noreply.github.com>
This commit is contained in:
Wilson 2025-03-12 11:44:34 -04:00 committed by GitHub
parent b039630266
commit 9cdba97a1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 13 deletions

View file

@ -109,12 +109,25 @@ class IGCTextData {
fullText: content.text,
);
final LanguageDetectionResponse detections = event.detections != null
? LanguageDetectionResponse.fromJson({
LanguageDetectionResponse detections = defaultDetections;
if (event.detections != null) {
try {
detections = LanguageDetectionResponse.fromJson({
"detections": event.detections,
"full_text": content.text,
});
} catch (e, s) {
ErrorHandler.logError(
e: e,
s: s,
m: "Error parsing detections in IGCTextData.fromRepresentationEvent",
data: {
"detections": event.detections,
"full_text": content.text,
})
: defaultDetections;
},
);
}
}
return IGCTextData(
detections: detections,

View file

@ -10,9 +10,20 @@ class LanguageDetection {
});
factory LanguageDetection.fromJson(Map<String, dynamic> json) {
final dynamic confValue = json[ModelKey.confidence];
double confidence;
if (confValue is String) {
confidence = double.parse(confValue);
} else if (confValue is double) {
confidence = confValue;
} else if (confValue is int) {
confidence = confValue.toDouble();
} else {
throw TypeError();
}
return LanguageDetection(
langCode: json[ModelKey.langCode],
confidence: json[ModelKey.confidence],
confidence: confidence,
);
}

View file

@ -510,14 +510,26 @@ class PangeaMessageEvent {
}
Future<String?> representationByDetectedLanguage() async {
final resp = await LanguageDetectionRepo.get(
MatrixState.pangeaController.userController.accessToken,
request: LanguageDetectionRequest(
text: _latestEdit.body,
senderl1: l1Code,
senderl2: l2Code,
),
);
LanguageDetectionResponse? resp;
try {
resp = await LanguageDetectionRepo.get(
MatrixState.pangeaController.userController.accessToken,
request: LanguageDetectionRequest(
text: _latestEdit.body,
senderl1: l1Code,
senderl2: l2Code,
),
);
} catch (e, s) {
ErrorHandler.logError(
e: e,
s: s,
data: {
"event": _event.toJson(),
},
);
return null;
}
final langCode = resp.detections.firstOrNull?.langCode;
if (langCode == null) return null;