SPARQL python

De Wikipast
Aller à : navigation, rechercher


Installation

Pour installer SPARQLWrapper, nous utilisons pip (correction(s): piu, pop, pi, pipo, pipi, pin, zip, più, hip, pif, pipa, bip, kip, pic, pie, pipé, pep, pipe, pis ) (qui est installé par défaut, dans le cas contraire installez pip (correction(s): piu, pop, pi, pipo, pipi, pin, zip, più, hip, pif, pipa, bip, kip, pic, pie, pipé, pep, pipe, pis )). Ouvrez un terminal et tapez la commande suivante:

<span style="color:red">pip</span> (correction(s): <span style="color:green">piu, pop, pi, pipo, pipi, pin, zip, più, hip, pif, pipa, bip, kip, pic, pie, pipé, pep, pipe, pis
</span>) install SPARQLWrapper

Exemple de code python pour interroger la base SPARQL

from SPARQLWrapper import SPARQLWrapper, JSON

sparql=SPARQLWrapper("http://iccluster052.iccluster.epfl.ch:8899/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