Skip to content

Commit

Permalink
feat: added unit for sodium in India (#10041)
Browse files Browse the repository at this point in the history
- Added unit_in:en: mg key value pair to the nutrients taxonomy
- Made a new subroutine in Food.pm named get_nutrient_unit
- added a test in food.t
- Fixes #10038
  • Loading branch information
himanshisrestha authored and john-gom committed May 24, 2024
1 parent e9520b6 commit 1c5286d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cgi/nutrients.pl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use ProductOpener::Lang qw/$lc/;
use ProductOpener::Display qw/$nutriment_table init_request/;
use ProductOpener::HTTP qw/write_cors_headers/;
use ProductOpener::Food qw/%nutriments_tables/;
use ProductOpener::Food qw/%nutriments_tables get_nutrient_unit/;
use ProductOpener::Tags qw/display_taxonomy_tag get_property/;

use Log::Any qw($log);
Expand Down Expand Up @@ -63,7 +63,7 @@
if (defined $name) {
$current_ref->{name} = $name;
}
my $unit = get_property("nutrients", "zz:$onid", "unit:en") // 'g';
my $unit = get_nutrient_unit($onid);
if (defined $unit) {
$current_ref->{unit} = $unit;
}
Expand Down
5 changes: 2 additions & 3 deletions cgi/product_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
use ProductOpener::Mail qw/send_email_to_admin/;
use ProductOpener::Products qw/:all/;
use ProductOpener::Food
qw/%nutriments_tables %other_nutriments_lists assign_nutriments_values_from_request_parameters compute_serving_size_data/;
qw/%nutriments_tables %other_nutriments_lists assign_nutriments_values_from_request_parameters compute_serving_size_data get_nutrient_unit/;
use ProductOpener::Units qw/g_to_unit mmoll_to_unit/;
use ProductOpener::Ingredients qw/:all/;
use ProductOpener::Images qw/:all/;
Expand Down Expand Up @@ -1218,8 +1218,7 @@ ($product_ref, $field, $language)
if (exists_taxonomy_tag("nutrients", "zz:$nid")) {
$nutriment_ref->{name} = display_taxonomy_tag($lc, "nutrients", "zz:$nid");
# We may have a unit specific to the country (e.g. US nutrition facts table using the International Unit for this nutrient, and Europe using mg)
$unit = get_property("nutrients", "zz:$nid", "unit_$cc:en")
// get_property("nutrients", "zz:$nid", "unit:en") // 'g';
$unit = get_nutrient_unit($nid, $cc);
}
else {
if (defined $product_ref->{nutriments}{$nid . "_unit"}) {
Expand Down
83 changes: 83 additions & 0 deletions lib/ProductOpener/Food.pm
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ BEGIN {
&check_nutriscore_categories_exist_in_taxonomy
&get_nutrient_unit
); # symbols to export on request
%EXPORT_TAGS = (all => [@EXPORT_OK]);
}
Expand Down Expand Up @@ -817,6 +819,69 @@ It is a list of nutrients names with eventual prefixes and suffixes:
'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
Expand Down Expand Up @@ -3172,4 +3237,22 @@ sub compute_estimated_nutrients ($product_ref) {
return $results_ref;
}

=head2 get_nutrient_unit ( $nid, $cc )
Returns the unit of the nutrient.
We may have a unit specific to the country (e.g. US nutrition facts table using the International Unit for this nutrient, and Europe using mg)
=cut

sub get_nutrient_unit ($nid, $cc = undef) {
my $unit;
if ($cc) {
$unit = get_property("nutrients", "zz:$nid", "unit_$cc:en");
return $unit if $unit;
}
$unit = get_property("nutrients", "zz:$nid", "unit:en") // 'g';
return $unit;
}

1;
2 changes: 2 additions & 0 deletions taxonomies/nutrients.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3089,6 +3089,7 @@ yi:זאלץ
yo:Iyọ̀
zh:食盐
unit:en: g
unit_in:en: mg
wikidata:en: Q11254
wikipedia:en: https://en.wikipedia.org/wiki/Salt
evaluation:en: bad
Expand Down Expand Up @@ -3228,6 +3229,7 @@ zh_HK:鈉
zh_TW:鈉
unit:en: g
unit_us:en: mg
unit_in:en: mg
wikidata:en: Q658
wikipedia:en: https://en.wikipedia.org/wiki/Sodium
evaluation:en: bad
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/food.t
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,23 @@ is_deeply(
}
) or diag explain $product_ref;

# Testing for get_nutrient_unit both for India and a country where no unit is described
# Test case for fetching unit for sodium in India
{
my $unit_in_india = get_nutrient_unit("sodium", "in");
is($unit_in_india, "mg", "Check if unit_in is fetched correctly for sodium in India");
}

# Test case for fetching unit for sodium outside India (eg: US)
{
my $unit_in_us = get_nutrient_unit("sodium", "us");
is($unit_in_us, "mg", "Check if unit_us is fetched correctly for sodium in US");
}

# Test case for fetching unit for sodium outside India (eg: Canada)
{
my $unit_in_canada = get_nutrient_unit("sodium", "ca");
is($unit_in_canada, "g", "Check if unit is fetched correctly for sodium in Canada");
}

done_testing();

0 comments on commit 1c5286d

Please sign in to comment.