<jupyter_start><jupyter_text>An example query<jupyter_code>query = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?label
WHERE { <http://dbpedia.org/resource/Asturias> rdfs:label ?label }
"""
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["label"]["value"])
def search(query):
    # results :: dict
    sparql = SPARQLWrapper("http://dbpedia.org/sparql")
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    return results
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX ontology: <http://dbpedia.org/ontology/>

SELECT distinct ?s  ?author 
WHERE {
?s rdf:type ontology:Book;
  ontology:author ?author .


}
LIMIT 10
"""
results = search(query)
results.keys()
results
results['head']
results['results'].keys()
results['results']['bindings']
def book_info(title='The_Wonderful_Wizard_of_Oz'):
    query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX ontology: <http://dbpedia.org/ontology/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>

SELECT ?book ?title ?author ?date ?country
WHERE {
    ?book rdf:type ontology:Book;
        ontology:author ?author.
    FILTER regex(str(?book), "%s").
    OPTIONAL {
      ?book ontology:releaseDate ?date;
          dbpedia:country ?country .
    }
}
LIMIT 10
""" % title
    return search(query)
book_info()
def book_info_alt(title='The_Wonderful_Wizard_of_Oz'):
    query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX ontology: <http://dbpedia.org/ontology/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>

SELECT ?book ?title ?author ?date ?country
WHERE {
    ?book rdf:type ontology:Book;
        ontology:author ?author .
    FILTER regex(str(?book), "%s") .
    OPTIONAL {
        ?author ontology:birthDate ?date .
        ?book dbpedia:country ?country .
    }
}
LIMIT 10
""" % title
    return search(query)
book_info_alt()<jupyter_output><empty_output><jupyter_text>(unused) Convert json to dictDit doet sparql automatisch<jupyter_code># from https://stackoverflow.com/questions/6578986/how-to-convert-json-data-into-a-python-object

data = '{"name": "<NAME>", "hometown": {"name": "New York", "id": 123}}'

# Parse JSON into an object with attributes corresponding to dict keys.
x = json.loads(data, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
print(x.name, x.hometown.name, x.hometown.id)
def _json_object_hook(d): 
    return namedtuple('X', d.keys())(*d.values())

def json2obj(data): 
    return json.loads(data, object_hook=_json_object_hook)

x = json2obj(data)
x
json2obj(results)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17204648160103292, 'fraction_numerical': 0.023886378308586184, 'mean_word_length': 4.51423487544484, 'pattern_counts': {'":': 4, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 7, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '138166', 'license': 'MIT', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1142, 'n_words': 210, 'path': 'src/dbpedia_.ipynb', 'repo_name': 'voschezang/books', 'size': '3102'}