Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active October 6, 2022 19:48
Show Gist options
  • Save toomasv/201b445e115d1aba7caae2d4943bea7e to your computer and use it in GitHub Desktop.
Save toomasv/201b445e115d1aba7caae2d4943bea7e to your computer and use it in GitHub Desktop.
Tiny Wikidata SPARQL
Red [
Description: {Playing while reading Wikidata SPARQL tutorial}
See: https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial
Also: https://stackoverflow.com/questions/38527828/how-to-query-wikidata-items-using-its-labels
Needs: View
Also-needs: https://github.com/toomasv/table-template
]
#include %table-template.red
ws: charset " ^-^/"
qt: charset {"'}
data: copy items: copy []
rule: [
(clear items) thru <head>
some [
not </head> [
"variable name=" copy _ qt copy item to _ (append items item)
| skip
]
] (append/only data copy items)
thru "<results" thru #">" any ws
some [
<result> (clear items)
some [
not <result> [[
"<literal" thru #">" copy item to </literal>
| "<uri>" to </uri> s: (s: find/reverse/tail s #"/") :s copy item to </uri> ;<uri> thru "entity/" copy item to </uri>
] (append items item)
| skip
]
| (append/only data copy items) fail
]
]
]
view/flags [
title "Wikidata SPARQL"
on-resizing [
btn/offset/x: face/size/x - btn/size/x - 10
end/size/x: btn/offset/x - 130
query/size/x: btn/offset/x - 20
tbl/size: face/size - tbl/offset - as-pair btn/size/x + 3 10
tbl/actors/resize tbl
]
text "SPARQL endpoint:"
end: drop-list 390 data [
"https://query.wikidata.org/sparql?query="
"https://dbpedia.org/sparql?query="
]
return query: area 500x200 wrap focus {select distinct ?Concept where {[] a ?Concept} LIMIT 50}
btn: button "Query" [
clear data
ans: read append to-url copy pick end/data end/selected enhex query/text
parse ans rule
tbl/actors/data: data
;tbl/actors/init tbl
tbl/actors/open-red-table/only tbl [frozen-rows: [1]]
]
return tbl: table 517x317
] 'resize
; Example queries
comment [
; Wikidata: https://query.wikidata.org/sparql?query=
SELECT ?child ?childLabel WHERE {
?child wdt:P22 wd:Q1339.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT ?child ?childLabel ?childDescription WHERE {
?child wdt:P22 wd:Q1339.
?article schema:about ?child .
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
;Slow one
SELECT ?item ?itemLabel
WHERE {
?item rdfs:label ?itemLabel.
FILTER(CONTAINS(LCASE(?itemLabel), "city"@en)).
} limit 10
SELECT distinct ?item ?label ?itemLabel ?itemDescription WHERE{
?item ?label "Something"@en.
?article schema:about ?item .
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT DISTINCT ?item ?label
WHERE
{
SERVICE wikibase:mwapi
{
bd:serviceParam wikibase:endpoint "www.wikidata.org";
wikibase:api "Generator";
mwapi:generator "search";
mwapi:gsrsearch "inlabel:Amsterdam"@en;
mwapi:gsrlimit "max".
?item wikibase:apiOutputItem mwapi:title.
}
?item rdfs:label ?label.
FILTER( LANG(?label)="en" )
# … at this point, you have matching ?item(s)
# and can further restrict or use them
# as in any other SPARQL query
# Example: the following restricts the matches
# to college towns (Q1187811) only
#?item wdt:P31 wd:Q1187811 .
}
;DBpedia: https://dbpedia.org/sparql?query=
select distinct ?Concept where {[] a ?Concept} LIMIT 50
--------------
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?birth ?person WHERE {
?person dbo:birthPlace :Tallinn .
?person dbo:birthDate ?birth .
?person foaf:name ?name .
FILTER (?birth > "1960-01-01"^^xsd:date) .
} ORDER BY ?name LIMIT 100
-------------
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?description_en ?musician
WHERE {
?musician a dbo:MusicalArtist .
?musician foaf:name ?name .
OPTIONAL {
?musician rdfs:comment ?description_en .
FILTER (LANG(?description_en) = 'en') .
}
}
LIMIT 100
--------------
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?birth ?description ?person WHERE {
?person a dbo:MusicalArtist .
#?person dbo:birthPlace :Berlin .
?person dbo:birthDate ?birth .
?person foaf:name ?name .
?person rdfs:comment ?description .
FILTER (LANG(?description) = 'en') .
} ORDER BY ?name LIMIT 50
--------------
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT distinct ?title ?description WHERE {
?game a dbo:Game ;
foaf:name ?title ;
rdfs:comment ?description .
FILTER (LANG(?description) = 'en') .
} ORDER by ?title
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment