This notebook demonstrates basic SPARQL queries against the ClimateKG Wikibase instance to count how many items belong to each major class in the corpus hierarchy.
PREFIX wd:
PREFIX wdt:
SELECT (COUNT(?item) AS ?count) WHERE {
?item wdt:P1 wd:Q3998 .
}
Summary Table
Here’s a summary of all the class counts:
Show code
# Collect all resultsall_results = [result_q2, result_q3, result_q4, result_q5, result_q1, result_q2087, result_q3998]# Create summary DataFramesummary_data = []for result in all_results:if'error'notin result: summary_data.append({'Class': result['class_qid'],'Name': result['class_name'],'Count': result['count'] })summary_df = pd.DataFrame(summary_data, columns=['Class', 'Name', 'Count'])# Display as formatted tabledisplay(Markdown("### Instance Counts by Class"))ifnot summary_df.empty: display(summary_df.style.format({'Count': '{:,}'}).set_properties(**{'text-align': 'left','font-size': '1.1em' }))print(f"\n✓ Total items counted: {summary_df['Count'].sum():,}")else: print("No results available — run the query cells above first.")
Instance Counts by Class
Class
Name
Count
0
Q2
Work
0
1
Q3
Report Series
1
2
Q4
Report
7
3
Q5
Text Division
10
4
Q1
Category
1,001
5
Q2087
Acronym
1,910
6
Q3998
Author
932
✓ Total items counted: 3,861
Understanding the Queries
SPARQL Query Structure
All the queries above follow the same basic pattern:
PREFIX wd: <https://climatekg.tibwiki.io/entity/>
PREFIX wdt: <https://climatekg.tibwiki.io/prop/direct/>
SELECT (COUNT(?item) AS ?count) WHERE {
?item wdt:P1 wd:QX .
}
Explanation: - PREFIX wd: - Defines the namespace for entities (classes and items) - PREFIX wdt: - Defines the namespace for direct properties - ?item wdt:P1 wd:QX - Finds all items (?item) that have property P1 (instance of) pointing to class QX - COUNT(?item) - Counts the number of matching items
Key Property
P1: “instance of” — This property links items to their class in ClimateKG