Skip to content

Commit 9cdba97

Browse files
WilsonLeggurdin
andauthored
(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>
1 parent b039630 commit 9cdba97

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

lib/pangea/choreographer/models/igc_text_data_model.dart

+17-4
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,25 @@ class IGCTextData {
109109
fullText: content.text,
110110
);
111111

112-
final LanguageDetectionResponse detections = event.detections != null
113-
? LanguageDetectionResponse.fromJson({
112+
LanguageDetectionResponse detections = defaultDetections;
113+
if (event.detections != null) {
114+
try {
115+
detections = LanguageDetectionResponse.fromJson({
116+
"detections": event.detections,
117+
"full_text": content.text,
118+
});
119+
} catch (e, s) {
120+
ErrorHandler.logError(
121+
e: e,
122+
s: s,
123+
m: "Error parsing detections in IGCTextData.fromRepresentationEvent",
124+
data: {
114125
"detections": event.detections,
115126
"full_text": content.text,
116-
})
117-
: defaultDetections;
127+
},
128+
);
129+
}
130+
}
118131

119132
return IGCTextData(
120133
detections: detections,

lib/pangea/choreographer/models/language_detection_model.dart

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@ class LanguageDetection {
1010
});
1111

1212
factory LanguageDetection.fromJson(Map<String, dynamic> json) {
13+
final dynamic confValue = json[ModelKey.confidence];
14+
double confidence;
15+
if (confValue is String) {
16+
confidence = double.parse(confValue);
17+
} else if (confValue is double) {
18+
confidence = confValue;
19+
} else if (confValue is int) {
20+
confidence = confValue.toDouble();
21+
} else {
22+
throw TypeError();
23+
}
1324
return LanguageDetection(
1425
langCode: json[ModelKey.langCode],
15-
confidence: json[ModelKey.confidence],
26+
confidence: confidence,
1627
);
1728
}
1829

lib/pangea/events/event_wrappers/pangea_message_event.dart

+20-8
Original file line numberDiff line numberDiff line change
@@ -510,14 +510,26 @@ class PangeaMessageEvent {
510510
}
511511

512512
Future<String?> representationByDetectedLanguage() async {
513-
final resp = await LanguageDetectionRepo.get(
514-
MatrixState.pangeaController.userController.accessToken,
515-
request: LanguageDetectionRequest(
516-
text: _latestEdit.body,
517-
senderl1: l1Code,
518-
senderl2: l2Code,
519-
),
520-
);
513+
LanguageDetectionResponse? resp;
514+
try {
515+
resp = await LanguageDetectionRepo.get(
516+
MatrixState.pangeaController.userController.accessToken,
517+
request: LanguageDetectionRequest(
518+
text: _latestEdit.body,
519+
senderl1: l1Code,
520+
senderl2: l2Code,
521+
),
522+
);
523+
} catch (e, s) {
524+
ErrorHandler.logError(
525+
e: e,
526+
s: s,
527+
data: {
528+
"event": _event.toJson(),
529+
},
530+
);
531+
return null;
532+
}
521533

522534
final langCode = resp.detections.firstOrNull?.langCode;
523535
if (langCode == null) return null;

0 commit comments

Comments
 (0)