Tutorial/nl: Difference between revisions

From Resistance in Belgium
(Created page with "<ol> <li><code>SELECT ?naam ?voornaam</code> :<br> Geeft aan dat we de waarden van de variabelen <code>?naam</code> en <code>?voornaam</code> willen weergeven in de resultaten.</li>")
(Created page with "<li><code>WHERE { ... }</code> :<br> Geeft de structuur en voorwaarden van de query: <ul><ul> <li><code>?persoon wdt:P1 wd:Q2</code> : selecteert elementen die menselijke wezens zijn (P1 = type item, Q2 = persoon).</li> <li><code>wdt:P3 ?naam</code> : koppelt de familienaam aan de variabele <code>?naam</code> (P3).</li> <li><code>wdt:P2 ?voornaam</code> : koppelt de voornaam aan de variabele <code>?voornaam</code> (P2).</li> </ul></ul> </li> </ol>")
Line 60: Line 60:
Geeft aan dat we de waarden van de variabelen <code>?naam</code> en <code>?voornaam</code> willen weergeven in de resultaten.</li>
Geeft aan dat we de waarden van de variabelen <code>?naam</code> en <code>?voornaam</code> willen weergeven in de resultaten.</li>


<div lang="en" dir="ltr" class="mw-content-ltr">
<li><code>WHERE { ... }</code> :<br>
<li><code>WHERE { ... }</code>:<br>
Geeft de structuur en voorwaarden van de query:
Describes the structure and conditions of the query:
<ul><ul>
<ul>
<li><code>?persoon wdt:P1 wd:Q2</code> : selecteert elementen die menselijke wezens zijn (P1 = type item, Q2 = persoon).</li>
<li><code>?person wdt:P1 wd:Q2</code>: selects items that are human beings (P1 = item type, Q2 = person).</li>
<li><code>wdt:P3 ?naam</code> : koppelt de familienaam aan de variabele <code>?naam</code> (P3).</li>
<li><code>wdt:P3 ?lastName</code>: associates the last name with the variable <code>?lastName</code> (P3).</li>
<li><code>wdt:P2 ?voornaam</code> : koppelt de voornaam aan de variabele <code>?voornaam</code> (P2).</li>
<li><code>wdt:P2 ?firstName</code>: associates the first name with the variable <code>?firstName</code> (P2).</li>
</ul></ul>
</ul>
</li>
</li>
</ol>
</ol>
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">

Revision as of 09:06, 2 April 2025

1/ Inleiding tot SPARQL

SPARQL gebruiken is alsof je een onderzoeker bent in een gigantisch archief. In plaats van elk document één voor één door te nemen, beschik je over een taal waarmee je met één gerichte zoekopdracht onmiddellijk de informatie kunt vinden die aan je criteria voldoet.

Binnen het project Resistance in Belgium maakt SPARQL het eenvoudiger om toegang te krijgen tot gegevens over verzetsleden: geboorteplaats, woonplaats, officiële erkenningsstatussen of lidmaatschap van een verzetsorganisatie.

Met SPARQL kun je:

  • Informatie filteren en combineren;
  • Je zoekopdrachten personaliseren met specifieke criteria;
  • Precieze gegevens ophalen zonder duizenden fiches handmatig te doorlopen;
  • Visualisaties genereren om je resultaten beter te interpreteren.

Deze aanpak geeft je waardevolle autonomie bij het analyseren van de gegevens uit Resistance in Belgium. Je kunt niet alleen namenlijsten opvragen, maar ook complexe verbanden ontdekken, zoals leden die betrokken waren bij meerdere organisaties of verschillende erkenningsstatussen kregen.

SPARQL geeft je de superkracht waar elke onderzoeker van droomt: stel een ultra-precieze vraag en zie het antwoord binnen milliseconden verschijnen. Natuurlijk vraagt dat enige oefening om onder de knie te krijgen. Geen zorgen! Deze handleiding is er om je te begeleiden en obstakels om te zetten in kansen… Wie zegt dat archiefonderzoek saai moet zijn?

Klaar om de kracht van SPARQL te beheersen? Aan de slag!

2/ Strategie voor je eerste zoekopdrachten

Nu je een algemeen idee hebt van wat SPARQL is, volgt hier een strategie om je onderzoek goed te starten.

Er bestaan hulpmiddelen om SPARQL-query’s op te stellen, maar om meer autonomie en flexibiliteit te verwerven, is het vaak effectiever om te vertrekken van een bestaande query en die aan te passen aan je specifieke noden.

Deze aanpak stelt je in staat om:

Deze handleiding zal je begeleiden bij het begrijpen van de basisprincipes van SPARQL en je leren hoe je de essentiële elementen kunt herkennen om een bestaande query succesvol aan te passen.

3/ De basis van een SPARQL-query

