Lab: Wikidata in RDF

From info216
Revision as of 14:18, 21 February 2024 by Sinoa (talk | contribs)

Topics

Wikidata in RDF:

  • retrieve truthy triples about a Wikidata entity
  • load the semantic data and metadata into GraphDB
  • visualise the semantic data and metadata

Motivation: So far you have built your own knowledge graph and worked on a small grap you were given. This week we will look at how to retrieve knowledge graphs from Wikidata, which can then be merged with your own graph to provide additional context. This is not a trivial problem because Wikidata most likely contains a lot more data - and in particular metadata - than you need.

Useful materials

Tasks

Getting ready: In a web browser, go to Wikidata's Query Service (WDQS). Be careful to always use a limit like LIMIT 100 when you test things. Otherwise, you risk being blocked from the query service or, worse, you risk blocking out a whole subdomain.

Emergency data: If Wikdata's Query Service is unavailable, you can load [:File:Q42-extended.txt | this Turtle file] into GraphDB instead, and continue there using Q42 as your example entity. (Remember to rename it from .txt to .ttl.)

Task: From Wikidata's ordinary UI, find the Q-code of one of the people or entities involved in the Mueller investigation. Use that entity as your reference in the rest of this lab. (The Q-code should look like this https://www.wikidata.org/entity/Q42 or wd:Q42.)

Task: Use a DESCRIBE query to retrieve some triples about your entity (remember LIMIT 100, although it is less critical on DESCRIBE queries).

Task: Use a SELECT query to retrieve the first 100 triples about your entity.

Tip: Always save your queries and updates as soon as they succeed. You may need to go back to them later.

Task: Start GraphDB on your local machine. Create a new repository (No inference needed), and activate it. Write a local SELECT query that embeds a <https://query.wikidata.org/bigdata/namespace/wdq/sparql> SERVICE query to retrieve the first 100 triples about your entity to your local machine.

Tip: wd: is a PREFIX for <http://www.wikidata.org/entity/>.

Tip: To make LIMIT work inside a SERVICE query, you have to add another SELECT inside it, like this:

SELECT ... {  # the local query
    SERVICE ... {  # the remote service
        SELECT ... {
            ...
        } LIMIT 100  # this limit works on the remote service
    }
}  # a limit here would work on your local service, 
   # but is not strictly necessary when you alreadu have an inner LIMIT

Task: Change the SELECT query to an INSERT query that adds the Wikidata triples your local repository. Use a local ASK and/or SELECT query to check that the triples have actually been added.

Task: Go back to the Wikidata Query Service (WDQS). (You can run the rest of the lab using a remote SERVICE inside GraphDB, but using WDQS might give you better error messages etc.)

Truthy Wikidata statements use the prefix wd: for resources and wdt: for predicates. Use a FILTER statement to only SELECT truthy triples in this sense.

Tip: These PREFIXes are built into WDQS, but you will need them if you run inside GraphDB:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

Task: You have now lost all triples with literal values. Edit your query (by relaxing the FILTER expression) so it also returns triples where the object has DATATYPE xsd:string.

Task: You still do not have the "fingerprint" triples, i.e., the label, aliases and description of your reference entity. Wikidata uses special properties like rdfs:label, skos:altLabel and schema:description for these. Relax the FILTER expression again so it also returns triples with these three predicates.

Tip: PREFIXes you may need:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX schema: <http://schema.org/>

Task: Now you may have too many fingerprint triples! Try to restrict the FILTER expression again so that, when the predicate is rdfs:label, skos:altLabel and schema:description, the object must have LANG "en".