Skip to content

Commit

Permalink
Merge pull request #588 from openfoodfacts/issue/386-water-hardness
Browse files Browse the repository at this point in the history
Implemented water hardness
  • Loading branch information
hangy authored Jan 17, 2017
2 parents 5950a43 + 895b77a commit 039d2a0
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
19 changes: 17 additions & 2 deletions cgi/product_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,12 @@
$value = $value / 100 * $Nutriments{$nid}{dv} ;
$unit = $Nutriments{$nid}{unit};
}
$product_ref->{nutriments}{$nid} = unit_to_g($value, $unit);
if ($nid eq 'water-hardness') {
$product_ref->{nutriments}{$nid} = unit_to_mmoll($value, $unit);
}
else {
$product_ref->{nutriments}{$nid} = unit_to_g($value, $unit);
}
}
}

Expand Down Expand Up @@ -1445,7 +1450,13 @@ ($$$$$$)
elsif ((exists $Nutriments{$nid}) and (exists $Nutriments{$nid}{unit})) {
$unit = $Nutriments{$nid}{unit};
}
my $value = g_to_unit($product_ref->{nutriments}{$nid}, $unit);
my $value;
if ($nid eq 'water-hardness') {
$value = mmoll_to_unit($product_ref->{nutriments}{$nid}, $unit);
}
else {
$value = g_to_unit($product_ref->{nutriments}{$nid}, $unit);
}

# user unit and value ? (e.g. DV for vitamins in US)
if ((defined $product_ref->{nutriments}{$nid . "_value"}) and (defined $product_ref->{nutriments}{$nid . "_unit"})) {
Expand Down Expand Up @@ -1483,6 +1494,10 @@ ($$$$$$)
elsif ($nid eq 'alcohol') {
@units = ('% vol');
}
elsif ($nid eq 'water-hardness') {
@units = ('mol/l', 'mmol/l', 'mval/l', 'ppm', "\N{U+00B0}rH", "\N{U+00B0}fH", "\N{U+00B0}e", "\N{U+00B0}dH", 'gpg');
}

if (((exists $Nutriments{$nid}) and (exists $Nutriments{$nid}{dv}) and ($Nutriments{$nid}{dv} > 0))
or ($nid =~ /^new_/)) {
push @units, '% DV';
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ requires 'Modern::Perl', '>= 1.20150127';
on 'test' => sub {
requires 'Test::More', '>= 1.302049, < 2.0';
requires 'Test::Perl::Critic';
requires 'Test::Number::Delta';
};
60 changes: 59 additions & 1 deletion lib/ProductOpener/Food.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ BEGIN
&unit_to_g
&g_to_unit
&unit_to_mmoll
&mmoll_to_unit
&canonicalize_nutriment
&fix_salt_equivalent
Expand Down Expand Up @@ -137,7 +140,51 @@ sub g_to_unit($$) {
return $value + 0;
}

sub unit_to_mmoll {
my ($value, $unit) = @_;
$unit = lc($unit);

if ((not defined $value) or ($value eq '')) {
return '';
}

$value =~ s/,/\./;
$value =~ s/^(<|environ|max|maximum|min|minimum)( )?//;

return $value * 1000 if $unit eq 'mol/l';
return $value + 0 if $unit eq 'mmol/l';
return $value / 2 if $unit eq 'mval/l';
return $value / 100 if $unit eq 'ppm';
return $value / 40.080 if $unit eq "\N{U+00B0}rh";
return $value / 10.00 if $unit eq "\N{U+00B0}fh";
return $value / 7.02 if $unit eq "\N{U+00B0}e";
return $value / 5.6 if $unit eq "\N{U+00B0}dh";
return $value / 5.847 if $unit eq 'gpg';
return $value + 0;
}

sub mmoll_to_unit {
my ($value, $unit) = @_;
$unit = lc($unit);

if ((not defined $value) or ($value eq '')) {
return '';
}

$value =~ s/,/\./;
$value =~ s/^(<|environ|max|maximum|min|minimum)( )?//;

return $value / 1000 if $unit eq 'mol/l';
return $value + 0 if $unit eq 'mmol/l';
return $value * 2 if $unit eq 'mval/l';
return $value * 100 if $unit eq 'ppm';
return $value * 40.080 if $unit eq "\N{U+00B0}rh";
return $value * 10.00 if $unit eq "\N{U+00B0}fh";
return $value * 7.02 if $unit eq "\N{U+00B0}e";
return $value * 5.6 if $unit eq "\N{U+00B0}dh";
return $value * 5.847 if $unit eq 'gpg';
return $value + 0;
}

# http://www.diw.de/sixcms/media.php/73/diw_wr_2010-19.pdf
@nutrient_levels = (
Expand Down Expand Up @@ -264,6 +311,7 @@ carbon-footprint
nutrition-score-fr-
nutrition-score-uk-
glycemic-index-
water-hardness-
)
],

Expand Down Expand Up @@ -368,6 +416,7 @@ carbon-footprint
nutrition-score-fr-
nutrition-score-uk-
glycemic-index-
water-hardness-
)
],

