14
$\begingroup$

I want to plot some nutritional values and do some computation with it.

Is there an analogue of CountryData[] to get a list of all the foods for which WA has information?

$\endgroup$

2 Answers 2

14
$\begingroup$

Go to a new cell in a notebook and write "=", this will create an orange equality sign in the beginning of the cell. This means that you have entered the free form input mode. Write the name of a food item or a type of food and evaluate the cell, Mathematica will then return the entity corresponding to that food item or food type. You can hover the entity to see what kind of entity it is. Then you can use EntityList to see all entities of this type. For example:

Mathematica screenshot

Here I searched for "pasta" and hovered on the resulting entity, which is of the food type, to find out that the entity type is called "Food". I can now retrieve all entities of this type:

Length@EntityList[Entity["Food"]]
(* Out: 33158 *)

This is a list of 33158 food items that Mathematica knows about. It is probably the same as what Wolfram Alpha knows, although this kind of correspondence between Mathematica and Wolfram Alpha is not guaranteed.

EntityProperties can be used to list the kind of information that you can retrieve about a food item.

Mathematica graphics

The result is a bunch of entities. All of that information is not available for all items, but those are the properties that you can try to retrieve at least. Sometimes it works, sometimes it doesn't work. EntityValue is the function that is used to retrieve a property.

Mathematica screenshot

Mathematica graphics

$\endgroup$
11
$\begingroup$

I remember from last years conference (Wolfram Technology Conference 2015) that there was a presentation on food entities. It doesn't seem to have been recorded but you can get the presentation (Food in the Wolfram Language).

It shows a great deal on how to access foods and gives details on some of their properties. The food data is somewhat deep and very wide. Below I look at nutritional properties and the "FoodType" entity. Then I use these to examine a food's vitamins starting from the food type and working down to a particular food.

RandomEntity["Food", 3]

Mathematica graphics

Nutritional properties

EntityValue["Food", "PropertyCount"]
(* 561 *)

RandomSample[EntityValue["Food", "Properties"], 3]

Mathematica graphics

EntityValue["Food", EntityProperty["Food", "RelativeProteinCaloriesContent"], "Definition"]
(* "The amount of protein calories in a food." *)

FoodType

A "FoodType" entity is a more generic food that has no specific nutritional information, but instead represents a class of foods that could come in many variations.

EntityValue["FoodType", "EntityCount"]
(* 1057 *)

RandomEntity["FoodType", 3]

Mathematica graphics

A look at Milk

EntityProperties[Entity["FoodType", "Milk"]]

Mathematica graphics

We can use "CategoricalPropertyValues" to narrow down to a certain instance of milk. The "Variety" list for milk is used below. This can be left off for an Association of all categorical property values.

EntityValue[Entity["FoodType", "Milk"], "CategoricalPropertyValues"][
  EntityProperty["Food", "Variety"]]

Mathematica graphics

Now lets look at the vitamins available in one of these varieties of milk by creating an implicit entity for human milk.

breastIsBest = 
 Entity["Food", {EntityProperty["Food", "FoodType"] -> Entity["FoodType", "Milk"], 
   EntityProperty["Food", "Variety"] -> Entity["FoodVariety", "Human"]}]

Mathematica graphics

With EntityList we can obtain a human milk food entity to query.

laLecheLeague = First@EntityList[breastIsBest]

Mathematica graphics

As was seen above there are many nutritional properties. Below the selection is limited to those with "vitamins" in their CanonicalName.

vitamins = 
 Select[StringContainsQ[CanonicalName@#, "vitamin", IgnoreCase -> True] &]@
   EntityProperties[laLecheLeague];

Now we can query laLecheLeague for the vitamins information. The Missing entries are deleted.

DeleteMissing@
 EntityValue[laLecheLeague, vitamins, "PropertyAssociation"]

Mathematica graphics

The typical serving size can also be queried.

EntityValue[laLecheLeague, "DefaultServingSizeMass"]
(* Quantity[30.8, "Grams"] *)

Hope this helps.

For more information on breast feeding visit La Leche League International

$\endgroup$
3
  • 3
    $\begingroup$ If you could summarize the most useful information and put it on here that would be appreciated. (See also this and this.) $\endgroup$
    – C. E.
    Apr 8, 2016 at 0:20
  • 1
    $\begingroup$ @C.E. I see. I will add some detail to this soon. $\endgroup$
    – Edmund
    Apr 8, 2016 at 10:57
  • $\begingroup$ This is a very helpful exposition. I upvoted it. However it does not really answer the (quite narrow) question, so I have accepted the other one. Thanks a lot! $\endgroup$
    – Andrea
    Apr 11, 2016 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.