#!/usr/bin/env python # coding: utf-8 # ## Basic SPARQLProg example # # This basic example walks through querying the Wikidata SPARQL endpoint. # # We make use of the wikidata module that is part of sparqlprog, and provides access to predicates such as `continent/1` (for instances of continents) and the generic `enlabel/2` (which maps an entity to it's English-language label) # # In[1]: from prologterms import TermGenerator, PrologRenderer, Program, Var from sparqlprog import SPARQLProg P = TermGenerator() # In[2]: server = 'http://localhost:9083' # server = 'https://evening-falls-87315.herokuapp.com' # In[3]: S = SPARQLProg(server=server, endpoint='wd') # ### setting up a query # In[4]: # our query will join over two predicates C = Var('C') N = Var('N') query = (P.continent(C), P.enlabel(C, N)) # same as logic program query: continent(C), enlabel(C,N) # ### running the query # # In[5]: res = S.query(query) for r in res: print(f"{r.C} {r.N}") # In[6]: C = Var('C') D = Var('D') CN = Var('CN') DN = Var('DN') query = ( P.country(D), P.part_of_continent(D,C), P.continent(C), P.enlabel(D,DN), P.enlabel(C,CN) ) res = S.query(query) for r in list(res)[0:8]: print(f"{r.D} {r.DN} part-of {r.C} {r.CN}") # In[7]: df = S.query_to_dataframe(query) # In[8]: df # In[ ]: