Lab: SPARQL Programming: Difference between revisions

From info216
Line 15: Line 15:
No additional installation is needed. You are already running Python and rdflib.
No additional installation is needed. You are already running Python and rdflib.


Parse the file ''russia_investigation_kg.ttl'' into an rdflib Graph. (The original file is available here: [[File:russia_investigation_kg.txt]], but remember to give it the correct extension (''.ttl'', not ''.txt'').  
Parse the file ''russia_investigation_kg.ttl'' into an rdflib Graph. (The original file is available here: [[File:russia_investigation_kg.txt]], but rename it from ''.ttl'' to ''.txt'').  


'''Task:'''
'''Task:'''

Revision as of 11:26, 1 February 2023

Topics

SPARQL programming in Python:

  • with rdflib: to manage an rdflib Graph internally in a program
  • with SPARQLWrapper and Blazegraph: to manage an RDF graph stored externally in Blazegraph (on your own local machine or on the shared online server)

Motivation: Last week we entered SPARQL queries and updates manually from the web interface. But in the majority of cases we want to program the management of triples in our graphs, for example to handle automatic or scheduled updates.

Useful materials

Tasks

SPARQL programming in Python with rdflib

Getting ready: No additional installation is needed. You are already running Python and rdflib.

Parse the file russia_investigation_kg.ttl into an rdflib Graph. (The original file is available here: File:Russia investigation kg.txt, but rename it from .ttl to .txt).

Task: Write the following queries and updates with Python and rdflib. See boilerplate examples below.

  • Print out a list of all the predicates used in your graph.
  • Print out a sorted list of all the presidents represented in your graph.
  • Create dictionary (Python dict) with all the represented presidents as keys. For each key, the value is a list of names of people indicted under that president.
  • Use an ASK query to investigate whether Donald Trump has pardoned more than 5 people.
  • Use a DESCRIBE query to create a new graph with information about Donald Trump. Print out the graph in Turtle format.

Contents of the file 'spouses.ttl':

@prefix ex: <http://example.org/> .
@prefix schema: <https://schema.org/> .

ex:Donald_Trump schema:spouse ( ex:IvanaTrump ex:MarlaMaples ex:MelaniaTrump ) .

Boilerplate code for rdflib query:

from rdflib import Graph

g = Graph()
g.parse("spouses.ttl", format='ttl')
result = g.query("""
    PREFIX ex: <http://example.org/>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX schema: <https://schema.org/>

    SELECT ?spouse WHERE {
        ex:Donald_Trump schema:spouse / rdf:rest* / rdf:first ?spouse .
    }""")
for row in result:
    print("Donald has spouse %s" % row)

Boilerplate code for rdflib update: This is the KG4News graph again:

from rdflib import Graph

update_str = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
PREFIX ss: <http://semanticscholar.org/>

INSERT DATA {    
    kg:paper_123 rdf:type ss:Paper ;
               ss:title "Semantic Knowledge Graphs for the News: A Review"@en ;
            kg:year 2022 ;
            dct:contributor kg:auth_456, kg:auth_789 . 
}"""

g = Graph()
g.update(update_str)
print(g.serialize(format='ttl'))  # format=’turtle’ also works

SPARQL programming in Python with SPARQLWrapper and Blazegraph

Getting ready: Make sure you have to access to a running Blazegraph as in Exercise 3: SPARQL. You can either run Blazegraph locally on your own machine (best) or online on a shared server at UiB (also ok).

Install SPARQLWrapper (in your virtual environment):

pip install SPARQLWrapper

Some older versions also require you to install requests API. The SPARQLWrapper page on GitHub contains more information.

Continue working with the RDF graph you created in exercises 1-2 (perhaps creating a new namespace in Blazegraph first.)

Task: Program the following queries and updates with SPARQLWrapper and Blazegraph.

  • Use a DESCRIBE query to create an rdflib Graph about Oliver Stone. Print the graph out in Turtle format.

Boilerplate code for SPARQLWrapper query:

from SPARQLWrapper import SPARQLWrapper

SERVER = 'http://sandbox.i2s.uib.no/bigdata/'       # you may want to change this
NAMESPACE = 's03'                                   # you most likely want to change this

endpoint = f'{SERVER}namespace/{NAMESPACE}/sparql'  # standard path for Blazegraph queries

query = """
    PREFIX ex: <http://example.org/>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX schema: <https://schema.org/>

    SELECT ?spouse WHERE {
 	 	ex:Donald_Trump schema:spouse / rdf:rest* / rdf:first ?spouse .
    }"""
    
client = SPARQLWrapper(endpoint)
client.setReturnFormat('json')
client.setQuery(query)

print('Spouses:')
results = client.queryAndConvert()
for result in results["results"]["bindings"]:
    print(result["spouse"]["value"])

Boilerplate code for SPARQLWrapper update:

from SPARQLWrapper import SPARQLWrapper

SERVER = 'http://sandbox.i2s.uib.no/bigdata/'       # you may want to change this
NAMESPACE = 's03'                                   # you most likely want to change this

endpoint = f'{SERVER}namespace/{NAMESPACE}/sparql'  # standard path for Blazegraph updates

update_str = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX kg: <http://i2s.uib.no/kg4news/>
PREFIX ss: <http://semanticscholar.org/>

INSERT DATA {    
    kg:paper_123 rdf:type ss:Paper ;
               ss:title "Semantic Knowledge Graphs for the News: A Review"@en ;
            kg:year 2023 ;
            dct:contributor kg:auth_654, kg:auth_789 . 
}"""

client = SPARQLWrapper(endpoint)
client.setMethod('POST')
client.setQuery(update_str)
res = client.queryAndConvert()


The different types of queries requires different return formats:

  • SELECT and ASK: a SPARQL Results Document in XML, JSON, or CSV/TSV format.
  • DESCRIBE and CONSTRUCT: an RDF graph serialized, for example, in the TURTLE or RDF/XML syntax, or an equivalent RDF graph serialization.

Remember to make sure that you can see the changes that take place after your inserts.