Human: I have written the following query that filter out Swedish PMs with the string "vilde" in the partyname and then find Wikicommons pictures for those people....
My problem is how to find the same people BUT has no picture i.e. people in Wikidataitems that has no pictures...
My "wikicommons" query:
#title: First/Sec chamber members pictures "vilde" how they are presented in "Porträttbooks"
#defaultView:ImageGrid
SELECT DISTINCT ?file ?wd ?name ?image (CONCAT(?party," ", COALESCE(?timevilde, "")) AS ?vilde) (concat("Book published ",str(year(?booktime))) AS ?bookPublished)
WITH
{ SELECT distinct ?wd ?name ?itemDescription ?party ?timevilde ?startvilde ?endvilde WHERE
{
SERVICE <https://query.wikidata.org/sparql>
{
VALUES ?position { wd:Q81531912 wd:Q33071890 }
?wd wdt:P31 wd:Q5;
wdt:P39 ?position.
?wd rdfs:label ?name. FILTER(lang(?name)="sv")
{
?wd p:P102 ?PartyWD.
?PartyWD ps:P102 ?p
OPTIONAL {?PartyWD pq:P580 ?startvilde}
OPTIONAL {?PartyWD pq:P582 ?endvilde}
BIND (concat(str(year(?startvilde))," - ", str(year(?endvilde))) AS ?timevilde)
?p rdfs:label ?party.
FILTER(LANG(?party) ="sv").
FILTER(CONTAINS(?party, 'vilde'))
SERVICE wikibase:label { bd:serviceParam wikibase:language "sv,en". }
#FILTER (?wd = wd:Q5555629)
}
}
}
} AS %Wikidataitems
WHERE
{ INCLUDE %Wikidataitems .
?file wdt:P180 ?wd.
VALUES ?booksP1433 {
wd:Q116445396 # 1894
wd:Q110380539 # 1897
wd:Q110380456 # 1900
wd:Q110375618 # 1903
wd:Q110376088 # 1906
wd:Q116313186 # 1909
}
SERVICE <https://query.wikidata.org/sparql>
{
?booksP1433 wdt:P585 ?booktime
}
FILTER (!BOUND(?startvilde) || ?startvilde <= ?booktime)
#FILTER (?startvilde <= ?booktime)
FILTER (!BOUND(?endvilde) || ?endvilde >= ?booktime)
#FILTER (?endvilde >= ?booktime)
?file wdt:P1433 ?booksP1433.
?file schema:contentUrl ?url.
bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?image)
}
order by ?name ?startvilde
ChatGPT:
The key to find people who don't have pictures in Wikidata is using the OPTIONAL
clause for the picture property (P18), then filter out the ones where the picture is not bound. Here's how you could modify your query:
SPARQL#title: First/Sec chamber members NO pictures "vilde" #defaultView:Table SELECT DISTINCT ?wd ?name ?vilde WITH { SELECT distinct ?wd ?name ?itemDescription ?party ?timevilde ?startvilde ?endvilde WHERE { SERVICE <https://query.wikidata.org/sparql> { VALUES ?position { wd:Q81531912 wd:Q33071890 } ?wd wdt:P31 wd:Q5; wdt:P39 ?position. ?wd rdfs:label ?name. FILTER(lang(?name)="sv") { ?wd p:P102 ?PartyWD. ?PartyWD ps:P102 ?p OPTIONAL {?PartyWD pq:P580 ?startvilde} OPTIONAL {?PartyWD pq:P582 ?endvilde} BIND (concat(str(year(?startvilde))," - ", str(year(?endvilde))) AS ?timevilde) ?p rdfs:label ?party. FILTER(LANG(?party) ="sv"). FILTER(CONTAINS(?party, 'vilde')) SERVICE wikibase:label { bd:serviceParam wikibase:language "sv,en". } } } } } AS %Wikidataitems WHERE { INCLUDE %Wikidataitems . OPTIONAL { ?wd wdt:P18 ?image } FILTER (!BOUND(?image)) } order by ?name ?startvilde
This query should return Swedish PMs with "vilde" in the party name, who don't have a picture associated with them in Wikidata. If you need more help, let me know!
Add a comment