Skip to content

Instantly share code, notes, and snippets.

@davidsbatista
Last active June 12, 2018 22:22
Show Gist options
  • Save davidsbatista/cdce57196bf84e3a988427b4d9ef9035 to your computer and use it in GitHub Desktop.
Save davidsbatista/cdce57196bf84e3a988427b4d9ef9035 to your computer and use it in GitHub Desktop.
Example of queries to DBpedia SPARQL Endpoint

DBpedia SPARQL Explorer: http://dbpedia.org/snorql/

DBpedia Virtuoso SPARQL Query Editor: https://dbpedia.org/sparql

=====

People and death-causes

PREFIX dbo: <http://dbpedia.org/ontology/> 
PREFIX dbp: <http://dbpedia.org/property/>

SELECT DISTINCT ?person ?death_cause ?wordnet_type

WHERE { ?person rdf:type dbo:Person .
        ?person rdfs:label ?label .
        OPTIONAL {?person dbp:deathCause ?death_cause} .
        ?person dbp:wordnet_type <http://www.w3.org/2006/03/wn/wn20/instances/synset-musician-noun-1> .
        ?person dbp:wordnet_type ?wordnet_type .
        ?person dbo:deathDate ?deathDate .
                 FILTER (?deathDate > "1980-01-01"^^xsd:date) .
}

}

Get all known American people of some foreign descent

SELECT DISTINCT(?isValueOf)
WHERE { 
   ?isValueOf <http://purl.org/dc/terms/subject> ?value .
   {
    SELECT DISTINCT(?value)
    WHERE { 
        ?resource <http://purl.org/dc/terms/subject> ?value .
        FILTER regex(?value, "Category:American_.*_descent", "i") 
    }
  }
}
ORDER BY ?isValueOf 

Get categories for different American people of some foreign descent

SELECT DISTINCT(?value)
WHERE { ?resource <http://purl.org/dc/terms/subject> ?value .
		FILTER regex(?value, 'Category:American_people_of.*descent', "i") }
ORDER BY ?value

Get all Portuguese writers/poets/philosophers

PREFIX dbc:<http://dbpedia.org/resource/Category:>
PREFIX dct:<http://purl.org/dc/terms/>

SELECT ?X   
WHERE {
	{ ?X dct:subject dbc:Portuguese_male_writers }
	UNION
	{ ?X dct:subject dbc:Portuguese_poets }
	UNION
	{ ?X dct:subject dbc:Portuguese_philosophers }
	UNION
	{ ?X dct:subject dbc:Portuguese_women_writers }
	UNION
	{ ?X dct:subject dbc:Portuguese_women_poets }
}

Get the abstract for Hemingway and his thumbnail picture

prefix dbpedia: <http://dbpedia.org/resource/>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>

select ?abstract ?thumbnail where { 
  dbpedia:Ernest_Hemingway dbpedia-owl:abstract ?abstract ;
                           dbpedia-owl:thumbnail ?thumbnail .
  filter(langMatches(lang(?abstract),"en"))
}

Retrieve a list of all the ingredients in Wikipedia.

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

SELECT DISTINCT ?ingredient_name
WHERE { ?food_recipe <http://dbpedia.org/ontology/ingredient> ?ingredient_name }
ORDER BY ?ingredient_name

Retrieve all the properties for an ingredient, for instance Brocolli

SELECT ?property ?hasValue ?isValueOf
WHERE {
  { <http://dbpedia.org/resource/Broccoli> ?property ?hasValue }
  UNION
  { ?isValueOf ?property <http://dbpedia.org/resource/Broccoli> }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment