0

I've started looking into SPARQL and I admit I find it very obscure and difficult.

I need to output the calories for all the foods.

I don't really understand the difference between the wd: wdt: p: and ps:

Similarly I am unclear on when to use P000 or Q000 ?

The best I got so far is:

SELECT ?food ?calories ?foodLabel ?caloriesLabel WHERE {
  ?food wdt:P31 wd:Q2095.
  ?food wdt:P31 ?calories.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Which gives : Results

Any suggestion would be welcome

svick
  • 236,525
  • 50
  • 385
  • 514
Sorade
  • 915
  • 1
  • 14
  • 29
  • `wdt:P31` is the *instance of* relationship, i.e. the first triple pattern returns all instances of the class food. I don't understand why you use the same relationship also in the second triple pattern. You would need to use a property that denotes the calories. – UninformedUser Jun 23 '17 at 05:46
  • But why do you think that this information exists? Do you have any example food in Wikidata with this information? You can click through the list of food that your query returns. I couldn't see any such information. – UninformedUser Jun 23 '17 at 05:49
  • Thank you for the comments. Well, when I had a look on Wikipedia at foods like apples, bacon etc for example they have a nutrition section in which calories are indicated. I'm not quite sure what the difference is between Wikipedia and Wikidata. How would you write the third line to use a property rather than an instance ? – Sorade Jun 23 '17 at 12:37
  • I have put the open food facts data on one of my servers. If you want to try it out, I will open it up to the public for a few hours. – Mark Miller Jun 23 '17 at 13:53
  • @Sorade What do you mean by "write the third line to use a property rather than an instance" ? The problem in your query is that you have to change the property of the second triple pattern, otherwise you will get back just the type that you specified as object in the first triple pattern. First triple pattern: "give me all instances of type `Food`". Second triple pattern "Give me all types of those foods, which is obviously again `Food`" – UninformedUser Jun 23 '17 at 14:13
  • Wikidata is not a mirror of Wikipedia. – UninformedUser Jun 23 '17 at 14:14
  • The OpenFoodFacts dataset might be more appropriate as @MarkMiller suggested. Loading it into a local triple store is not that difficult. – UninformedUser Jun 23 '17 at 14:16

1 Answers1

3

If you are willing to download a 1 GB RDF file and publish it at your own triplestore, this looks promising:

https://datahub.io/dataset/open-food-facts

Specifically, http://en.openfoodfacts.org/data/en.openfoodfacts.org.products.rdf (If English is your preferred languague.)

I say download and publish as Googling suggests these data aren't already exposed at any public endpoint.

You will likely have to use different classes and predicates, compared to WikiData.

Hey, this is pretty neat!

SELECT  *
WHERE
  { GRAPH <http://openfoodfacts.org>
      { ?s  a                     <http://data.lirmm.fr/ontologies/food#FoodProduct> ;
            <http://data.lirmm.fr/ontologies/food#name>  ?fn ;
            <http://data.lirmm.fr/ontologies/food#energyPer100g>  ?energy
      }
  }
LIMIT   9

gives

+------------------------------------------------------------------------------------------------------+---------------------------------------------+--------+
|                                                  s                                                   |                     fn                      | energy |
+------------------------------------------------------------------------------------------------------+---------------------------------------------+--------+
| http://world-en.openfoodfacts.org/product/9400547030446/heinz-big-n-chunky-chicken-corn              | "Heinz Big'n Chunky Chicken & Corn"         | "245"  |
| http://world-en.openfoodfacts.org/product/9400550004847/sour-patch-kids-pascall                      | "Sour Patch Kids"                           | "1440" |
| http://world-en.openfoodfacts.org/product/9400550602487/brunch-mixed-berry-bar-cadbury               | "Brunch Mixed Berry Bar"                    | "1810" |
| http://world-en.openfoodfacts.org/product/9400550646276/pascall-family-pack-sweets-fruit-bursts      | "Pascall Family Pack Sweets Fruit Bursts"   | "1427" |
| http://world-en.openfoodfacts.org/product/9400553011477/choc-thins-griffin-s                         | "Choc Thins"                                | "2010" |
| http://world-en.openfoodfacts.org/product/9400553438786/gingernuts-griffin-s                         | "Gingernuts"                                | "1810" |
| http://world-en.openfoodfacts.org/product/9400563448614/nice-natural-rosted-nut-bar-chocolate        | "Nice & Natural Rosted Nut Bar - Chocolate" | "2060" |
| http://world-en.openfoodfacts.org/product/9400563740589/nut-bar-caramel-cashew-flavour-nice-natural  | "Nut Bar - Caramel Cashew Flavour"          | "1960" |
| http://world-en.openfoodfacts.org/product/9400563741784/superfruits-cranberry-blueberry-nice-natural | "Superfruits - Cranberry & Blueberry"       | "1520" |
+------------------------------------------------------------------------------------------------------+---------------------------------------------+--------+
Mark Miller
  • 3,011
  • 1
  • 14
  • 34