Expand Down Expand Up @@ -471,6 +520,7 @@ carbon-footprint
nutrition-score-fr-
nutrition-score-uk-
glycemic-index-
water-hardness-
)
],

Expand Down Expand Up @@ -577,6 +627,7 @@ carbon-footprint
nutrition-score-fr-
nutrition-score-uk-
glycemic-index-
water-hardness-
)
],

Expand Down Expand Up @@ -2404,7 +2455,14 @@ ph => {
'glycemic-index' => {
en => 'Glycemic Index',
de => 'Glykämischer Index',
unit => ''
unit => '',
},
"water-hardness" => {
en => 'Water hardness',
fr => "Dureté de l'eau",
ru => 'Жёсткость воды',
de => 'Wasserhärte',
unit => 'mmol/l',
},
"fruits-vegetables-nuts" => {
en => "Fruits, vegetables and nuts (minimum)",
Expand Down
36 changes: 36 additions & 0 deletions t/food.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/perl -w

use strict;
use warnings;

use Test::More;
use Test::Number::Delta relative => 1.001;

use ProductOpener::Food qw/:all/;

# Based on https://de.wikipedia.org/w/index.php?title=Wasserh%C3%A4rte&oldid=160348959#Einheiten_und_Umrechnung
is( mmoll_to_unit(1, 'mol/l'), 0.001 );
is( mmoll_to_unit('1', 'moll/l'), 1 );
is( mmoll_to_unit(1, 'mmol/l'), 1 );
is( mmoll_to_unit(1, 'mval/l'), 2 );
is( mmoll_to_unit(1, 'ppm'), 100 );
is( mmoll_to_unit(1, "\N{U+00B0}rH"), 40.080 );
is( mmoll_to_unit(1, "\N{U+00B0}fH"), 10.00 );
is( mmoll_to_unit(1, "\N{U+00B0}e"), 7.02 );
is( mmoll_to_unit(1, "\N{U+00B0}dH"), 5.6 );
is( mmoll_to_unit(1, 'gpg'), 5.847 );

is( unit_to_mmoll(1, 'mol/l'), 1000 );
is( unit_to_mmoll('1', 'mmol/l'), 1 );
is( unit_to_mmoll(1, 'mmol/l'), 1 );
is( unit_to_mmoll(1, 'mval/l'), 0.5 );
is( unit_to_mmoll(1, 'ppm'), 0.01 );
delta_ok( unit_to_mmoll(1, "\N{U+00B0}rH"), 0.025 );
delta_ok( unit_to_mmoll(1, "\N{U+00B0}fH"), 0.1 );
delta_ok( unit_to_mmoll(1, "\N{U+00B0}e"), 0.142 );
delta_ok( unit_to_mmoll(1, "\N{U+00B0}dH"), 0.1783 );
delta_ok( unit_to_mmoll(1, 'gpg'), 0.171 );

is( mmoll_to_unit(unit_to_mmoll(1, 'ppm'), "\N{U+00B0}dH"), 0.056 );

done_testing();

0 comments on commit 039d2a0

Please sign in to comment.