Function Repository Resource:

NutritionReport

Source Notebook

Generate a nutrition report about foods specified in text

Contributed by: Isabel Skidmore

ResourceFunction["NutritionReport"][ingredients]

returns nutrition information for ingredients as a Dataset.

ResourceFunction["NutritionReport"][ingredients, format]

returns information in the specified format.

Details and Options

NutritionReport can handle a variety of input forms:
"ingredients"a single string, with different foods separated by a delimiting character (default "\n")
{"ingredient1","ingredient2",}a list of strings
{{"amount1","food1","note1"},}a list of lists of foods, amounts and notes, with each element given as a string

In the third form above, the "notei" parameters can be useful for tagging purposes, but do not affect numerical results.

The following values of format are accepted:
"Dataset" (default) returns nutrition as a Dataset
"Association"returns nutrition as a nested Association
"Table"returns nutrition as a nested List
"ASCIITable"returns nutrition as an ASCII table
The following options are accepted:
"DelimiterCharacter""\n"character used to separate ingredients within a single string
"NutritionProperties"{"AbsoluteTotalCaloriesContent","TotalFat", "TotalProtein","TotalCarbohydrates"}properties to be returned for each food
"RemoveAfterComma"Truewhether to trim any non-essential text after a comma for each food
"AdditionalFluff"{}additional strings to remove from food text to improve interpretation
An element of the "NutritionProperties" option value can be specified either as an expression of the form EntityProperty["Food" ,"standard name"], or as the bare string "standard name". A full list of available properties can be obtained by evaluating EntityValue["Food", "Properties"]

Examples

Basic Examples (1) 

Get nutrition information about the foods specified in a piece of text:

In[1]:=
ResourceFunction[
 "NutritionReport"]["½ cup (1 stick) butter, at room temperature
1 cup sugar
2 eggs
2 cups all-purpose flour"]
Out[1]=

Scope (3) 

Get nutrition information as an ASCII table, with columns delimited by pipe characters ("|"):

In[2]:=
ResourceFunction[
 "NutritionReport"][{"100g rice", "5oz grilled chicken"}, "ASCIITable"]
Out[2]=

Get nutrition information as a nested List:

In[3]:=
data = ResourceFunction[
  "NutritionReport"][{"100g rice", "5oz grilled chicken"}, "Table"]
Out[3]=

Use TableForm to display this output with foods varying horizontally and nutrition properties varying vertically:

In[4]:=
data // TableForm
Out[4]=

Get nutrition information as a nested Association:

In[5]:=
ResourceFunction[
 "NutritionReport"][{"100g rice", "5oz grilled chicken"}, "Association"]
Out[5]=

Options (6) 

DelimiterCharacter (1) 

Use "DelimiterCharacter" to specify how to split up foods in a string:

In[6]:=
ResourceFunction[
 "NutritionReport"]["1 egg; 200g sugar; 8tbsp butter", "DelimiterCharacter" -> ";"]
Out[6]=

NutritionProperties (2) 

Specify the desired nutrition properties using the "NutritionProperties" option:

In[7]:=
ResourceFunction["NutritionReport"]["1 egg
200g sugar
8tbsp butter", "NutritionProperties" -> {EntityProperty["Food", "RelativeAshContent"], EntityProperty["Food", "RelativeTotalCarbohydratesContent"], EntityProperty["Food", "RelativeTotalFatContent"], EntityProperty["Food", "RelativeTotalProteinContent"]}]
Out[7]=

Lists of valid "Food" properties can be obtained using EntityValue:

In[8]:=
allFoodProperties = EntityValue["Food", "Properties"];
RandomSample[allFoodProperties, 5]
Out[9]=

Finding a particular property of interest can be simplified by querying particular EntityClass expressions:

In[10]:=
EntityValue["Food", "PropertyClasses"]
Out[10]=
In[11]:=
EntityValue[
 EntityPropertyClass["Food", "Macronutrients"], "Properties"]
Out[11]=

RemoveAfterComma (2) 

Use "RemoveAfterComma"False if there is important information to be retained after commas:

In[12]:=
ResourceFunction[
 "NutritionReport"][{"100g rice", "5oz chicken, grilled"}, "RemoveAfterComma" -> False]
Out[12]=

The default setting "RemoveAfterComma"True ignores the preparation of the chicken in this example, and thus returns different results:

In[13]:=
ResourceFunction[
 "NutritionReport"][{"100g rice", "5oz chicken, grilled"}]
Out[13]=

AdditionalFluff (1) 

There are some words that are filtered out of food strings by default as they do not impact nutrition. Use "AdditionalFluff" if there are additional strings that can be removed to improve interpretation (note that "fluff" strings are case-sensitive):

In[14]:=
ResourceFunction["NutritionReport"]["100g flour
100ml lukewarm tap water", "AdditionalFluff" -> {"tap", "lukewarm"}]
Out[14]=

Properties and Relations (1) 

NutritionReport uses Interpreter["Food"] internally to convert strings into computable "Food" entities:

In[15]:=
Interpreter["Food"]["Egg"]
Out[15]=
In[16]:=
%[EntityProperty["Food", "RelativeTotalFatContent"]]
Out[16]=

Possible Issues (2) 

More specific foods can be difficult to find data for:

In[17]:=
ResourceFunction[
 "NutritionReport"]["8 oz lightly grilled thawed chicken breast with fat removed"]
Out[17]=

Use simple strings with only necessary information to get better results:

In[18]:=
ResourceFunction["NutritionReport"]["8 oz grilled chicken breast"]
Out[18]=

Use options and specific input formats to help NutritionReport understand what information is essential:

In[19]:=
ResourceFunction[
 "NutritionReport"][{{"8oz", "chicken breast", "lightly grilled, thawed, with fat removed"}}, "AdditionalFluff" -> "lightly"]
Out[19]=

Note that some "Food" properties are non-quantitative in nature. Using these in NutritionReport can cause errors:

In[20]:=
ResourceFunction["NutritionReport"]["1 egg, 1 carrot", "DelimiterCharacter" -> ",", "NutritionProperties" -> {EntityProperty["Food", "GeometryType"], EntityProperty["Food", "FiberLabel"]}]
Out[20]=

A large subset of strictly quantitative nutritional properties can be obtained as follows:

In[21]:=
Select[EntityValue["Food", "Properties"], StringContainsQ[Last@#, "content", IgnoreCase -> True] &];
RandomSample[%, 5]
Out[22]=

Version History

  • 1.0.0 – 14 April 2023

Related Resources

Author Notes

Other output formats that show nice reports, pie charts, etc. can be added with time. There is also endless tweaking to the string cleaning process that can be done; I have included common strings that I find to be irrelevant to nutrition but there could always be more. The more specific the default behavior is the less versatile, so I like to keep it flexible for users by using an option.

License Information