-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
Food.pm
3266 lines (2637 loc) · 116 KB
/
Food.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of Product Opener.
#
# Product Opener
# Copyright (C) 2011-2023 Association Open Food Facts
# Contact: contact@openfoodfacts.org
# Address: 21 rue des Iles, 94100 Saint-Maur des Fossés, France
#
# Product Opener is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
=encoding UTF-8
=head1 NAME
ProductOpener::Food - functions related to food products and nutrition
=head1 DESCRIPTION
C<ProductOpener::Food> contains functions specific to food products, in particular
related to nutrition facts. This module provides functions It does not contain functions related to ingredients which
are in the C<ProductOpener::Ingredients> module.
..
=cut
package ProductOpener::Food;
use ProductOpener::PerlStandards;
use Exporter qw< import >;
BEGIN {
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
@EXPORT_OK = qw(
%nutriments_labels
%cc_nutriment_table
%nutriments_tables
%other_nutriments_lists
%nutriments_lists
@nutrient_levels
%categories_nutriments_per_country
&normalize_nutriment_value_and_modifier
&assign_nid_modifier_value_and_unit
&canonicalize_nutriment
&fix_salt_equivalent
&is_beverage_for_nutrition_score_2021
&is_fat_oil_nuts_seeds_for_nutrition_score
&is_water_for_nutrition_score
&has_category_that_should_have_prepared_nutrition_data
&check_availability_of_nutrients_needed_for_nutriscore
&compute_nutriscore_data
&compute_nutriscore
&compute_nova_group
&compute_nutrition_data_per_100g_and_per_serving
&compute_unknown_nutrients
&compute_nutrient_levels
&evaluate_nutrient_level
&compute_units_of_alcohol
&compute_estimated_nutrients
&compare_nutriments
&extract_nutrition_from_image
&default_unit_for_nid
&create_nutrients_level_taxonomy
&assign_categories_properties_to_product
&assign_nutriments_values_from_request_parameters
&check_nutriscore_categories_exist_in_taxonomy
&get_nutrient_unit
); # symbols to export on request
%EXPORT_TAGS = (all => [@EXPORT_OK]);
}
use vars @EXPORT_OK;
use ProductOpener::Store qw/get_string_id_for_lang retrieve/;
use ProductOpener::Config qw/:all/;
use ProductOpener::Paths qw/%BASE_DIRS/;
use ProductOpener::Lang qw/$lc %Lang %Langs lang/;
use ProductOpener::Tags qw/:all/;
use ProductOpener::Images qw/extract_text_from_image/;
use ProductOpener::Nutriscore qw/compute_nutriscore_score_and_grade/;
use ProductOpener::Numbers qw/convert_string_to_number round_to_max_decimal_places/;
use ProductOpener::Ingredients
qw/estimate_nutriscore_2021_milk_percent_from_ingredients estimate_nutriscore_2023_red_meat_percent_from_ingredients/;
use ProductOpener::Text qw/remove_tags_and_quote/;
use ProductOpener::FoodGroups qw/compute_food_groups/;
use ProductOpener::Units qw/:all/;
use ProductOpener::Products qw(&remove_fields);
use ProductOpener::Display qw/single_param/;
use ProductOpener::APIProductWrite qw/skip_protected_field/;
use ProductOpener::NutritionEstimation qw/estimate_nutrients_from_ingredients/;
use Hash::Util;
use Encode;
use URI::Escape::XS;
use CGI qw/:cgi :form escapeHTML/;
use Data::DeepAccess qw(deep_set deep_get);
use Storable qw/dclone/;
use Log::Any qw($log);
sub check_nutriscore_categories_exist_in_taxonomy() {
# Normalize values listed in Config.pm
# Canonicalize the list of categories used to compute Nutri-Score, so that Nutri-Score
# computation does not change if we change the canonical English name of a category
foreach my $categories_list_id (
qw(
categories_not_considered_as_beverages_for_nutriscore_2021
categories_not_considered_as_beverages_for_nutriscore_2023
categories_exempted_from_nutriscore
categories_not_exempted_from_nutriscore
categories_exempted_from_nutrient_levels
)
)
{
my $categories_list_ref = $options{$categories_list_id};
if (defined $categories_list_ref) {
foreach my $category_id (@{$categories_list_ref}) {
$category_id = canonicalize_taxonomy_tag("en", "categories", $category_id);
# Check that the entry exists
if (not exists_taxonomy_tag("categories", $category_id)) {
$log->error(
"Category used in Nutri-Score and listed in Config.pm \$options\{$categories_list_id\} does not exist in the categories taxonomy.",
{category_id => $category_id}
) if $log->is_error();
die(
"Category used in Nutri-Score and listed in Config.pm \$options\{$categories_list_id\} does not exist in the categories taxonomy."
);
}
}
}
}
return;
}
# Load nutrient stats for all categories and countries
# the stats are displayed on category pages and used in product pages,
# as well as in data quality checks and improvement opportunity detection
if (opendir(my $dh, "$BASE_DIRS{PRIVATE_DATA}/categories_stats")) {
foreach my $file (readdir($dh)) {
if ($file =~ /categories_nutriments_per_country.(\w+).sto$/) {
my $country_cc = $1;
$categories_nutriments_per_country{$country_cc}
= retrieve(
"$BASE_DIRS{PRIVATE_DATA}/categories_stats/categories_nutriments_per_country.$country_cc.sto");
}
}
closedir $dh;
}
# Unicode category 'Punctuation, Dash', SWUNG DASH and MINUS SIGN
my $dashes = qr/(?:\p{Pd}|\N{U+2053}|\N{U+2212})/i;
=head2 normalize_nutriment_value_and_modifier ( $value_ref, $modifier_ref )
Each nutrient value is entered as a string (by users on the product edit form,
or through the API). The string value may not always be numeric (e.g. it can include a < sign).
This function normalizes the string value to remove signs, and stores extra information in the "modifier" field.
=head3 Arguments
=head4 string value reference $value_ref
Input string value reference. The value will be normalized.
=head4 modifier reference $modifier_ref
Output modifier reference.
=head3 Possible return values
=head4 value
- 0 if the input value indicates traces
- Number (as a string)
- undef for 'NaN' (not a number, sometimes sent by broken API clients)
=head4 modifier
<, >, ≤, ≥, ~ character sign, for lesser, greater, lesser or equal, greater or equal, and about
- (minus sign) character when the input value is - (or other dashes) : indicates that the value is not present on the package
=cut
sub normalize_nutriment_value_and_modifier ($value_ref, $modifier_ref) {
${$modifier_ref} = undef;
return if not defined ${$value_ref};
# empty or null value
if ((${$value_ref} =~ /^\s*$/) or (lc(${$value_ref}) =~ /nan/)) {
${$value_ref} = undef;
}
# < , >, etc. signs
elsif (${$value_ref} =~ /(\<=|<=|\N{U+2264})( )?/) {
${$value_ref} =~ s/(\<=|<=|\N{U+2264})( )?//;
${$modifier_ref} = "\N{U+2264}";
}
elsif (
${$value_ref} =~ /(\<|<|max|maxi|maximum|inf|inférieur|inferieur|less|less than|menos|menor|inferior)( )?/i)
{
${$value_ref}
=~ s/(\<|<|max|maxi|maximum|inf|inférieur|inferieur|less|less than|menos|menor|inferior)( )?//i;
${$modifier_ref} = '<';
}
elsif (${$value_ref} =~ /(\>=|>=|\N{U+2265})/) {
${$value_ref} =~ s/(\>=|>=|\N{U+2265})( )?//;
${$modifier_ref} = "\N{U+2265}";
}
elsif (${$value_ref} =~ /(\>|>|min|mini|minimum|greater|more|more than|más|mayor|superior)/i) {
${$value_ref} =~ s/(\>|>|min|mini|minimum|greater|more|more than|más|mayor|superior)( )?//i;
${$modifier_ref} = '>';
}
elsif (${$value_ref} =~ /(env|environ|about|~|≈|aprox|alrededor)/i) {
${$value_ref} =~ s/(env|environ|about|~|≈|aprox|alrededor)( )?//i;
${$modifier_ref} = '~';
}
elsif (${$value_ref} =~ /(trace|traces|traza|trazas)/i) {
${$value_ref} = 0;
${$modifier_ref} = '~';
}
# - indicates that there is no value specified on the package
elsif (${$value_ref} =~ /^\s*$dashes\s*$/) {
${$value_ref} = undef;
${$modifier_ref} = '-';
}
# Remove extra spaces
if (defined ${$value_ref}) {
${$value_ref} =~ s/^\s+//;
${$value_ref} =~ s/\s+$//;
}
return;
}
=head2 default_unit_for_nid ( $nid)
Return the default unit that we convert everything to internally
=head3 Parameters
$nid: String
=head3 Return values
Default value for that certain unit
=cut
sub default_unit_for_nid ($nid) {
$nid =~ s/_prepared//;
my %default_unit_for_nid_map = (
"energy-kj" => "kJ",
"energy-kcal" => "kcal",
"energy" => "kJ",
"alcohol" => "% vol",
"water-hardness" => "mmol/l"
);
if (exists($default_unit_for_nid_map{$nid})) {
return $default_unit_for_nid_map{$nid};
}
elsif (($nid =~ /^fruits/) or ($nid =~ /^collagen/)) {
return "%";
}
else {
return "g";
}
}
=head2 assign_nid_modifier_value_and_unit ($product_ref, $nid, $modifier, $value, $unit)
Assign a value with a unit and an optional modifier (< or ~) to a nutrient in the nutriments structure.
=head3 Parameters
=head4 $product_ref
=head4 $nid
Nutrient id, possibly suffixed with "_prepared"
=head4 value
=head4 unit
=cut
sub assign_nid_modifier_value_and_unit ($product_ref, $nid, $modifier, $value, $unit) {
# Get the nutrient id in the nutrients taxonomy from the nid (without a prefix and possibly suffixed by _prepared)
my $nutrient_id = "zz:" . $nid;
$nutrient_id =~ s/_prepared$//;
# We can have only a modifier with value '-' to indicate that we have no value
if ((defined $modifier) and ($modifier ne '')) {
$product_ref->{nutriments}{$nid . "_modifier"} = $modifier;
}
else {
delete $product_ref->{nutriments}{$nid . "_modifier"};
}
if ((defined $value) and ($value ne '')) {
# empty unit?
if ((not defined $unit) or ($unit eq "")) {
$unit = default_unit_for_nid($nid);
}
$value = convert_string_to_number($value);
$product_ref->{nutriments}{$nid . "_unit"} = $unit;
$product_ref->{nutriments}{$nid . "_value"} = $value;
# Convert values passed in international units IU or % of daily value % DV to the default unit for the nutrient
if ( ((uc($unit) eq 'IU') or (uc($unit) eq 'UI'))
and (defined get_property("nutrients", $nutrient_id, "iu_value:en")))
{
$value = $value * get_property("nutrients", $nutrient_id, "iu_value:en");
$unit = get_property("nutrients", $nutrient_id, "unit:en");
}
elsif ((uc($unit) eq '% DV') and (defined get_property("nutrients", $nutrient_id, "dv_value:en"))) {
$value = $value / 100 * get_property("nutrients", $nutrient_id, "dv_value:en");
$unit = get_property("nutrients", $nutrient_id, "unit:en");
}
if ($nid =~ /^water-hardness(_prepared)?$/) {
$product_ref->{nutriments}{$nid} = unit_to_mmoll($value, $unit) + 0;
}
elsif ($nid =~ /^energy-kcal(_prepared)?/) {
# energy-kcal is stored in kcal
$product_ref->{nutriments}{$nid} = unit_to_kcal($value, $unit) + 0;
}
else {
$product_ref->{nutriments}{$nid} = unit_to_g($value, $unit) + 0;
}
}
else {
# We do not have a value for the nutrient
delete $product_ref->{nutriments}{$nid . "_value"};
# Delete other fields dervied from the value
delete $product_ref->{nutriments}{$nid};
delete $product_ref->{nutriments}{$nid . "_100g"};
delete $product_ref->{nutriments}{$nid . "_serving"};
# Delete modifiers (e.g. < sign), unless it is '-' which indicates that the field does not exist on the packaging
if ((defined $modifier) and ($modifier ne '-')) {
delete $product_ref->{nutriments}{$nid . "_modifier"};
}
}
return;
}
# For fat, saturated fat, sugars, salt: https://www.diw.de/sixcms/media.php/73/diw_wr_2010-19.pdf
@nutrient_levels = (['fat', 3, 20], ['saturated-fat', 1.5, 5], ['sugars', 5, 12.5], ['salt', 0.3, 1.5],);
#
# -sugars : sub-nutriment
# -- : sub-sub-nutriment
# vitamin-a- : do not show by default in the form
# !proteins : important, always show even if value has not been entered
%cc_nutriment_table = (
default => "europe",
ca => "ca",
ru => "ru",
us => "us",
hk => "hk",
jp => "jp",
);
=head2 %nutriments_tables
An array that condition how nutrients are displayed.
It is a list of nutrients names with eventual prefixes and suffixes:
=over
=item C<#nutrient> a leading C<#> indicates a comment and will be ignored
=item C<!nutrient> a leading C<!> indicates an important nutrient, they should always be shown
=item The level of each nutrient is indicated by leading dashes before its id:
=over
=item C<nutrient> - no dash for top nutrients
=item C<-sub-nutrient> - for level 2
=item C<--sub-sub-nutrient> - for level 3, etc.
=back
=item C<nutrient-> a C<-> at the end indicates that the nutrient should be hidden and only shown if explicitly added.
=back
=cut
# http://healthycanadians.gc.ca/eating-nutrition/label-etiquetage/tips-conseils/nutrition-fact-valeur-nutritive-eng.php
%nutriments_tables = (
europe => [
(
'!energy-kj', '!energy-kcal',
'!energy-', '-energy-from-fat-',
'!fat', '!-saturated-fat',
'--butyric-acid-', '--caproic-acid-',
'--caprylic-acid-', '--capric-acid-',
'--lauric-acid-', '--myristic-acid-',
'--palmitic-acid-', '--stearic-acid-',
'--arachidic-acid-', '--behenic-acid-',
'--lignoceric-acid-', '--cerotic-acid-',
'--montanic-acid-', '--melissic-acid-',
'-unsaturated-fat-', '--monounsaturated-fat-',
'---omega-9-fat-', '--polyunsaturated-fat-',
'---omega-3-fat-', '---omega-6-fat-',
'--alpha-linolenic-acid-', '--eicosapentaenoic-acid-',
'--docosahexaenoic-acid-', '--linoleic-acid-',
'--arachidonic-acid-', '--gamma-linolenic-acid-',
'--dihomo-gamma-linolenic-acid-', '--oleic-acid-',
'--elaidic-acid-', '--gondoic-acid-',
'--mead-acid-', '--erucic-acid-',
'--nervonic-acid-', '-trans-fat-',
'-cholesterol-', '!carbohydrates',
'!-sugars', '--added-sugars-',
'--sucrose-', '--glucose-',
'--fructose-', '--lactose-',
'--maltose-', '--maltodextrins-',
'-starch-', '-polyols-',
'--erythritol-', '!fiber',
'-soluble-fiber-', '-insoluble-fiber-',
'!proteins', '-casein-',
'-serum-proteins-', '-nucleotides-',
'!salt', '-added-salt-',
'sodium', 'alcohol',
'#vitamins', 'vitamin-a-',
'beta-carotene-', 'vitamin-d-',
'vitamin-e-', 'vitamin-k-',
'vitamin-c-', 'vitamin-b1-',
'vitamin-b2-', 'vitamin-pp-',
'vitamin-b6-', 'vitamin-b9-',
'folates-', 'vitamin-b12-',
'biotin-', 'pantothenic-acid-',
'#minerals', 'silica-',
'bicarbonate-', 'potassium-',
'chloride-', 'calcium-',
'phosphorus-', 'iron-',
'magnesium-', 'zinc-',
'copper-', 'manganese-',
'fluoride-', 'selenium-',
'chromium-', 'molybdenum-',
'iodine-', 'caffeine-',
'taurine-', 'ph-',
'fruits-vegetables-nuts-', 'fruits-vegetables-nuts-dried-',
'fruits-vegetables-nuts-estimate-', 'collagen-meat-protein-ratio-',
'cocoa-', 'chlorophyl-',
'carbon-footprint-', 'carbon-footprint-from-meat-or-fish-',
'nutrition-score-fr-', 'nutrition-score-uk-',
'glycemic-index-', 'water-hardness-',
'choline-', 'phylloquinone-',
'beta-glucan-', 'inositol-',
'carnitine-', 'sulphate-',
'nitrate-', 'acidity-',
)
],
ca => [
(
'!energy-kcal', 'energy-',
'!fat', '-saturated-fat',
'--butyric-acid-', '--caproic-acid-',
'--caprylic-acid-', '--capric-acid-',
'--lauric-acid-', '--myristic-acid-',
'--palmitic-acid-', '--stearic-acid-',
'--arachidic-acid-', '--behenic-acid-',
'--lignoceric-acid-', '--cerotic-acid-',
'--montanic-acid-', '--melissic-acid-',
'-unsaturated-fat-', '--monounsaturated-fat-',
'---omega-9-fat-', '--polyunsaturated-fat-',
'---omega-3-fat-', '---omega-6-fat-',
'--alpha-linolenic-acid-', '--eicosapentaenoic-acid-',
'--docosahexaenoic-acid-', '--linoleic-acid-',
'--arachidonic-acid-', '--gamma-linolenic-acid-',
'--dihomo-gamma-linolenic-acid-', '--oleic-acid-',
'--elaidic-acid-', '--gondoic-acid-',
'--mead-acid-', '--erucic-acid-',
'--nervonic-acid-', '-trans-fat',
'cholesterol', '!carbohydrates',
'-fiber', '--soluble-fiber-',
'--insoluble-fiber-', '-sugars',
'--added-sugars-', '--sucrose-',
'--glucose-', '--fructose-',
'--lactose-', '--maltose-',
'--maltodextrins-', '-starch-',
'-polyols-', '-erythritol-',
'!proteins', '-casein-',
'-serum-proteins-', '-nucleotides-',
'salt', '-added-salt-',
'sodium', 'alcohol',
'#vitamins', 'vitamin-a',
'beta-carotene-', 'vitamin-d-',
'vitamin-e-', 'vitamin-k-',
'vitamin-c', 'vitamin-b1-',
'vitamin-b2-', 'vitamin-pp-',
'vitamin-b6-', 'vitamin-b9-',
'folates-', 'vitamin-b12-',
'biotin-', 'pantothenic-acid-',
'#minerals', 'silica-',
'bicarbonate-', 'potassium-',
'chloride-', 'calcium',
'phosphorus-', 'iron',
'magnesium-', 'zinc-',
'copper-', 'manganese-',
'fluoride-', 'selenium-',
'chromium-', 'molybdenum-',
'iodine-', 'caffeine-',
'taurine-', 'ph-',
'fruits-vegetables-nuts-', 'fruits-vegetables-nuts-dried-',
'fruits-vegetables-nuts-estimate-', 'collagen-meat-protein-ratio-',
'cocoa-', 'chlorophyl-',
'carbon-footprint-', 'carbon-footprint-from-meat-or-fish-',
'nutrition-score-fr-', 'nutrition-score-uk-',
'glycemic-index-', 'water-hardness-',
'choline-', 'phylloquinone-',
'beta-glucan-', 'inositol-',
'carnitine-', 'sulphate-',
'nitrate-', 'acidity-',
)
],
ru => [
(
'!proteins', '-casein-',
'-serum-proteins-', '-nucleotides-',
'!fat', '-saturated-fat',
'--butyric-acid-', '--caproic-acid-',
'--caprylic-acid-', '--capric-acid-',
'--lauric-acid-', '--myristic-acid-',
'--palmitic-acid-', '--stearic-acid-',
'--arachidic-acid-', '--behenic-acid-',
'--lignoceric-acid-', '--cerotic-acid-',
'--montanic-acid-', '--melissic-acid-',
'-unsaturated-fat-', '--monounsaturated-fat-',
'---omega-9-fat-', '--polyunsaturated-fat-',
'---omega-3-fat-', '---omega-6-fat-',
'--alpha-linolenic-acid-', '--eicosapentaenoic-acid-',
'--docosahexaenoic-acid-', '--linoleic-acid-',
'--arachidonic-acid-', '--gamma-linolenic-acid-',
'--dihomo-gamma-linolenic-acid-', '--oleic-acid-',
'--elaidic-acid-', '--gondoic-acid-',
'--mead-acid-', '--erucic-acid-',
'--nervonic-acid-', '-trans-fat-',
'-cholesterol-', '!carbohydrates',
'-sugars', '--added-sugars-',
'--sucrose-', '--glucose-',
'--fructose-', '--lactose-',
'--maltose-', '--maltodextrins-',
'-starch-', '-polyols-',
'--erythritol-', '!energy-kj',
'!energy-kcal', 'energy-',
'-energy-from-fat-', 'fiber',
'salt', '-added-salt-',
'sodium', 'alcohol',
'#vitamins', 'vitamin-a-',
'beta-carotene-', 'vitamin-d-',
'vitamin-e-', 'vitamin-k-',
'vitamin-c-', 'vitamin-b1-',
'vitamin-b2-', 'vitamin-pp-',
'vitamin-b6-', 'vitamin-b9-',
'folates-', 'vitamin-b12-',
'biotin-', 'pantothenic-acid-',
'#minerals', 'silica-',
'bicarbonate-', 'potassium-',
'chloride-', 'calcium-',
'phosphorus-', 'iron-',
'magnesium-', 'zinc-',
'copper-', 'manganese-',
'fluoride-', 'selenium-',
'chromium-', 'molybdenum-',
'iodine-', 'caffeine-',
'taurine-', 'ph-',
'fruits-vegetables-nuts-', 'fruits-vegetables-nuts-dried-',
'fruits-vegetables-nuts-estimate-', 'collagen-meat-protein-ratio-',
'cocoa-', 'chlorophyl-',
'carbon-footprint-', 'carbon-footprint-from-meat-or-fish-',
'nutrition-score-fr-', 'nutrition-score-uk-',
'glycemic-index-', 'water-hardness-',
'choline-', 'phylloquinone-',
'beta-glucan-', 'inositol-',
'carnitine-', 'sulphate-',
'nitrate-', 'acidity-',
)
],
us => [
(
'!energy-kcal', 'energy-',
'-energy-from-fat-', '!fat',
'-saturated-fat', '--butyric-acid-',
'--caproic-acid-', '--caprylic-acid-',
'--capric-acid-', '--lauric-acid-',
'--myristic-acid-', '--palmitic-acid-',
'--stearic-acid-', '--arachidic-acid-',
'--behenic-acid-', '--lignoceric-acid-',
'--cerotic-acid-', '--montanic-acid-',
'--melissic-acid-', '-unsaturated-fat-',
'--monounsaturated-fat-', '---omega-9-fat-',
'--polyunsaturated-fat-', '---omega-3-fat-',
'---omega-6-fat-', '--alpha-linolenic-acid-',
'--eicosapentaenoic-acid-', '--docosahexaenoic-acid-',
'--linoleic-acid-', '--arachidonic-acid-',
'--gamma-linolenic-acid-', '--dihomo-gamma-linolenic-acid-',
'--oleic-acid-', '--elaidic-acid-',
'--gondoic-acid-', '--mead-acid-',
'--erucic-acid-', '--nervonic-acid-',
'-trans-fat', 'cholesterol',
'salt-', '-added-salt-',
'sodium', '!carbohydrates',
'-fiber', '--soluble-fiber-',
'--insoluble-fiber-', '-sugars',
'--added-sugars', '--sucrose-',
'--glucose-', '--fructose-',
'--lactose-', '--maltose-',
'--maltodextrins-', '-starch-',
'-polyols-', '-erythritol-',
'!proteins', '-casein-',
'-serum-proteins-', '-nucleotides-',
'alcohol', '#vitamins',
'vitamin-a-', 'beta-carotene-',
'vitamin-d', 'vitamin-e-',
'vitamin-k-', 'vitamin-c-',
'vitamin-b1-', 'vitamin-b2-',
'vitamin-pp-', 'vitamin-b6-',
'vitamin-b9-', 'folates-',
'vitamin-b12-', 'biotin-',
'pantothenic-acid-', '#minerals',
'calcium', 'iron',
'potassium', 'silica-',
'bicarbonate-', 'chloride-',
'phosphorus-', 'magnesium-',
'zinc-', 'copper-',
'manganese-', 'fluoride-',
'selenium-', 'chromium-',
'molybdenum-', 'iodine-',
'caffeine-', 'taurine-',
'ph-', 'fruits-vegetables-nuts-',
'fruits-vegetables-nuts-dried-', 'fruits-vegetables-nuts-estimate-',
'collagen-meat-protein-ratio-', 'cocoa-',
'chlorophyl-', 'carbon-footprint-',
'carbon-footprint-from-meat-or-fish-', 'nutrition-score-fr-',
'nutrition-score-uk-', 'glycemic-index-',
'water-hardness-', 'sulfate-',
'nitrate-', 'acidity-',
)
],
us_before_2017 => [
(
'!energy', '-energy-from-fat',
'!fat', '-saturated-fat',
'--butyric-acid-', '--caproic-acid-',
'--caprylic-acid-', '--capric-acid-',
'--lauric-acid-', '--myristic-acid-',
'--palmitic-acid-', '--stearic-acid-',
'--arachidic-acid-', '--behenic-acid-',
'--lignoceric-acid-', '--cerotic-acid-',
'--montanic-acid-', '--melissic-acid-',
'-unsaturated-fat-', '--monounsaturated-fat-',
'---omega-9-fat-', '--polyunsaturated-fat-',
'---omega-3-fat-', '---omega-6-fat-',
'--alpha-linolenic-acid-', '--eicosapentaenoic-acid-',
'--docosahexaenoic-acid-', \'--linoleic-acid-',
'--arachidonic-acid-', '--gamma-linolenic-acid-',
'--dihomo-gamma-linolenic-acid-', '--oleic-acid-',
'--elaidic-acid-', '--gondoic-acid-',
'--mead-acid-', '--erucic-acid-',
'--nervonic-acid-', '-trans-fat',
'cholesterol', 'salt-',
'sodium', '!carbohydrates',
'-fiber', '--soluble-fiber-',
'--insoluble-fiber-', '-sugars',
'--sucrose-', '--glucose-',
'--fructose-', '--lactose-',
'--maltose-', '--maltodextrins-',
'-starch-', '-polyols-',
'--erythritol-', '!proteins',
'-casein-', '-serum-proteins-',
'-nucleotides-', 'alcohol',
'#vitamins', 'vitamin-a',
'beta-carotene-', 'vitamin-d-',
'vitamin-e-', 'vitamin-k-',
'vitamin-c', 'vitamin-b1-',
'vitamin-b2-', 'vitamin-pp-',
'vitamin-b6-', 'vitamin-b9-',
'folates-', 'vitamin-b12-',
'biotin-', 'pantothenic-acid-',
'#minerals', 'silica-',
'bicarbonate-', 'potassium-',
'chloride-', 'calcium',
'phosphorus-', 'iron',
'magnesium-', 'zinc-',
'copper-', 'manganese-',
'fluoride-', 'selenium-',
'chromium-', 'molybdenum-',
'iodine-', 'caffeine-',
'taurine-', 'ph-',
'fruits-vegetables-nuts-', 'fruits-vegetables-nuts-dried-',
'fruits-vegetables-nuts-estimate-', 'collagen-meat-protein-ratio-',
'cocoa-', 'chlorophyl-',
'carbon-footprint-', 'carbon-footprint-from-meat-or-fish-',
'nutrition-score-fr-', 'nutrition-score-uk-',
'glycemic-index-', 'water-hardness-',
'choline-', 'phylloquinone-',
'beta-glucan-', 'inositol-',
'carnitine-', 'sulfate-',
'nitrate-', 'acidity-',
)
],
hk => [
(
'!energy-kj', '!energy-kcal', '!proteins', '!fat',
'-saturated-fat', '-unsaturated-fat-', '--monounsaturated-fat-', '--monounsaturated-fat-',
'-trans-fat', 'cholesterol', '!carbohydrates', '-sugars',
'-fiber', 'salt-', 'sodium', '#vitamins',
'vitamin-a', 'vitamin-d-', 'vitamin-c', 'vitamin-b1-',
'vitamin-b2-', 'vitamin-pp-', 'vitamin-b6-', 'vitamin-b9-',
'folates-', 'vitamin-b12-', '#minerals', 'calcium',
'potassium-', 'phosphorus-', 'iron', 'alcohol',
'nutrition-score-fr-', 'sulphate-', 'nitrate-', 'acidity-',
)
],
jp => [
(
'!energy-kj-', '!energy-kcal',
'!energy-', '-energy-from-fat-',
'!proteins', '-casein-',
'-serum-proteins-', '-nucleotides-',
'!fat', '-saturated-fat-',
'--butyric-acid-', '--caproic-acid-',
'--caprylic-acid-', '--capric-acid-',
'--lauric-acid-', '--myristic-acid-',
'--palmitic-acid-', '--stearic-acid-',
'--arachidic-acid-', '--behenic-acid-',
'--lignoceric-acid-', '--cerotic-acid-',
'--montanic-acid-', '--melissic-acid-',
'-unsaturated-fat-', '--monounsaturated-fat-',
'---omega-9-fat-', '--polyunsaturated-fat-',
'---omega-3-fat-', '---omega-6-fat-',
'--alpha-linolenic-acid-', '--eicosapentaenoic-acid-',
'--docosahexaenoic-acid-', '--linoleic-acid-',
'--arachidonic-acid-', '--gamma-linolenic-acid-',
'--dihomo-gamma-linolenic-acid-', '--oleic-acid-',
'--elaidic-acid-', '--gondoic-acid-',
'--mead-acid-', '--erucic-acid-',
'--nervonic-acid-', '-trans-fat-',
'cholesterol-', '!carbohydrates',
'-sugars-', '-fiber-',
'-soluble-fiber-', '-insoluble-fiber-',
'!salt', '-added-salt-',
'#sodium-', 'alcohol',
'#vitamins', 'vitamin-a-',
'beta-carotene-', 'vitamin-d-',
'vitamin-e-', 'vitamin-k-',
'vitamin-c-', 'vitamin-b1-',
'vitamin-b2-', 'vitamin-pp-',
'vitamin-b6-', 'vitamin-b9-',
'folates-', 'vitamin-b12-',
'biotin-', 'pantothenic-acid-',
'#minerals', 'silica-',
'bicarbonate-', 'potassium-',
'chloride-', 'calcium-',
'phosphorus-', 'iron-',
'magnesium-', 'zinc-',
'copper-', 'manganese-',
'fluoride-', 'selenium-',
'chromium-', 'molybdenum-',
'iodine-', 'caffeine-',
'taurine-', 'ph-',
'fruits-vegetables-nuts-', 'fruits-vegetables-nuts-dried-',
'fruits-vegetables-nuts-estimate-', 'collagen-meat-protein-ratio-',
'cocoa-', 'chlorophyl-',
'carbon-footprint-', 'carbon-footprint-from-meat-or-fish-',
'nutrition-score-fr-', 'nutrition-score-uk-',
'glycemic-index-', 'water-hardness-',
'choline-', 'phylloquinone-',
'beta-glucan-', 'inositol-',
'carnitine-', 'sulphate-',
'nitrate-', 'acidity-',
)
],
in => [
(
'!energy-kj', '!energy-kcal',
'!proteins', '-casein-',
'-serum-proteins-', '-nucleotides-',
'!fat', '-saturated-fat',
'--butyric-acid-', '--caproic-acid-',
'--caprylic-acid-', '--capric-acid-',
'--lauric-acid-', '--myristic-acid-',
'--palmitic-acid-', '--stearic-acid-',
'--arachidic-acid-', '--behenic-acid-',
'--lignoceric-acid-', '--cerotic-acid-',
'--montanic-acid-', '--melissic-acid-',
'-unsaturated-fat-', '--monounsaturated-fat-',
'---omega-9-fat-', '--polyunsaturated-fat-',
'---omega-3-fat-', '---omega-6-fat-',
'--alpha-linolenic-acid-', '--eicosapentaenoic-acid-',
'--docosahexaenoic-acid-', '--linoleic-acid-',
'--arachidonic-acid-', '--gamma-linolenic-acid-',
'--dihomo-gamma-linolenic-acid-', '--oleic-acid-',
'--elaidic-acid-', '--gondoic-acid-',
'--mead-acid-', '--erucic-acid-',
'--nervonic-acid-', '-trans-fat-',
'-cholesterol-', '!carbohydrates',
'-sugars', '--added-sugars-',
'--sucrose-', '--glucose-',
'--fructose-', '--lactose-',
'--maltose-', '--maltodextrins-',
'-starch-', '-polyols-',
'--erythritol-', '!fiber',
'-soluble-fiber-', '-insoluble-fiber-',
'!salt', '-added-salt-',
'sodium', 'alcohol',
'#vitamins', 'vitamin-a-',
'beta-carotene-', 'vitamin-d-',
'vitamin-e-', 'vitamin-k-',
'vitamin-c-', 'vitamin-b1-',
'vitamin-b2-', 'vitamin-pp-',
'vitamin-b6-', 'vitamin-b9-',
'folates-', 'vitamin-b12-',
'biotin-', 'pantothenic-acid-',
'#minerals', 'silica-',
'bicarbonate-', 'potassium-',
'chloride-', 'calcium-',
'phosphorus-', 'iron-',
'magnesium-', 'zinc-',
'copper-', 'manganese-',
'fluoride-', 'selenium-',
'chromium-', 'molybdenum-',
'iodine-', 'caffeine-',
'taurine-', 'ph-',
'fruits-vegetables-nuts-', 'fruits-vegetables-nuts-dried-',
'fruits-vegetables-nuts-estimate-', 'collagen-meat-protein-ratio-',
'cocoa-', 'chlorophyl-',
'carbon-footprint-', 'carbon-footprint-from-meat-or-fish-',
'nutrition-score-fr-', 'nutrition-score-uk-',
'glycemic-index-', 'water-hardness-',
'choline-', 'phylloquinone-',
'beta-glucan-', 'inositol-',
'carnitine-', 'sulphate-',
'nitrate-', 'acidity-',
)
],
);
# Compute the list of nutriments that are not shown by default so that they can be suggested
foreach my $region (keys %nutriments_tables) {
$nutriments_lists{$region} = [];
$other_nutriments_lists{$region} = [];
foreach (@{$nutriments_tables{$region}}) {
my $nutriment = $_; # copy instead of alias
if ($nutriment =~ /-$/) {
$nutriment = $`;
$nutriment =~ s/^(-|!)+//g;
push @{$other_nutriments_lists{$region}}, $nutriment;
}
next if $nutriment =~ /\#/;
$nutriment =~ s/^(-|!)+//g;
$nutriment =~ s/-$//g;
push @{$nutriments_lists{$region}}, $nutriment;
}
}
=head2 canonicalize_nutriment ( $product_ref )
Canonicalizes the nutrients input by the user in the nutrition table product edit.
This sub converts these nutrients (which are arguments to this function), into a recognizable/standard form.
=head3 Parameters
Two strings are passed,
$target_lc: The language in which the nutriment is (example: "en", "fr")
$nutrient: The nutrient that needs to be canonicalized. (the user input nutrient, example: "AGS", "unsaturated-fat")
=head3 Return values
Returns the $nid (a string)
Example: For the parameter "dont saturés", we get the $nid as "saturated fat"
=cut
sub canonicalize_nutriment ($target_lc, $nutrient) {
my $nid = canonicalize_taxonomy_tag($target_lc, "nutrients", $nutrient);
if ($nid =~ /^zz:/) {
# Recognized nutrients start with zz: -> remove zz: to get the nid
$nid = $';
}
else {
# Unrecognized nutrients start with the language code (e.g. fr:)
# -> turn it to fr-
$nid = get_string_id_for_lang($lc, $nid);
}
return $nid;
}
=head2 is_beverage_for_nutrition_score_2021 ( $product_ref )
Determines if a product should be considered as a beverage for Nutri-Score computations,
based on the product categories.
2021 Nutri-Score: Dairy drinks are not considered as beverages if they have at least 80% of milk.
=cut
sub is_beverage_for_nutrition_score_2021 ($product_ref) {
my $is_beverage = 0;
if ( has_tag($product_ref, "categories", "en:beverages")
or has_tag($product_ref, "categories", "en:beverage-preparations"))
{
$is_beverage = 1;
if (defined $options{categories_not_considered_as_beverages_for_nutriscore_2021}) {
foreach my $category_id (@{$options{categories_not_considered_as_beverages_for_nutriscore_2021}}) {
if (has_tag($product_ref, "categories", $category_id)) {
$is_beverage = 0;
last;
}
}
}
# exceptions
if (defined $options{categories_considered_as_beverages_for_nutriscore_2021}) {
foreach my $category_id (@{$options{categories_considered_as_beverages_for_nutriscore_2021}}) {
if (has_tag($product_ref, "categories", $category_id)) {
$is_beverage = 1;
last;
}
}
}
# dairy drinks need to have at least 80% of milk to be considered as food instead of beverages
my $milk_percent = estimate_nutriscore_2021_milk_percent_from_ingredients($product_ref);
if ((defined $milk_percent) and ($milk_percent >= 80)) {
$log->debug("milk >= 80%", {milk_percent => $milk_percent}) if $log->is_debug();