Een SPARQL-query bestaat uit enkele essentiële onderdelen. Hier zijn de twee belangrijkste:

  • SELECT: met deze clausule kies je welke informatie wordt weergegeven in de resultaten.
  • WHERE: met deze clausule kun je de gegevens filteren door specifieke criteria te definiëren. Hier beschrijf je aan welke voorwaarden de gegevens moeten voldoen.

📝 Eenvoudig voorbeeld

Hier is een query die de voor- en achternaam van personen in de databank ophaalt. Regels die beginnen met # zijn commentaarregels om elk onderdeel van de query uit te leggen.

SELECT ?naam ?voornaam
WHERE {
  ?persoon wdt:P1 wd:Q2 ;      # Selecteert personen
            wdt:P3 ?naam ;     # Haalt de achternaam op
            wdt:P2 ?voornaam . # Haalt de voornaam op
}

Uitleg bij het voorbeeld:

  1. SELECT ?naam ?voornaam :
    Geeft aan dat we de waarden van de variabelen ?naam en ?voornaam willen weergeven in de resultaten.
  2. WHERE { ... } :
    Geeft de structuur en voorwaarden van de query:
      • ?persoon wdt:P1 wd:Q2 : selecteert elementen die menselijke wezens zijn (P1 = type item, Q2 = persoon).
      • wdt:P3 ?naam : koppelt de familienaam aan de variabele ?naam (P3).
      • wdt:P2 ?voornaam : koppelt de voornaam aan de variabele ?voornaam (P2).

You now know the basics of all SPARQL queries. You’ll be able to adapt them to your needs by easily selecting the variables to use and by adding filters, additional columns, or advanced options.

4. The Trick That Makes All the Difference

When writing a SPARQL query, it can be tedious to remember numeric identifiers for municipalities, recognition statuses, or resistance organizations. Fortunately, an auto-completion feature is available to simplify the process.

  • Pro tip: Press Ctrl + Space while typing a property or a value. This allows you to search for terms in your language instead of relying on numeric identifiers.

Example:
If you're searching for a municipality like Anderlecht, instead of memorizing its identifier (e.g. wd:Q102), press Ctrl + Space and start typing "Anderlecht" — the editor will automatically suggest the correct identifier.

This method helps you save time and avoid errors when entering identifiers.

SPARQL autocomplete animation
Auto-completion in action — Press Ctrl + Space to find the right property or value.
5. Displaying Labels in Your Preferred Language

When querying a Wikibase dataset with SPARQL, the results may sometimes display identifiers (e.g. Q102) instead of explicit text (e.g. Anderlecht). To make the results more readable, you can retrieve the labels (i.e., names) in the language of your choice (French, Dutch, German, or English). Simply use SERVICE wikibase:label at the end of your query to display values in your preferred language.

How to do it?
Add the wikibase:label service to your query. Here is a simple example that retrieves people’s names along with their place of residence, with labels displayed in French:

SELECT ?person ?personLabel ?residence ?residenceLabel
WHERE {
  ?person wdt:P1 wd:Q2 .              # Select human entities
  ?person wdt:P7 wd:Q102 .            # Residence (P7) = Anderlecht (Q102)
  SERVICE wikibase:label {            # Service to retrieve labels
    bd:serviceParam wikibase:language "fr" . # Language preference: French
  }
}

Quick explanation

  • SERVICE wikibase:label: activates the label retrieval service.
  • bd:serviceParam wikibase:language "fr": sets the label language (replace "fr" with "en", "nl", "de", etc. as needed).

Using AUTO_LANGUAGE
To automatically show labels in the language set by your browser or user preferences, use AUTO_LANGUAGE:

bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr,en".

