#Install a library to help us run some SPARQL queries if we haven't already installed it #http://rdflib.github.io/sparqlwrapper/ #!pip3 install sparqlwrapper #Import the necessary packages from SPARQLWrapper import SPARQLWrapper, JSON #Declare the Open University open Linked Data endpoint endpoint="http://data.open.ac.uk/query" #A helper... def runQuery(endpoint,prefix,q): ''' Run a SPARQL query with a declared prefix over a specified endpoint ''' sparql = SPARQLWrapper(endpoint) sparql.setQuery(prefix+q) sparql.setReturnFormat(JSON) return sparql.query().convert() import pandas as pd #And some more helpers def dict2df(results): ''' Hack a function to flatten the SPARQL query results and return the column values ''' data=[] for result in results["results"]["bindings"]: tmp={} for el in result: tmp[el]=result[el]['value'] data.append(tmp) df = pd.DataFrame(data) return df def dfResults(endpoint,prefix,q): ''' Generate a data frame containing the results of running a SPARQL query with a declared prefix over a specified endpoint ''' return dict2df( runQuery( endpoint, prefix, q ) ) #Sometimes it's convenient to build a default prefix to handle most queries to the endpoint prefix=''' ''' #Prices (ordered) with currency of OU level 1 courses in Arts and Humanities as available in France. #via http://data.open.ac.uk/site/queries.html q=''' #We're looking for some course identifiers along with the value and currency of their price select ?course ?price ?cur from where { #The course must be at level 1 ?course "1"^^. #The course must be in in Arts and Humanities ?course . #The course must be part of an offering ?off ?course. #And that offering must have a price ?off ?ps. #The course must be located(?) in France ?course . #And the offering must be available in France ?off . #The price must have a value ?ps ?price. #The price must also be in a particular currency ?ps ?cur } order by ?price #The ordering ranks the results in increasing price order ''' #We can now run the query over the endpoint with the desired prefix #and get the results back as a pandas dataframe dfResults(endpoint,prefix,q) q='DESCRIBE ' ans=runQuery(endpoint,prefix,q) print(ans.decode('utf-8')) prefix=''' PREFIX rdfs: PREFIX dcterms: PREFIX aiiso: ''' q=''' SELECT ?code ?title ?desc WHERE { aiiso:code ?code. rdfs:label ?title. dcterms:description ?desc. } ''' runQuery(endpoint,prefix,q) def printQuery(endpoint,prefix,q): ''' Print the results from the SPARQL query ''' results=runQuery(endpoint,prefix,q) for result in results["results"]["bindings"]: for ans in result: print('{0}: {1}'.format(ans,result[ans]['value'])) print() printQuery(endpoint,prefix,q) prefix=''' PREFIX rdf: PREFIX rdfs: PREFIX dcterms: PREFIX aiiso: PREFIX xsd: ''' q=''' SELECT ?code ?title ?desc WHERE { ?module aiiso:code 'AA100'^^xsd:string. ?module rdf:type aiiso:Module. ?module aiiso:code ?code. ?module aiiso:code ?code. ?module rdfs:label ?title. ?module dcterms:description ?desc. } ''' printQuery(endpoint,prefix,q) prefix=''' PREFIX rdf: PREFIX rdfs: PREFIX dcterms: PREFIX aiiso: PREFIX xsd: PREFIX saou: ''' q=''' SELECT ?code ?book WHERE { ?module aiiso:code 'AA100'^^xsd:string. ?module rdf:type aiiso:Module. ?module aiiso:code ?code. ?module aiiso:code ?code. ?module rdfs:label ?title. ?module dcterms:description ?desc. ?module saou:hasBook ?book. } ''' printQuery(endpoint,prefix,q) q='DESCRIBE ' def printDesc(endpoint,q): ans=runQuery(endpoint,prefix,q) print(ans.decode('utf-8')) printDesc(endpoint,q) prefix=''' PREFIX rdf: PREFIX rdfs: PREFIX dcterms: PREFIX aiiso: PREFIX xsd: PREFIX saou: PREFIX bibo: ''' q=''' SELECT ?booktitle ?isbn13 ?publisher WHERE { ?module aiiso:code 'AA100'^^xsd:string. ?module rdf:type aiiso:Module. ?module aiiso:code ?code. ?module aiiso:code ?code. ?module rdfs:label ?title. ?module dcterms:description ?desc. ?module saou:hasBook ?book. ?book dcterms:title ?booktitle. ?book bibo:ISBN13 ?isbn13. ?book dcterms:publisher ?publisher. } ''' printQuery(endpoint,prefix,q)