Selecting Food Calories from SPARQL - sparql

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

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" |
+------------------------------------------------------------------------------------------------------+---------------------------------------------+--------+

Related

Column result unexpectedly returning nothing

Have a SPARQL query that returns nothing for Airport_Name when I believe it should.
PREFIX nas: <https://data.nasa.gov/ontologies/atmonto/NAS#>
PREFIX gen: <https://data.nasa.gov/ontologies/atmonto/general#>
SELECT *
WHERE{
{
{SELECT ?Airport_Name{
?Airport rdf:type nas:Airport ;
nas:airportName ?Airport_Name .
}
}
}UNION{
?Location rdf:type gen:PointLocation;
gen:longitude ?X ;
gen:latitude ?Y .
}
?Location rdf:type gen:PointLocation;
gen:longitude ?X ;
gen:latitude ?Y .
FILTER (?X > -80 && ?X < -64 && ?Y > 18 && ?Y < 32)
}
Expected result should return Location, X, Y, and Airport_Name
When I try to return Airport_Name at the bottom, the Location columns then appear blank, so im not sure what has been done wrongly. If anyone could point me in the right direction, that would be much appreciated.
Your query has several issues, the correct query would be something like below.
Query
SELECT *
WHERE
{ ?airport rdf:type/rdfs:subClassOf* nas:Airport ;
nas:airportName ?Airport_Name ;
nas:airportLocation ?location .
?location gen:longitude ?X ;
gen:latitude ?Y
FILTER ( ?X > "-80"^^xsd:float && ?X < "-64"^^xsd:float && ?Y > "18"^^xsd:float && ?Y < "32"^^xsd:float )
}
Result (sample with LIMIT 10)
--------------------------------------------------------------------------------------------------------------------------
| airport | Airport_Name | location | X | Y |
==========================================================================================================================
| nas:MYEMairport | "Governors Harbour" | nas:MYEMcoordinates | "-76.330178"^^xsd:float | "25.283586"^^xsd:float |
| nas:MDPCairport | "Punta Cana Intl" | nas:MDPCcoordinates | "-68.366186"^^xsd:float | "18.570781"^^xsd:float |
| nas:MUGTairport | "Mariana Grajales" | nas:MUGTcoordinates | "-75.158333"^^xsd:float | "20.085278"^^xsd:float |
| nas:MDSTairport | "Cibao Intl" | nas:MDSTcoordinates | "-70.604689"^^xsd:float | "19.406092"^^xsd:float |
| nas:MYLSairport | "Stella Maris" | nas:MYLScoordinates | "-75.268778"^^xsd:float | "23.583047"^^xsd:float |
| nas:MBNCairport | "North Caicos" | nas:MBNCcoordinates | "-71.939658"^^xsd:float | "21.917486"^^xsd:float |
| nas:MUMOairport | "Orestes Acosta" | nas:MUMOcoordinates | "-74.922222"^^xsd:float | "20.653889"^^xsd:float |
| nas:MYRPairport | "New Port Nelson" | nas:MYRPcoordinates | "-74.836186"^^xsd:float | "23.684378"^^xsd:float |
| nas:MYEGairport | "George Town" | nas:MYEGcoordinates | "-75.781670"^^xsd:float | "23.466667"^^xsd:float |
| nas:MUBYairport | "Carlos Manuel De Cespedes" | nas:MUBYcoordinates | "-76.621389"^^xsd:float | "20.396389"^^xsd:float |
--------------------------------------------------------------------------------------------------------------------------
Note, the query uses the property path rdf:type/rdfs:subClassOf* which only works if you've also loaded the ontology (NAS.ttl). The property path is necessary because the instance data (airportInst.ttl) uses subclasses of nas:Airport like nas:InternationAirport or nas:CanadianAirport.

Get all Wikidata items that have more than 10 languages?