Tips

  • To automatically display labels in your browser or user’s preferred language, use: bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr,en"
  • You can also prioritize multiple languages. For example, to display labels in French and fall back to English if unavailable: bd:serviceParam wikibase:language "fr,en,[AUTO_LANGUAGE]"
  • 6. Adding Additional Information

    To enrich your results, you can add multiple columns containing additional information, such as residence, date of birth, or other relevant details. To get inspired: check out the list of properties or see how data is modeled using an example, like Andrée De Jongh.

    Adding simple columns
    Just include the properties that correspond to the information you want to extract.

    Example: Add the residence and date of birth of individuals.

    SELECT ?person ?personLabel ?residenceLabel ?birthDate
    WHERE {
      ?person wdt:P7 ?residence .        # P7: Residence
      ?person wdt:P67 ?birthDate .       # P67: Date of birth
    </div>
    
      <div lang="en" dir="ltr" class="mw-content-ltr">
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr" }
    }
    

    Making a column optional with OPTIONAL
    If some information might be missing, use OPTIONAL to avoid limiting the results. Adding OPTIONAL lets you retrieve results even if a column is empty.

    Example: Add date of birth only if the information is available.

    SELECT ?person ?personLabel ?residenceLabel ?birthDate
    WHERE {
      ?person wdt:P7 ?residence .        # P7: Residence
    </div>
    
      <div lang="en" dir="ltr" class="mw-content-ltr">
    OPTIONAL { ?person wdt:P67 ?birthDate . }  # P67: Optional date of birth
    </div>
    
      <div lang="en" dir="ltr" class="mw-content-ltr">
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr" }
    }
    

    Tip: To combine multiple values into a single column (for example, to “concatenate” membership in multiple resistance organizations), use CONCAT. This allows grouping the data into a single cell while keeping one result row per person. To explore this function and its variants, refer to the SPARQL documentation available online.

    7. Limiting the Number of Results

    To reduce the number of results displayed (especially if you’re testing a query with many columns), add the LIMIT clause to your query.

    Example: Show only the first 10 results.

    SELECT ?person ?personLabel ?birthDate
    WHERE {
      ?person wdt:P67 ?birthDate .
    </div>
    
      <div lang="en" dir="ltr" class="mw-content-ltr">
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr"
      }
    }
    LIMIT 10
    
    8. Sorting the Results

    To sort results by a specific column, use the ORDER BY clause. By default, the sorting is ascending (ASC), but you can also specify descending order using DESC.

    Example: Sort results by birth date in ascending order.

    SELECT ?person ?personLabel ?birthDate
    WHERE {
      ?person wdt:P67 ?birthDate .
    </div>
    
      <div lang="en" dir="ltr" class="mw-content-ltr">
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr"
      }
    }
    ORDER BY ASC(?birthDate)
    
    9. Adding Filters

    Filters allow you to refine your results by adding specific criteria to your query. They are useful for combining multiple conditions or excluding certain values.

    📝 Simple example without filter

    To select individuals matching a basic criterion—like “residing in Anderlecht”—you can write:

    ?person wdt:P4 wd:Q102 .  # Selects individuals residing in Anderlecht
    

    Using FILTER for specific conditions
    If you want to narrow your results even more, use FILTER. This allows you to combine multiple conditions. For example: select individuals living in Anderlecht and born after January 1st, 1920 (Link to test the query).

    SELECT ?person ?personLabel ?residenceLabel ?birthDate
    WHERE {
      # Select residence and birth date of the person
      ?person wdt:P7 ?residence .        # P7: Residence
      ?person wdt:P67 ?birthDate .       # P67: Date of birth
    </div>
    
      <div lang="en" dir="ltr" class="mw-content-ltr">
    # Filter for people living in Anderlecht (wd:Q102) and born after Jan 1, 1920
      FILTER(?residence = wd:Q102 && ?birthDate >= "1920-01-01T00:00:00Z"^^xsd:dateTime)
    </div>
    
      <div lang="en" dir="ltr" class="mw-content-ltr">
    # Use the label service for readable output
      SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr"
      }
    }
    

    Advanced usage: It’s also possible to use FILTER NOT EXISTS for more complex exclusions. This allows you to filter out results that meet a specific condition. You can explore this functionality further in advanced SPARQL resources.

    10. Visualizing Your Results

    To create charts from your SPARQL queries, you have two options:

    • Use the features of the Query Service: visualize the data in the form of tables, maps, bar charts, and timelines. For more details, see the Wikidata documentation.
    • Export the data and use other tools: you can export your results in CSV or TSV format, and then process them with data visualization tools.
    11. Federated Queries

    Advanced feature: If you want to query multiple databases at once to enrich your results, you can use federated queries. For an example, see this sample query, which extracts information from Resistance in Belgium and combines it with data from external sources (e.g., Wikidata).

    12. Saving and Sharing Your Queries

    After running a SPARQL query, you can save and share its link. Click the “Link” tab above the results to generate a Tiny URL (a short link) that’s easy to copy and share. This allows others to run the same query directly, with no extra effort.

    13. Downloading the Results

    After executing a query, use the “Download” tab (located on the right above the results) to export the data in different formats: CSV (compatible with LibreOffice Calc or Excel), TSV, or JSON.

    Best practice: Save the file with a clear name that reflects your search criteria to make it easier to reuse later.

    14. Additional Resources

    To deepen your knowledge and create more advanced queries, here are some useful resources:

  • Example profile of an individual (e.g. Andrée De Jongh):
    https://data.arch.be/wiki/Item:Q6793
  • Sample of nine SPARQL queries organized by complexity level:
    https://data.arch.be/wiki/In-depth_search/fr
  • FAQ to understand the context and content of the available data:
    https://data.arch.be/wiki/FAQ/en
  • External Resources
    We also encourage you to consult external resources, such as Wikidata tutorials, which offer excellent introductions to SPARQL queries. Although these examples use the same software (Wikibase) as this project, note that the data and query structure are specific to Wikidata and do not apply directly to the content of Resistance in Belgium.