-
-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added robotoff nutrient extraction #1026
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
import '../interface/json_object.dart'; | ||
|
||
part 'robotoff_nutrient_extraction.g.dart'; | ||
|
||
@JsonSerializable() | ||
class RobotoffNutrientExtractionResult extends JsonObject { | ||
final String? status; | ||
final int? count; | ||
final List<NutrientExtractionInsight>? insights; | ||
|
||
const RobotoffNutrientExtractionResult({ | ||
this.status, | ||
this.count, | ||
this.insights, | ||
}); | ||
|
||
NutrientExtractionInsight? get getLatestInsights { | ||
insights?.sort((a, b) => a.completedAt!.compareTo(b.completedAt!)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please deal with the case where |
||
return insights?.last; | ||
} | ||
|
||
NutrientEntity? getNutrientEntity(String nutrientKey) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with most methods regarding nutrients, please consider re-using or using something similar to |
||
return getLatestInsights?.data?.nutrients?[nutrientKey]; | ||
} | ||
|
||
factory RobotoffNutrientExtractionResult.fromJson( | ||
Map<String, dynamic> json) => | ||
_$RobotoffNutrientExtractionResultFromJson(json); | ||
|
||
@override | ||
Map<String, dynamic> toJson() => | ||
_$RobotoffNutrientExtractionResultToJson(this); | ||
} | ||
|
||
@JsonSerializable() | ||
class NutrientEntity { | ||
final int? start; | ||
final int? end; | ||
final String? text; | ||
final String? unit; | ||
final double? score; | ||
final bool? valid; | ||
final String? value; | ||
final String? entity; | ||
@JsonKey(name: 'char_start') | ||
final int? charStart; | ||
@JsonKey(name: 'char_end') | ||
final int? charEnd; | ||
|
||
const NutrientEntity({ | ||
this.start, | ||
this.end, | ||
this.text, | ||
this.unit, | ||
this.score, | ||
this.valid, | ||
this.value, | ||
this.entity, | ||
this.charStart, | ||
this.charEnd, | ||
}); | ||
|
||
factory NutrientEntity.fromJson(Map<String, dynamic> json) => | ||
_$NutrientEntityFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$NutrientEntityToJson(this); | ||
} | ||
|
||
@JsonSerializable() | ||
class NutrientAnnotationData { | ||
final String? unit; | ||
final String? value; | ||
Comment on lines
+72
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quite obscure |
||
|
||
const NutrientAnnotationData({ | ||
this.unit, | ||
this.value, | ||
}); | ||
|
||
factory NutrientAnnotationData.fromJson(Map<String, dynamic> json) => | ||
_$NutrientAnnotationDataFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$NutrientAnnotationDataToJson(this); | ||
} | ||
|
||
@JsonSerializable() | ||
class NutrientAnnotation { | ||
final Map<String, NutrientAnnotationData>? nutrients; | ||
@JsonKey(name: 'serving_size') | ||
final String? servingSize; | ||
@JsonKey(name: 'nutrition_data_per') | ||
final String? nutritionDataPer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please consider using |
||
|
||
const NutrientAnnotation({ | ||
this.nutrients, | ||
this.servingSize, | ||
this.nutritionDataPer, | ||
}); | ||
|
||
factory NutrientAnnotation.fromJson(Map<String, dynamic> json) => | ||
_$NutrientAnnotationFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$NutrientAnnotationToJson(this); | ||
} | ||
|
||
@JsonSerializable() | ||
class NutrientDataWrapper { | ||
final Map<String, List<NutrientEntity>>? entities; | ||
final Map<String, NutrientEntity>? nutrients; | ||
Comment on lines
+108
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That |
||
final NutrientAnnotation? annotation; | ||
@JsonKey(name: 'was_updated') | ||
final bool? wasUpdated; | ||
|
||
const NutrientDataWrapper({ | ||
this.entities, | ||
this.nutrients, | ||
this.annotation, | ||
this.wasUpdated, | ||
}); | ||
|
||
factory NutrientDataWrapper.fromJson(Map<String, dynamic> json) => | ||
_$NutrientDataWrapperFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$NutrientDataWrapperToJson(this); | ||
} | ||
|
||
@JsonSerializable() | ||
class NutrientExtractionInsight extends JsonObject { | ||
@JsonKey(name: 'id') | ||
final String? insightId; | ||
final String? barcode; | ||
final String? type; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there known types? |
||
final NutrientDataWrapper? data; | ||
final String? timestamp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that a date? |
||
@JsonKey(name: 'completed_at') | ||
final String? completedAt; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that a date? |
||
final int? annotation; | ||
@JsonKey(name: 'annotated_result') | ||
final int? annotatedResult; | ||
@JsonKey(name: 'n_votes') | ||
final int? nVotes; | ||
final String? username; | ||
final List<String>? countries; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Localized countries? Country codes? |
||
final List<String>? brands; | ||
@JsonKey(name: 'process_after') | ||
final String? processAfter; | ||
@JsonKey(name: 'value_tag') | ||
final String? valueTag; | ||
final String? value; | ||
@JsonKey(name: 'source_image') | ||
final String? sourceImage; | ||
@JsonKey(name: 'automatic_processing') | ||
final bool? automaticProcessing; | ||
@JsonKey(name: 'server_type') | ||
final String? serverType; | ||
@JsonKey(name: 'unique_scans_n') | ||
final int? uniqueScansN; | ||
@JsonKey(name: 'reserved_barcode') | ||
final bool? reservedBarcode; | ||
final String? predictor; | ||
@JsonKey(name: 'predictor_version') | ||
final String? predictorVersion; | ||
final List<String>? campaign; | ||
final double? confidence; | ||
@JsonKey(name: 'bounding_box') | ||
final dynamic boundingBox; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const NutrientExtractionInsight({ | ||
this.insightId, | ||
this.barcode, | ||
Comment on lines
+168
to
+170
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure using |
||
this.type, | ||
this.data, | ||
this.timestamp, | ||
this.completedAt, | ||
this.annotation, | ||
this.annotatedResult, | ||
this.nVotes, | ||
this.username, | ||
this.countries, | ||
this.brands, | ||
this.processAfter, | ||
this.valueTag, | ||
this.value, | ||
this.sourceImage, | ||
this.automaticProcessing, | ||
this.serverType, | ||
this.uniqueScansN, | ||
this.reservedBarcode, | ||
this.predictor, | ||
this.predictorVersion, | ||
this.campaign, | ||
this.confidence, | ||
this.boundingBox, | ||
}); | ||
|
||
factory NutrientExtractionInsight.fromJson(Map<String, dynamic> json) => | ||
_$NutrientExtractionInsightFromJson(json); | ||
|
||
@override | ||
Map<String, dynamic> toJson() => _$NutrientExtractionInsightToJson(this); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally speaking, I would prefer one file per class (I'm from Java) and an explicit
Robotoff
prefix for all class names.Not the most important part of my review, though.