I'm trying to get the most famous movies in the world from Wikidata with SPARQL.
I have the following query:
SELECT ?item WHERE {
?item wdt:P31 wd:Q11424.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
which returns ALL movies (about 214143).
I basically only need movies that have, let's say, more than 10 language entries on wikipedia, as I'm guessing these will be the most famous ones.
Is there a way to do this inside the query itself, without checking all entries ?
A naive answer to your question is:
SELECT ?movie (count(?wikipage) AS ?count) WHERE {
hint:Query hint:optimizer "None" .
?movie wdt:P31 wd:Q11424 .
?wikipage schema:about ?movie .
?wikipage schema:isPartOf/wikibase:wikiGroup "wikipedia"
} GROUP BY ?movie HAVING (?count > 10) ORDER BY DESC(?count)
Try it!
Alternatively, you could consider total number of sitelinks. Sitelinks include links to Wikipedia and also links to Wikiquote, Wikivoyage etc. The advantage is that total number of sitelinks is precomputed.
SELECT ?movie ?sitelinks WHERE {
?movie wdt:P31 wd:Q11424 .
?movie wikibase:sitelinks ?sitelinks .
FILTER (?sitelinks > 10)
} ORDER BY DESC(?sitelinks)
Try it!
See also these questions:
Get Wikipedia URLs (sitelinks) in Wikidata SPARQL query
Wikidata results sorted by something similar to a PageRank
As #TallTed and #AKSW have pointed out, the number of labels in different languages may be differ from the number of Wikipedia articles in different languages. Here below a comparison.
Top 5 movies by Wikipedia articles
| title | articles | sitelinks | labels |
|---------------------|----------|-----------|--------|
| Avatar | 92 | 103 | 99 |
| Titanic | 86 | 100 | 101 |
| The Godfather | 79 | 103 | 82 |
| Slumdog Millionaire | 72 | 75 | 80 |
| Forrest Gump | 71 | 101 | 84 |
Top 5 movies by sitelinks
| title | articles | sitelinks | labels |
|---------------|----------|-----------|--------|
| Avatar | 92 | 103 | 99 |
| The Godfather | 79 | 103 | 82 |
| Forrest Gump | 71 | 101 | 84 |
| Titanic | 86 | 100 | 101 |
| The Matrix | 67 | 94 | 77 |
Top 5 movies by labels
| title | articles | sitelinks | labels |
|------------------------------|----------|-----------|--------|
| The 25th Reich | 2 | 2 | 227 |
| Time Is But Brief | 0 | 0 | 224 |
| Michael Moore in TrumpLand | 6 | 6 | 222 |
| Magnus - The Mozart of Chess | 1 | 1 | 221 |
| Lee Chong Wei | 1 | 1 | 196 |

Get all Countries Name from DBpedia using SPARQL

I want list of countries name from DBpedia.
I am using http://dbpedia.org/snorql/ to execute my query, but till now I have not found all countries name which are available in DBpedia.
For Example : dbr:United_Kingdom, dbr:India, dbr:United_States, etc.
Is the problem
You don't know how to write SPARQL in general? (That's OK, it's hard to get started.)
You don' know about the classes and predicates in DBpedia? (That's OK too. I have to check each time.)
Something else?
This gets all UN member nations. Getting "all countries" is probably just a matter of finding the right class in their ontology.
select distinct ?s
where { ?s a <http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations> }
I happen to know that New York City is the largest city in the United States, and that DBpedia has a largestCity predicate and a New_York_City instance. So I wrote a query that should only get the United States as the subject, and then asked for all connected predicates and objects. You should look in that for an object that meets your exceptions for defining "all countries." If you don't find one, you may have to union a few other triple patterns into one query.
I have also filtered out objects that contain either of two terms that be relevant for you: "country" or "nation"
select distinct *
where { ?s a <http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations> ;
dbo:largestCity dbr:New_York_City ;
?p ?o
filter(isURI(?o))
filter((regex(lcase(str(?o)), "country")) || (regex(lcase(str(?o)), "nation")))
}
Gives the foloowing, which should help you write a followup question that isn't specific to the United States.
+--------------------------------------------+---------------------------------------------------+------------------------------------------------------------------------------+
| s | p | o |
+--------------------------------------------+---------------------------------------------------+------------------------------------------------------------------------------+
| http://dbpedia.org/resource/United_States | http://dbpedia.org/ontology/wikiPageExternalLink | http://www.ifs.du.edu/ifs/frm_CountryProfile.aspx?Country=US |
| http://dbpedia.org/resource/United_States | http://dbpedia.org/ontology/wikiPageExternalLink | http://nationalatlas.gov/ |
| http://dbpedia.org/resource/United_States | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://dbpedia.org/class/yago/Country108544813 |
| http://dbpedia.org/resource/United_States | http://purl.org/dc/terms/subject | http://dbpedia.org/resource/Category:G7_nations |
| http://dbpedia.org/resource/United_States | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://schema.org/Country |
| http://dbpedia.org/resource/United_States | http://www.w3.org/2000/01/rdf-schema#seeAlso | http://dbpedia.org/resource/Anti-miscegenation_laws |
| http://dbpedia.org/resource/United_States | http://purl.org/dc/terms/subject | http://dbpedia.org/resource/Category:Member_states_of_the_United_Nations |
| http://dbpedia.org/resource/United_States | http://www.w3.org/2002/07/owl#sameAs | http://transparency.270a.info/classification/country/US |
| http://dbpedia.org/resource/United_States | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://dbpedia.org/ontology/Country |
| http://dbpedia.org/resource/United_States | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations |
| http://dbpedia.org/resource/United_States | http://purl.org/dc/terms/subject | http://dbpedia.org/resource/Category:G8_nations |
| http://dbpedia.org/resource/United_States | http://www.w3.org/2002/07/owl#sameAs | http://linked-web-apis.fit.cvut.cz/resource/united_states_of_america_country |
| http://dbpedia.org/resource/United_States | http://dbpedia.org/ontology/wikiPageExternalLink | http://www.nationalcenter.org/HistoricalDocuments.html |
| http://dbpedia.org/resource/United_States | http://dbpedia.org/ontology/wikiPageExternalLink | http://news.bbc.co.uk/2/hi/americas/country_profiles/1217752.stm |
| http://dbpedia.org/resource/United_States | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://umbel.org/umbel/rc/Country |
| http://dbpedia.org/resource/United_States | http://purl.org/dc/terms/subject | http://dbpedia.org/resource/Category:G20_nations |
+--------------------------------------------+---------------------------------------------------+------------------------------------------------------------------------------+

