This course has already ended.

The latest instance of the course can be found at: Semantic Web: 2024 MOOC


Using RDF knowledge in a recommendation application

In this task you will create a program with Python, that can be used to retrieve and save information about books from BookSampo, according to the interests defined in your FOAF profile in the first exercise set.

Two external python libraries will be required for this exercise:

RDFLib and SPARQLWrapper, which can be installed with pip install rdflib sparqlwrapper

To ensure that the tests work correctly, you should use python version 3.0 or higher. If you are using python3 without a virtual environment, replace pip with pip3 when installing.

Implement the following python functions in the following list.

Note that the functions should be all in one file and some tests test the functionality of more than one function (e.g. the first function is used in all tests). Executable code in the submission file outside the functions may break the tests.

  1. Function: graph_from_turtle_file(filename)

    Read your FOAF profile from an RDF file using an RDF library into a graph (RDFLib: Graph.parse). The function should return the parsed graph.

    The function’s input parameter type is str

    (The test assumes that the FOAF file is in the same directory as the python executable, the parameter filename is an ordinary string)

  2. Function: get_interests(graph)

    Create a function that finds the triple (or triples) that have the FOAF property you used to define your interests from a given RDFLib Graph (the automated tests want foaf:topic_interest, use your own property for manual testing). Use the RDF graph operations of the RDF library for this (RDFLib tip: Graph.triples), don’t use SPARQL.

    The function should return the found triples as an iterable (e.g. a list)

    The function’s input parameter type is rdflib.Graph

    You can use the returned graph from the first function for testing

  3. Function: get_related_books_query(interests)

    1. Create a function which returns a SPARQLWrapper object containing a SPARQL query. The query should find books from BookSampo that have keywords (property matching the interest(s) from your FOAF profile. The function takes as input the triples found with get_interests().

      The SPARQL query should return the book uris and labels in variables ?book_uri and ?book_name in json format.

      The test data contains only a few books and topic interest concepts with the following predicates and classes:


      <> (class)

      topic interest concepts:

      skos:Concept (class)

      Using other properties will result in tests’ query results being empty.

      Useful SPARQLWrapper functions: SPARQLWrapper.setQuery, SPARQLWrapper.setReturnFormat, SPARQLWrapper.query, QueryResult.convert

      Note: Do not execute the query with your_wrapper.query() within the function (unless testing manually), tests don’t have access to the Internet and the HTTP request will timeout causing a TimeOutError!

      The function’s input parameter type is list

      You can make a separate function or python script/interpreter and use the topic interest triples returned from the previous function for testing.

    2. Improve the function’s results by expanding the query with the transitive subconcepts of your interest. You can implement this as a federated SPARQL query, in which you retrieve the subconcepts of your interest from KOKO ontology (SPARQL endpoint: by using the hierarchy relations (skos:broader).

      If you don’t get any book recommendations for testing your query, you can change the interest in your FOAF profile file to something that matches some keywords used in Kirjasampo (e.g.

  4. Function: add_to_foaf_profile(graph, foaf_person, book)

    Create a function to add the book recommendations (book’s URIRef) to your FOAF profile programmatically with some suitable property.

    The function’s input parameter types are rdflib.Graph, rdflib.URIRef, rdflib.URIRef

    You can use the previous functions’ results’ for testing.

    RDFLib tip: Graph.add().

  5. Function: save_graph(graph, filename)

    Create a function that saves a given graph so that the updated FOAF profile can be saved into a new RDF file.

    The function’s input parameter types are rdflib.Graph, str

    RDFLib tip: Graph.serialize.

A+ presents the exercise submission form here.

Posting submission...