Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/src/model/old_product_result.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

201 changes: 201 additions & 0 deletions lib/src/model/robotoff_nutrient_extraction.dart
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 {
Comment on lines +6 to +7
Copy link
Contributor

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.

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!));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please deal with the case where completedAt is null, as you don't reject that possibility.

return insights?.last;
}

NutrientEntity? getNutrientEntity(String nutrientKey) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 String _getTag(Nutrient nutrient, PerSize perSize) in nutriments.dart.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite obscure String? unit and String? value.
Are these randoms strings, or are we supposed to get typical values from a list.


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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider using PerSize.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That String key is a vague.
Please consider adding getters with Nutrient and PerSize parameters.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there known types?

final NutrientDataWrapper? data;
final String? timestamp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a date?

@JsonKey(name: 'completed_at')
final String? completedAt;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dynamic?


const NutrientExtractionInsight({
this.insightId,
this.barcode,
Comment on lines +168 to +170
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure using final fields is very helpful.
That forces us to maintain the list of fields in the constructor.

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);
}
181 changes: 181 additions & 0 deletions lib/src/model/robotoff_nutrient_extraction.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading