Query DBpedia using Natural Language
by @kidehen
Transform natural language questions into SPARQL queries for DBpedia and generate beautiful HTML results pages. Query the DBpedia knowledge graph using plain...
clawhub install query-dbpediaπ About This Skill
name: dbpedia-query-skill description: Transform natural language questions into SPARQL queries for DBpedia and generate beautiful HTML results pages. Query the DBpedia knowledge graph using plain English prompts.
DBpedia Query Skill
When to Use This Skill
Use this skill when users want to:
Core Capabilities
β Natural Language to SPARQL: Convert user questions into valid SPARQL queries β Query Execution: Execute queries against DBpedia endpoint β HTML Generation: Create beautiful, interactive HTML result pages β Multiple Output Formats: JSON, Markdown tables, or HTML β Error Handling: Graceful handling of malformed queries or no results
DBpedia Endpoint
SPARQL Endpoint: https://dbpedia.org/sparql
Format: JSON results (format=json)
Method: HTTP GET with URL-encoded query
Common DBpedia Prefixes
PREFIX dbo:
PREFIX dbr:
PREFIX dbp:
PREFIX rdfs:
PREFIX rdf:
PREFIX foaf:
PREFIX dct:
Query Conversion Workflow
When a user provides a natural language prompt:
1. Analyze the Question
2. Map to DBpedia Properties
Common mappings:
dbo:directordbp:date or dbo:releaseDatedbo:budgetdbo:birthPlacedbo:populationTotaldbo:capitaldbo:authordbo:starring3. Construct SPARQL Query
Template:
PREFIX dbo:
PREFIX dbr:
PREFIX rdfs: SELECT DISTINCT ?variable ?label
WHERE {
?variable
4. Execute Query
Use curl to execute against DBpedia:
curl -s -G "https://dbpedia.org/sparql" \
--data-urlencode "query=" \
--data-urlencode "format=json"
5. Generate Output
Options:
Example Query Patterns
Pattern 1: Films by Director
User: "Show me movies directed by Christopher Nolan"SPARQL:
PREFIX dbo:
PREFIX dbr:
PREFIX rdfs: SELECT DISTINCT ?film ?title ?releaseDate
WHERE {
?film dbo:director dbr:Christopher_Nolan ;
a dbo:Film ;
rdfs:label ?title .
OPTIONAL { ?film dbo:releaseDate ?releaseDate }
FILTER(LANG(?title) = 'en')
}
ORDER BY DESC(?releaseDate)
Pattern 2: Population Queries
User: "What are the 10 most populous cities in France?"SPARQL:
PREFIX dbo:
PREFIX dbr:
PREFIX rdfs: SELECT ?city ?name ?population
WHERE {
?city dbo:country dbr:France ;
a dbo:City ;
rdfs:label ?name ;
dbo:populationTotal ?population .
FILTER(LANG(?name) = 'en')
}
ORDER BY DESC(?population)
LIMIT 10
Pattern 3: Person Information
User: "Tell me about Albert Einstein - when was he born and where?"SPARQL:
PREFIX dbo:
PREFIX dbr:
PREFIX rdfs: SELECT ?birthDate ?birthPlace ?placeLabel
WHERE {
dbr:Albert_Einstein dbo:birthDate ?birthDate ;
dbo:birthPlace ?birthPlace .
?birthPlace rdfs:label ?placeLabel .
FILTER(LANG(?placeLabel) = 'en')
}
HTML Template Generation
When generating HTML results:
Required Elements
1. Title: Question or query description 2. Statistics: Number of results, query execution time 3. Table: Results with hyperlinked DBpedia URIs 4. SPARQL Query Display: Show the executed query 5. Footer: Link to DBpedia, data source attributionStyling Guidelines
HTML Template Structure
[Query Description] - DBpedia
[Question/Title]
[Results count]
[Results]
[Query code]
Error Handling
No Results
If query returns 0 results:Query Errors
If SPARQL syntax error:Timeout
If query times out:Output Preferences
Always ask the user:
"Would you like the results as:
1. JSON (raw data)
2. Markdown table (terminal display)
3. HTML page (interactive visualization)"
Best Practices
1. Always use DISTINCT: Avoid duplicate results
2. Filter by language: Use FILTER(LANG(?label) = 'en')
3. Add LIMIT: Default to LIMIT 100 unless specified
4. Use OPTIONAL: For properties that may not exist
5. Order results: Make results meaningful with ORDER BY
6. Hyperlink in HTML: All DBpedia URIs should be clickable
Example Session
User: "List books written by J.K. Rowling with publication dates"
Assistant: "I'll query DBpedia for books authored by J.K. Rowling.
Executing SPARQL query against DBpedia endpoint..."
[Constructs and executes query]
"Found 15 books! Would you like the results as: 1. JSON 2. Markdown table 3. HTML page"
User: "HTML page"
Assistant: [Generates beautiful HTML page with results]
"β HTML page generated and saved to: ./jk_rowling_books.html β 15 books found with publication dates"
Scope
This skill handles:
This skill does NOT handle:
Version: 1.0.0 Endpoint: https://dbpedia.org/sparql Data Source: DBpedia (Wikipedia structured data)
π Tips & Best Practices
1. Always use DISTINCT: Avoid duplicate results
2. Filter by language: Use FILTER(LANG(?label) = 'en')
3. Add LIMIT: Default to LIMIT 100 unless specified
4. Use OPTIONAL: For properties that may not exist
5. Order results: Make results meaningful with ORDER BY
6. Hyperlink in HTML: All DBpedia URIs should be clickable