Following namespaces in SPARQL query

Given this data structure
#prefix core <http://www.w3.org/2004/02/skos/core#>
<http://localhost/myvocab/1> core#notation 1 .
<http://localhost/myvocab/1> <http://www.w3.org/2006/time#inDateTime> <http://localhost/myvocab/item1#DateDescription> .
<http://localhost/myvocab/item1#DateDescription> <http://www.w3.org/2006/time#year> 2016 ;
<http://www.w3.org/2006/time#month> "June"
If I run
select * where {?s ?p ?o .
<http://www.w3.org/2004/02/skos/core#notation> 1 . }
then only the first 2 triples (:hasID and :hasTime) are returned. Is there a sparql query (preferably not using a regex filter for matching the id) to return all triples from the child namespaces too?
I'm hoping I can achieve a result something along the lines of
-------------------------------------------------------------------------------------------------------------------------------------------------------
| s | p | o |
=======================================================================================================================================================
| <http://localhost/myvocab/item1> | <http://www.w3.org/2006/time#inDateTime> | <http://localhost/myvocab/item1#DateDescription |
| <http://localhost/myvocab/item1> | <http://www.w3.org/2004/02/skos/core#notation> | 1 |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#month> | "June" |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#year> | 2016 |
-------------------------------------------------------------------------------------------------------------------------------------------------------
It always helps if you provide data that we can actually load and property formed queries that you've tried. The data that you've shown isn't actually legal, nor is the query that you provided. Here's some sample data that's actually loadable:
#prefix core: <http://www.w3.org/2004/02/skos/core#>
<http://localhost/myvocab/1> core:notation 1 ;
<http://www.w3.org/2006/time#inDateTime> <http://localhost/myvocab/item1#DateDescription> .
<http://localhost/myvocab/item1#DateDescription> <http://www.w3.org/2006/time#year> 2016 ;
<http://www.w3.org/2006/time#month> "June"
If I understand you, you want to follow the :item1_DateTime object's properties too. You can do that with a property path that follows :item1's properties and value, and so on. In this query, I use (:|!:) as a wildcard, since every property is either : or it isn't. The * at the end means to follow paths of arbitrary length.
prefix core: <http://www.w3.org/2004/02/skos/core#>
select ?s ?p ?o where {
?s_ core:notation 1 ;
(<>|!<>)* ?s .
?s ?p ?o .
}
--------------------------------------------------------------------------------------------------------------------------------------------------
| s | p | o |
==================================================================================================================================================
| <http://localhost/myvocab/1> | <http://www.w3.org/2006/time#inDateTime> | <http://localhost/myvocab/item1#DateDescription> |
| <http://localhost/myvocab/1> | core:notation | 1 |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#month> | "June" |
| <http://localhost/myvocab/item1#DateDescription> | <http://www.w3.org/2006/time#year> | 2016 |
--------------------------------------------------------------------------------------------------------------------------------------------------

Duplicate output results with SPARQL [duplicate]

This question already has an answer here:
Aggregating results from SPARQL query
(1 answer)
Closed 9 years ago.
I'm new to the semantic web concept, and I have a diploma work based on the semantic web. In my OWL ontology, I have defined the individuals in he following manner:
<Announce rdf:ID="Ann1" ...>
<...>
<...>
<requiredTechnologies>
<Technology rdfs:resource="tech1"/>
</requiredTechnologies>
<requiredTechnologies>
<Technology rdfs:resource="tech2"/>
</requiredTechnologies>
</Announce>
...
Basically, there are some properties that appear only once for a given individual, but the property "required technologies" can be used multiple times in one record (for one individual). So, when I run a SPARQL query, selecting all the data (I use Jena), I get this output:
=====================================
| "Ann1" | ... | ... | "tech1" |
| "Ann1" | ... | ... | "tech2" |
| "Ann2" | ... | ... | "tech3" |
| "Ann3" | ... | ... | "tech4" |
| "Ann3" | ... | ... | "tech5" |
| "Ann3" | ... | ... | "tech6" |
| ... | ... | ... | ... |
=====================================
The records where multiple "requiredTechnologies" attribute is present appear more then once. My question is how to get all the technologies that belong to a given individual in one line (something like this):
===================================================
| "Ann1" | ... | ... | "tech1, tech2" |
| "Ann2" | ... | ... | "tech2" |
| "Ann3" | ... | ... | "tech4, tech5, tech6" |
| ... | ... | ... | ... |
===================================================
Is there a way in SPARQL to get the multiple attribute in a list? Or any other workaround?
Yes, use the GROUP BY clause in conjunction with the GROUP_CONCAT aggregate.
Here's a generic example, you should easily be able to adapt to your data model:
SELECT ?s (GROUP_CONCAT(?value ; SEPARATOR = ",") AS ?values)
WHERE
{
?s <http://somePredicate> ?value .
}
GROUP BY ?s