<jupyter_start><jupyter_text>KEN3140: Lab 5  Writing and executing advanced SPARQL queries on remote RDF graphs (SPARQL endpoints) Authors:+ [<NAME>](https://www.maastrichtuniversity.nl/vincent.emonet): [<EMAIL>](mailto:<EMAIL>)+ [<NAME>](https://www.maastrichtuniversity.nl/kody.moodley): [<EMAIL>](mailto:<EMAIL>) Affiliation: [Institute of Data Science](https://www.maastrichtuniversity.nl/research/institute-data-science) License:[CC-BY 4.0](https://creativecommons.org/licenses/by/4.0) Date:2021-09-06  In this lab you will learn:How to compose more advanced [SPARQL](https://www.w3.org/TR/2013/REC-sparql11-query-20130321/) queries to answer questions about the content of an RDF knowledge graph Specific learning goals:+ How to select the appropriate SPARQL query form to answer a given question about a knowledge graph+ How to select the appropriate SPARQL functions required to perform any calculations in a SPARQL query+ How to refer to entities located in external SPARQL endpoints from a SPARQL query (querying multiple distributed endpoints)  Prerequisite knowledge: + Lecture 4: Introduction to SPARQL+ Lab 4: Introduction to SPARQL+ Lecture 5: Advanced SPARQL+ [SPARQL 1.1 language specification](https://www.w3.org/TR/sparql11-query/)+ Chapters 1 - 3 of [Learning SPARQL](https://maastrichtuniversity.on.worldcat.org/external-search?queryString=SPARQL/oclc/853679890)+ Chapters 4 - 9, 11 of [Learning SPARQL](https://maastrichtuniversity.on.worldcat.org/external-search?queryString=SPARQL/oclc/853679890)  Task information:+ In this lab, we will ask you to query the [DBpedia](https://dbpedia.org/) knowledge graph!+ [DBpedia](https://dbpedia.org/) is a crowd-sourced community effort to extract structured content in RDF from the information created in various [Wikimedia](https://www.wikimedia.org/) projects (e.g. [Wikipedia](https://www.wikipedia.org/)). DBpedia is similar in information content to [Wikidata](https://www.wikidata.org/wiki/Wikidata:Main_Page). + **A word on data quality:** remember that DBpedia is crowd-sourced. This means that volunteers and members of the general public are permitted to add and maintain it's content. As a result, you may encounter inaccuracies / omissions in the content and inconsistencies in how the information is represented. Don't be alarmed by this, it is not critical that the content is accurate for the learning objectives of this lab.  Task information (contd):+ The DBpedia SPARQL endpoint URL is: [https://dbpedia.org/sparql](https://dbpedia.org/sparql)+ DBPedia has it's own SPARQL query interface at [https://dbpedia.org/sparql](https://dbpedia.org/sparql) which is built on OpenLink's [Virtuoso](https://virtuoso.openlinksw.com/) [RDF](https://www.w3.org/TR/rdf11-concepts/) triplestore management system.+ In this lab, we will use an alternative SPARQL query interface to query DBPedia. It is called **[YASGUI](https://yasgui.triply.cc)**. The reason is that YASGUI has additional user-friendly features e.g. management of multiple SPARQL queries in separate tabs. It also allows one to query any publicly available SPARQL endpoint from the same interface.  Tips 🔎+ How do I find vocabulary to use in my SPARQL query from DBpedia?> Search on google, e.g., if you want to know the term for "capital city" in DBpedia, search for: "**[dbpedia capital](https://www.google.com/search?&q=dbpedia+capital)**" In general, "dbpedia [approximate name of predicate or class you are looking for]" > Your search query does not have to exactly match the spelling of the DBpedia resource name you are looking for> Alternatively, you can formulate SPARQL queries to list properties and types in DBpedia Do you know what these queries might look like?+ Use [prefix.cc](http://prefix.cc/) to discover the full IRIs for unknown prefixes you may encounter+ First read the question carefully and determine what **query form** the question requires+ Do not try to formulate the full SPARQL query for each task from the get go. Rather build and test smaller queries first and add to it incrementally, testing each time to see if the results are expected+ Make use of the Lecture 4 & 5 slides and SPARQL specification page if you need to find additional functions and query features that may assist you in doing the tasks  YASGUI interface  <!--  Install the SPARQL kernelThis notebook uses the SPARQL Kernel to define and **execute SPARQL queries in the notebook** codeblocks.To **install the SPARQL Kernel** in your JupyterLab installation:```shellpip install sparqlkernel --userjupyter sparqlkernel install --user```To start running SPARQL query in this notebook, we need to define the **SPARQL kernel parameters**:* 🔗 **URL of the SPARQL endpoint to query*** 🌐 Language of preferred labels* 📜 Log level --><jupyter_code>%endpoint http://dbpedia.org/sparql

# This is optional, it would increase the log level
%log debug

# Uncomment the next line to return label in english and avoid duplicates
# %lang en<jupyter_output><empty_output><jupyter_text>Anatomy of a SPARQL queryAs we saw in Lecture 4, these are the main components of a SPARQL query:  Task 1 [10min]:  Calculate and display the average [Gross Domestic Product (GDP)](https://en.wikipedia.org/wiki/Gross_domestic_product) of each country in DBpedia and aggregate the results by the currency that each country uses. Display the GDP averages from highest at the top of the list, to lowest at the bottom of the list  Task 2 [20min]:  a) List the first 10 countries to have been dissolved (ceased to exist) and display the resulting countries in order from the most recently created, to the least recently created. **Hint:** use the properties **[dbo:dissolutionDate](https://dbpedia.org/ontology/dissolutionDate)** and **[dbo:foundingYear](https://dbpedia.org/ontology/foundingYear)** in your answer. b) Display a set of triples indicating the country of origin of book authors in DBpedia. In the displayed triples:The type of book author instances should be indicated by **[schema:Person](https://schema.org/Person)** from **[Schema.org](http://schema.org)**The country of origin of book authors should be represented by **[schema:countryOfOrigin](https://schema.org/countryOfOrigin)**. **Note:** you can observe that in this CONSTRUCT query, we have basically created new triples using **new vocabulary** about the place of birth (country of origin) for book authors. This is sometimes called "mapping", "transforming" or "capturing" data in "another model". Here "model" refers to a new vocabulary. The old vocabulary used was DBpedia, and the new one is Schema.org.Suppose that I already had a knowledge graph using Schema.org vocabulary. If I wanted to integrate external information from another knowledge graph (e.g. DBpedia), this kind of transformation is helpful for importing those triples into the same "model" (vocabulary) of my knowledge graph. This allows me to query DBpedia information using my own vocabulary (Schema.org) without needing to use DBpedia terms in my queries.Of course, CONSTRUCT will not insert the triples to my graph. If I wanted to do that, I would need INSERT, and I would also need a federated query to access the SPARQL endpoint of my own knowledge graph from the DBpedia knowledge graph.The utility of CONSTRUCT here is to show us what these triples will look like and I can verify the correctness of these triples before I import them, if that is what I wanted to do.  Task 3 [20min]: **Background:** This task will ask you to create a federated SPARQL query to query information from two SPARQL distributed endpoints. In preparation for this task, change the SPARQL endpoint in YASGUI or this notebook (whichever you are using) to: https://bio2rdf.org/sparql/.<jupyter_code>%endpoint http://bio2rdf.org/sparql<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08310888803385917, 'fraction_numerical': 0.010645119917917148, 'mean_word_length': 5.169303797468355, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 19, 'https://': 27, 'lorem ipsum': 0, 'www.': 10, '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': '508369', 'license': 'MIT', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2078, 'n_words': 1005, 'path': 'lab05-sparql-advanced/Lab5_SPARQL_advanced.ipynb', 'repo_name': 'MaastrichtU-IDS/UM_KEN3140_SemanticWeb', 'size': '7931'}