SPARQL python
Aller à la navigation
Aller à la recherche
Installation
Pour installer SPARQLWrapper, nous utilisons pip (qui est installé par défaut, dans le cas contraire installez pip). Ouvrez un terminal et tapez la commande suivante:
pip install SPARQLWrapper
Exemple de code python pour interroger la base SPARQL
from SPARQLWrapper import SPARQLWrapper, JSON #sparql=SPARQLWrapper("http://128.178.21.39:8890/sparql") sparql=SPARQLWrapper("http://iccluster056.iccluster.epfl.ch:8890/sparql") sparql.setQuery(""" select distinct STR(?f) as ?fonction where { ?p a lt-owl:PersonMention . ?p lt-owl:name ?name . ?p lt-owl:function ?f . FILTER contains(?name, "Jacques Chirac") } ORDER BY ASC(?f) """) sparql.setReturnFormat(JSON) results=sparql.query().convert() for result in results["results"]["bindings"]: print(result['fonction']["value"])
Exemples de requêtes SPARQL
having information about person mentions
select STR(?name) AS ?name STR(?firstname) AS ?firstname STR(?lastname) AS ?lastname STR(?function) AS ?function STR(?functionType) AS ?functiontype STR(?nationality) AS ?nationality STR(?issue) AS ?issue where { ?p a lt-owl:PersonMention . ?p lt-owl:name ?name . OPTIONAL {?p lt-owl:firstname ?firstname } OPTIONAL {?p lt-owl:lastname ?lastname } OPTIONAL {?p lt-owl:function ?function .} OPTIONAL {?p lt-owl:functionType ?functionType .} OPTIONAL {?p lt-owl:nationality ?nationality .} ?p lt-owl:mentionedIn ?artComp . ?artComp lt-owl:box ?box . ?artComp lt-owl:publication ?issue . } limit 50
distinct titles of a specific person
SELECT distinct STR(?t) AS ?function WHERE { ?p a lt-owl:PersonMention . ?p lt-owl:name ?name . ?p lt-owl:function ?t . FILTER(regex(str(?name), "Jacques Chirac")) }
all title for a person ordered by year
SELECT ?year STR(?issue) AS ?issue STR(?f) AS ?function WHERE { ?p a lt-owl:PersonMention . ?p lt-owl:name ?name . ?p lt-owl:function ?f . ?p lt-owl:mentionedIn ?artComp . ?artComp lt-owl:issueDate ?date . ?artComp lt-owl:publication ?issue . BIND (year(?date) AS ?year ). FILTER contains(?name, "Jacques Chirac") } ORDER BY ?year
all articles talking of a specific person having a specific title
SELECT STR(?issue) AS ?issue STR(?date) AS ?date STR(?title) AS ?articleTitle STR(?name) AS ?name WHERE { ?p a lt-owl:PersonMention . ?p lt-owl:name ?name . ?p lt-owl:function ?f . ?p lt-owl:mentionedIn ?artComp . ?artComp lt-owl:issueDate ?date . ?artComp lt-owl:publication ?issue . ?artComp dct:title ?title . BIND (year(?date) AS ?year ). FILTER contains(?name, "Jacques Chirac") FILTER contains(?f, "secrétaire d'Etat" ) } ORDER BY ?year
all articles talking of a specific person having a specific title
SELECT STR(?issue) AS ?issue STR(?date) AS ?date STR(?title) AS ?articleTitle STR(?name) AS ?name WHERE { ?p a lt-owl:PersonMention . ?p lt-owl:name ?name . ?p lt-owl:function ?f . ?p lt-owl:mentionedIn ?artComp . ?artComp lt-owl:issueDate ?date . ?artComp lt-owl:publication ?issue . ?artComp dct:title ?title . BIND (year(?date) AS ?year ). FILTER contains(?name, "Jacques Chirac") FILTER contains(?f, "secrétaire d'Etat" ) } ORDER BY ?year
all the "violoniste"
SELECT STR(?issue) AS ?issue STR(?date) As ?date STR(?f) AS ?function STR(?name) AS ?name WHERE { ?p a lt-owl:PersonMention . ?p lt-owl:name ?name . ?p lt-owl:function ?f . ?p lt-owl:mentionedIn ?artComp . ?artComp lt-owl:issueDate ?date . ?artComp lt-owl:publication ?issue . ?artComp dct:title ?title . BIND (year(?date) AS ?year ). FILTER contains(?f, "violoniste" ) }
number of personMention over a specific time window
SELECT ?year count(distinct ?p) WHERE { ?p a lt-owl:PersonMention . ?p lt-owl:mentionedIn ?artComp . ?artComp lt-owl:issueDate ?date . BIND (year(?date) AS ?year ) FILTER (?date > "1914-01-01T05:00:00"^^xsd:dateTime) FILTER (?date < "1920-12-31T05:00:00"^^xsd:dateTime) } GROUP BY ?year ORDER BY ASC(?year)
get location mentions
SELECT STR(?name) AS ?locationName STR(?issue) AS ?journal ?year AS ?year WHERE { ?l a lt-owl:LocationMention . ?l lt-owl:name ?name . ?l lt-owl:mentionedIn ?artComp . ?artComp lt-owl:issueDate ?date . ?artComp lt-owl:publication ?issue . BIND (year(?date) AS ?year ) } LIMIT 200