- Notifications
You must be signed in to change notification settings - Fork0
nutriscore computation for Ruby
License
q-m/nutriscore-ruby
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Consumer food products in some countries have a nutritional score, indicatinghow healthy the product is to eat. Each country has its own approach, but inthe European Union theNutri-Score in several countries.
This gem implements the Nutri-Score for Ruby and includes adaptations for differentcountries. The maintainer would be open to integrating other nutritional scores as well.
Note: this is currently under development, the API may change without notice,and scoring has not yet been fully verified. Be very careful using this in production.
There are currently two versions of the Nutri-Score in use. The first wasdevelopedin the UK by the Food Standards Agency andcurrently maintaintedby the Department of Health. It is used for regulating food advertisements.
In France, an adaptedNutri-Scorewas adopted in 2017 for use as a voluntary label on the packaging.In 2018, Belgium and Spain adopted the same scheme.
Other countries are evaluating adopting the Nutri-Score as well, includingThe Netherlands. There was also aEuropean citizen's initiativeto adopt it Europe-wide, but this has been withdrawn.
gem install nutriscoreThe input for all nutritional scores are a product category and nutritional values.Which nutrients are required depends on the product category. Note thatfvnp standsfor the amount of fruits, vegetables, nuts and pulses (and, since Oct 2019, alsorapeseed, walnuts and olive oils).
require'nutriscore'# Fruit fromage fraisproduct_a={energy:459,# kJ/100gfat_saturated:1.8,# g/100gsugar:13.4,# g/100gsodium:0.1,# g/100gfvnp:8,# g/100g (= weight-%)fibres:0.6,# g/100gproteins:6.5,# g/100g}# Compute the french Nutri-Score for a generic product.score=Nutriscore::FR::SpecificScore.new(product_a)#<Nutriscore::FR::SpecificScore score=0# positive_score=#<Nutriscore::FR::PositiveScore score=4# fvnp=0 fibres=0 proteins=4># negative_score=#<Nutriscore::FR::NegativeScore score=4# energy=1 fat_saturated=1 sugar=2 sodium=0>>score.score.single# => 0score.score_class.single# => "B"
To be able to work with incomplete information, results are returned as ranges.The use of.single in the above example converts these to a single value (itreturnsnil if there is not enough information to get a single result). Thefollowing example shows what happens when data is missing.
score=Nutriscore::FR::SpecificScore.new(product_a.merge({sodium:nil}))score.score# => 0..10score.score.single# => nilscore.score_class# => "B".."C"score.score_class.single# => nil
Please only use#single and the regular RubyRange methods on#score and#score_class.Other methods do exist, but are not guaranteed to be stable across releases.
Different categories can use different score classes:
Nutriscore::FR::CheeseScorefor cheeseNutriscore::FR::FatsScorefor vegetable and animal oils and fatsNutriscore::FR::MineralWaterScorefor mineral waterNutriscore::FR::DrinksScorefor other drinksNutriscore::FR::SpecificScorefor other food products
Note that the score classes may use different nutrients (there is one additional nutrient,fat_total, used byFatsScore).
The UK has the same basis for computation, but it is used to determinewhether a product can be advertised (it must not be less healthy).
score=Nutriscore::UK::SpecificScore.new(product_a)score.score# => 0score.less_healthy?# => false
The method#less_healthy? is UK-specific, and returnstrue,false, ornilif there is not enough information to make a judgement.
By default, the fibres measurement method is AOAC (which is preferred), butit is possible to use fibres values measured with the NSP method:
# Acceptable values for the fibres_method are: :aoac and :nsp.score=Nutriscore::UK::SpecificScore.new(product_a,fibres_method::nsp)
Different categories can use different score classes:
Nutriscore::UK::SpecificScorefor food productsNutriscore::UK::DrinksScorefor drinks
This software is distributed under theMIT license.
About
nutriscore computation for Ruby
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.