Skip to content

Commit

Permalink
Food groups facet and export (#6358)
Browse files Browse the repository at this point in the history
* food groups / groupes alimentaires

* add index on food_groups_tags

* add food_groups_tags

* added strings for food groups

* fix export_database.pl and add food_groups

* export food_groups

* stats for food groups
  • Loading branch information
stephanegigandet committed Jan 28, 2022
1 parent 2e8bbd8 commit 204ac1c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/ProductOpener/Config_off.pm
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ periods_after_opening nutrients food_groups
nova_group
pnns_groups_1
pnns_groups_2
food_groups
states
brand_owner
ecoscore_score_fr
Expand Down
27 changes: 27 additions & 0 deletions lib/ProductOpener/DataQualityFood.pm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ use ProductOpener::Tags qw(:all);
use ProductOpener::Food qw(:all);
use ProductOpener::Ecoscore qw(:all);

use Data::DeepAccess qw(deep_exists);

use Log::Any qw($log);

=head1 HARDCODED BRAND LISTS
Expand Down Expand Up @@ -1158,6 +1160,30 @@ sub check_ecoscore_data($) {
}


=head2 check_food_groups( PRODUCT_REF )
Add info tags about food groups.
=cut

sub check_food_groups($) {

my $product_ref = shift;

for (my $level = 1; $level <= 3; $level++) {

if (deep_exists($product_ref, "food_groups_tags", $level - 1)) {
push @{$product_ref->{data_quality_info_tags}}, 'en:food-groups-' . $level . '-known';
}
else {
push @{$product_ref->{data_quality_info_tags}}, 'en:food-groups-' . $level . '-unknown';
}
}

return;
}


=head2 check_quality_food( PRODUCT_REF )
Run all quality checks defined in the module.
Expand All @@ -1180,6 +1206,7 @@ sub check_quality_food($) {
check_categories($product_ref);
compare_nutriscore_with_value_from_producer($product_ref);
check_ecoscore_data($product_ref);
check_food_groups($product_ref);

return;
}
Expand Down
10 changes: 5 additions & 5 deletions scripts/export_database.pl
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
use ProductOpener::Food qw/:all/;
use ProductOpener::Ingredients qw/:all/;
use ProductOpener::Data qw/:all/;
use ProductOpener::Text qw/:all/;

# for RDF export: replace xml_escape() with xml_escape_NFC()
use Unicode::Normalize;
use URI::Escape::XS;

use CGI qw/:cgi :form escapeHTML/;
use URI::Escape::XS;
use Storable qw/dclone/;
use Encode;
use JSON::PP;
Expand Down Expand Up @@ -94,7 +94,7 @@ sub sanitize_field_content {
}


my %tags_fields = (packaging => 1, brands => 1, categories => 1, labels => 1, origins => 1, manufacturing_places => 1, emb_codes=>1, cities=>1, allergen=>1, traces => 1, additives => 1, ingredients_from_palm_oil => 1, ingredients_that_may_be_from_palm_oil => 1, countries => 1, states=>1);
my %tags_fields = (packaging => 1, brands => 1, categories => 1, labels => 1, origins => 1, manufacturing_places => 1, emb_codes=>1, cities=>1, allergen=>1, traces => 1, additives => 1, ingredients_from_palm_oil => 1, ingredients_that_may_be_from_palm_oil => 1, countries => 1, states=>1, food_groups=>1);


my %langs = ();
Expand Down Expand Up @@ -146,7 +146,7 @@ sub sanitize_field_content {
print STDERR "Write file: $csv_filename.temp\n";
print STDERR "Write file: $rdf_filename.temp\n";

open (my $OUT, ">:encoding(UTF-8)", "$csv_filename.temp");
open (my $OUT, ">:encoding(UTF-8)", "$csv_filename.temp") or die("Cannot write $csv_filename.temp: $!\n");
open (my $RDF, ">:encoding(UTF-8)", "$rdf_filename.temp");
open (my $BAD, ">:encoding(UTF-8)", "$log_filename");

Expand Down Expand Up @@ -290,9 +290,9 @@ sub sanitize_field_content {
$field_value = $product_ref->{$field . "_" . $l};
}

# Eco-Score
# Eco-Score data is stored in ecoscore_data.(grades|scores).(language code)
if (($field =~ /^ecoscore_(score|grade)_(\w\w)/) and (defined $product_ref->{ecoscore_data})) {
$field_value = $product_ref->{ecoscore_data}{$1 . "_" . $2};
$field_value = ($product_ref->{ecoscore_data}{$1 . "s"}{$2} // "");
}

if ($field_value ne '') {
Expand Down

0 comments on commit 204ac1c

Please sign in to comment.