PassageQueryServiceExamples

From Project Passage
Jump to: navigation, search

Consider adding a comment in the query noting what it illustrates, when and by whom it was written and which are its limitations given the current data and use of properties at Wikidata.


Simple Queries

These basic queries help to understand SPARQL and the Wikibase RDF format.

Timeline of Douglas Adams' books

PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
#defaultView:Timeline
SELECT ?item ?itemLabel ?pubdate 
WHERE {
  ?item passagedt:P13 passagee:Q526648 ;
        passagedt:P17 ?pubdate .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
}
ORDER BY ?pubdate

Try it!

Books authored by librarians

PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
SELECT ?item ?itemLabel ?author ?authorLabel
WHERE {
  ?item passagedt:P13 ?author. 
  ?author passagedt:P94 passagee:Q780843 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 100

Try it!

Translations of Heidegger's Sein und Zeit

# Translations of Heidegger's Sein und Zeit
PREFIX passageps: <http://18.218.102.193/prop/statement/>
PREFIX passagepq: <http://18.218.102.193/prop/qualifier/>
PREFIX passagep: <http://18.218.102.193/prop/>
PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
SELECT ?translation ?translationTitle (GROUP_CONCAT(DISTINCT YEAR(?pubdate);SEPARATOR=", ") AS ?estimatedDate) ?translationLanguageLabel (GROUP_CONCAT(?translatorName;SEPARATOR=" & ") AS ?translators) ?representativeOCN ?fullText
WHERE {
  VALUES ?originalWork {
    passagee:Q1024094
  }
  ?translation passagedt:P117 ?originalWork .
  OPTIONAL {?translation passagedt:P18 ?representativeOCN}
  OPTIONAL {?translation passagedt:P30 ?translationTitle}
  OPTIONAL {?translation passagedt:P17 ?pubdate}
  OPTIONAL {?translation passagedt:P155 ?fullText}
  OPTIONAL {
    ?translation passagedt:P48 ?translator.
    OPTIONAL {
      ?translator passagedt:P192 ?translatorName.
    }
    OPTIONAL {
      ?translator rdfs:label ?translatorName.
      FILTER(LANG(?translatorName)="en")
    }
    OPTIONAL {
      {?translator rdfs:label ?translatorName}
    }
  }
  OPTIONAL {?translation passagedt:P128 ?translationLanguage}
  OPTIONAL {
    ?translation passagedt:P6 ?viafID.
    OPTIONAL {
      ?translation passagep:P6 ?stmt.
      ?stmt passageps:P6 ?viafID; 
            passagepq:P172 ?viafHeading.
    }
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
#  BIND(CONCAT(?viafID, ': ', ?viafHeading) AS ?viafExpressionHeading)
}
GROUP BY ?translation ?translationTitle ?translationLanguageLabel ?representativeOCN ?fullText #?viafExpressionHeading
ORDER BY ?translationLanguageLabel ?translatorLabel

Try it!

Timeline of selected translations of Sein und Zeit

This is a live SPARQL variation of https://www.slideshare.net/oclcr/challenges-of-multilingualism/21

PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
#defaultView:Timeline
SELECT ?item (GROUP_CONCAT(?translatorName;SEPARATOR=" & ") AS ?translators) ?pubdate ?languageLabel ?title
WHERE {
  VALUES ?item {
    passagee:Q1224705
    passagee:Q1224699
    passagee:Q1224595
    passagee:Q1224703
    passagee:Q1224575
  }
  ?item passagedt:P117 passagee:Q1024094 ;
        passagedt:P17 ?pubdate .
  OPTIONAL {?item passagedt:P30 ?title}
  OPTIONAL {?item passagedt:P128 ?language}
  OPTIONAL {
    ?item passagedt:P48 ?translator.
  }
  OPTIONAL {
    ?translator passagedt:P192 ?translatorName.
  }
  OPTIONAL {
    ?translator rdfs:label ?translatorName .
    FILTER(LANG(?translatorName)="en")
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
}
GROUP BY ?item ?pubdate ?languageLabel ?title
ORDER BY ?pubdate

Try it!

Books in the Tempus book series "Battles and campaigns"

# Books in the Tempus series "Battles and campaigns"
PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
PREFIX viaf: <http://viaf.org/viaf/>
PREFIX lcnaf: <http://id.loc.gov/authorities/names/>
PREFIX fast: <http://id.worldcat.org/fast/>
SELECT ?bookSeries ?bookSeriesLabel ?bookSeriesDescription ?book ?bookLabel
WHERE {
  VALUES ?bookSeries {
    passagee:Q1224635
  }
  ?bookSeries passagedt:P5 passagee:Q1005557;
        rdfs:label "Battles and campaigns"@en .
  OPTIONAL {?bookSeries passagedt:P115 ?officialWebsite}
  OPTIONAL {?bookSeries passagedt:P105 ?lcnafID}
  OPTIONAL {?bookSeries passagedt:P6 ?viafID}
  OPTIONAL {?bookSeries passagedt:P7 ?fastID}
  OPTIONAL {?book passagedt:P154 ?bookSeries}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Try it!

Layered maps that depict Koshigaya by various publishers over time

#Layered maps that depict Koshigaya by various map publishers over time
#defaultView:Map
PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
SELECT ?map ?mapLabel ?publisher ?publisherLabel ?pointInTime ?place ?placeLabel ?placeDescription ?placeLocation (?mapLabel AS ?layer)
WHERE {
  VALUES ?selectedPlace {
    passagee:Q1224724
  }
  ?map passagedt:P194 ?selectedPlace;
       passagedt:P194 ?place;
       passagedt:P15 ?publisher.
  OPTIONAL {?map passagedt:P45 ?pointInTime}
  ?place passagedt:P58 ?placeLocation.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Try it!

Timeline of maps that depict Koshigaya by various publishers over time

#Timeline of maps that depict Koshguya by various map publishers over time
#defaultView:Timeline
PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
SELECT DISTINCT ?map ?mapLabel ?publisher ?publisherLabel ?pointInTime #?place ?placeLabel ?placeDescription #?placeLocation (?publisherLabel AS ?layer)
WHERE {
  VALUES ?selectedPlace {
    passagee:Q1224724
  }
  ?map passagedt:P194 ?selectedPlace;
#       passagedt:P194 ?place;
       passagedt:P15 ?publisher.
  OPTIONAL {?map passagedt:P45 ?pointInTime}
#  ?place passagedt:P58 ?placeLocation.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Try it!

Odd outlier of a place depicted on a map

What's up with this map? http://18.218.102.193/entity/Q1224722

#Odd outlier of a place depicted on a map
#defaultView:Map
PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
SELECT ?map ?mapLabel ?publisher ?publisherLabel ?pointInTime ?place ?placeLabel ?placeDescription ?placeLocation (?mapLabel AS ?layer)
WHERE {
  VALUES ?map {
    passagee:Q1224722
  }
  ?map passagedt:P194 ?place;
       passagedt:P15 ?publisher.
  OPTIONAL {?map passagedt:P45 ?pointInTime}
  ?place passagedt:P58 ?placeLocation.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Try it!

Heading selection demo

#Heading selection demo
PREFIX passagepq: <http://18.218.102.193/prop/qualifier/>
PREFIX passageps: <http://18.218.102.193/prop/statement/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
PREFIX passagee: <http://18.218.102.193/entity/>
SELECT DISTINCT ?entity ?entityLabel (GROUP_CONCAT(?officialName;SEPARATOR=", ") AS ?officialNames) ?schemeLabel ?schemeFormatter ?schemeID ?schemePrefLabel
{
#  VALUES ?entity {
#    passagee:Q1224655
#  }
  
  VALUES ?prefProperty {
    passagedt:P105
    passagedt:P7
  }
  
  #OPTIONAL 
  {
    ?stmt ?ps ?schemeID; 
          passagepq:P175 ?schemePrefLabel.
  }
  ?scheme wikibase:directClaim ?prefProperty.
  OPTIONAL {?scheme passagedt:P131 ?schemeFormatter}
  BIND((STRAFTER(STR(?prefProperty), STR(passagedt:))) AS ?propID)
  BIND(URI(CONCAT(STR(passagepq:), ?propID)) AS ?pq)
  BIND(URI(CONCAT(STR(passageps:), ?propID)) AS ?ps)
  
  ?entity ?prefProperty ?schemeID
  OPTIONAL {?entity passagedt:P177 ?officialName}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?entity ?entityLabel ?schemeLabel ?schemeFormatter ?schemeID ?schemePrefLabel
ORDER BY ?entityLabel

Try it!

List of Genres

PREFIX passagedt: <http://18.218.102.193/prop/direct/>
#List of genres
SELECT DISTINCT ?genre ?genreLabel
WHERE {
   ?item passagedt:P63 ?genre.	# genre: *
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Try it!

Bubblechart of genres

#added on 2017-12-18
#Items with the property for Genre
#defaultView:BubbleChart
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagep: <http://18.218.102.193/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?genre ?genreLabel ?lcGenreFormID (COUNT(DISTINCT ?item) AS ?itemCount)
WHERE {
   ?item passagep:P63 ?genre.	# genre: *
   OPTIONAL {?genre passagep:P130 ?lcGenreFormID}
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?genre ?genreLabel ?lcGenreFormID
ORDER BY DESC(?itemCount)

Try it!

Subjects, Predicates, and Objects

#added 2017-12-18
 # Returns a limited set of subject-predicate-object statements
 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
 SELECT * WHERE {
   ?sub ?pred ?obj .
 } 
 LIMIT 10

Try it!

List of Properties

#added 2017-12-18
 # Lists labels for property identifiers
 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
 PREFIX entity: <http://18.218.102.193/entity/>
 PREFIX wct: <http://18.218.102.193/prop/direct/>
 PREFIX wikibase: <http://wikiba.se/ontology#>
 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 SELECT ?p ?enLabel
 WHERE {
   ?prop wikibase:directClaim ?p;
         rdfs:label ?enLabel.
   FILTER(LANG(?enLabel)="en")
 }
 ORDER BY ?enLabel

Try it!

Entities with English language-tagged Labels

#added 2017-12-18
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX entity: <http://18.218.102.193/entity/>
PREFIX wct: <http://18.218.102.193/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?entity ?enLabel
WHERE {
  ?entity rdfs:label ?enLabel;
          a wikibase:Item.
  FILTER(LANG(?enLabel)="en")
}
#ORDER BY ?enLabel
LIMIT 100

Try it!

List of entities of type Person

#added 2017-12-18
#Illustrates filtering by "instance of" a type 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?person ?enLabel
WHERE {
  ?person passagedt:P5 passagee:Q7;	# instance of: person
          rdfs:label ?enLabel.
  FILTER(LANG(?enLabel)="en")
}
LIMIT 100

Try it!

List of Films

#added on 2017-12-18
#Another illustration of "instance of" filtering
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX passagee: <http://18.218.102.193/entity/>
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?film ?enLabel
WHERE {
  ?film passagedt:P5 passagee:Q27;	# instance of: film
          rdfs:label ?enLabel.
  FILTER(LANG(?enLabel)="en")
}

Try it!

Entities without a Wikidata ID

#added 2017-12-18
 #illustrates how SPARQL queries can assist in selecting entities for data quality assessment
 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
 PREFIX passagee: <http://18.218.102.193/entity/>
 PREFIX passagedt: <http://18.218.102.193/prop/direct/>
 PREFIX wikibase: <http://wikiba.se/ontology#>
 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 SELECT ?item ?enLabel
 WHERE {
   ?item a wikibase:Item;
         rdfs:label ?enLabel.
   MINUS {?item passagedt:P8 ?wd}
   FILTER(LANG(?enLabel)="en")
 }
 LIMIT 100

Try it!

Entities without any external equivalents

#added 2017-12-18
 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
 PREFIX passagee: <http://18.218.102.193/entity/>
 PREFIX passagedt: <http://18.218.102.193/prop/direct/>
 PREFIX wikibase: <http://wikiba.se/ontology#>
 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 SELECT ?property ?enLabel
 WHERE {
   ?property a wikibase:Property;
         rdfs:label ?enLabel.
   MINUS {?property passagedt:P27 ?equivalent}
   FILTER(LANG(?enLabel)="en")
 }
 LIMIT 100

Try it!

Most prolific authors

#added 2018-03-12
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
PREFIX passagee: <http://18.218.102.193/entity/>
SELECT ?author ?authorLabel (COUNT(DISTINCT ?item) AS ?itemCount) 
WHERE {
  ?item passagedt:P13 ?author.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?author ?authorLabel
ORDER BY DESC(?itemCount)
LIMIT 100

Try it!

items authored by Stephen King

#added 2018-03-12
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
PREFIX passagee: <http://18.218.102.193/entity/>
SELECT ?item ?itemLabel (GROUP_CONCAT(?typeLabel;SEPARATOR=", ") AS ?itemTypes)
WHERE {
  ?item passagedt:P13 passagee:Q805276;
        passagedt:P5 ?type.
  ?type rdfs:label ?typeLabel.
  FILTER(LANG(?typeLabel)="en")
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?item ?itemLabel
ORDER BY ?itemLabel

Try it!

authors with the most translations

#added 2018-03-12
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
PREFIX passagee: <http://18.218.102.193/entity/>
SELECT ?author ?authorLabel (COUNT(DISTINCT ?translation) AS ?numTranslations)
WHERE {
  {
    ?translation passagedt:P5 passagee:Q1006836
  }
  UNION
  {
    ?translation passagedt:P117 []
  }
  UNION
  {
    ?translation passagedt:P48 []
  }
  ?translation passagedt:P13 ?author.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?author ?authorLabel
ORDER BY DESC(?numTranslations)
LIMIT 100

Try it!

types associated with translation (multi-typed)

#added 2018-03-12
#added 2018-03-12
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
PREFIX passagee: <http://18.218.102.193/entity/>
SELECT DISTINCT ?type ?typeLabel (COUNT(DISTINCT ?item) AS ?numItems)
WHERE {
    ?item passagedt:P5 passagee:Q134;
             passagedt:P5 ?type.
  FILTER(?type != passagee:Q135)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
GROUP BY ?type ?typeLabel 
ORDER BY DESC(?numItems)

Try it!

People born between 1800 and 1880 without a specified death date

#added 2018-05-22
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
SELECT ?h ?enLabel ?date WHERE {
  ?h passagedt:P5 passagee:Q7.
  ?h passagedt:P9 ?date.
  ?h rdfs:label ?enLabel.
  OPTIONAL { ?h passagedt:P10 ?d. }
  FILTER(?date > "1800-01-01T00:00:00Z"^^xsd:dateTime)
  FILTER(?date < "1880-01-01T00:00:00Z"^^xsd:dateTime)
  FILTER(!BOUND(?d))
  FILTER(LANG(?enLabel)="en")
}
LIMIT 100

Try it!

Types and their most useful properties

#added 2018-05-22
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
SELECT DISTINCT ?type ?typeLabel ?typicalProperty ?typicalPropertyLabel 
WHERE {
  ?type passagedt:P158 ?typicalProperty.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?typeLabel ?typicalPropertyLabel

Try it!

Images of ships

#added 2018-05-22
#defaultView:ImageGrid
PREFIX passagedt: <http://18.218.102.193/prop/direct/>
SELECT ?item ?itemLabel ?pic WHERE {
  ?item passagedt:P5 passagee:Q520502.
  ?item passagedt:P3 ?pic.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Try it!

Problematic data

List of translations with varying gaps and inconsistencies

PREFIX passagedt: <http://18.218.102.193/prop/direct/>
SELECT DISTINCT ?item ?itemLabel ?title ?languageLabel ?author ?authorLabel ?translatedFromLabel ?translatorLabel ?typeLabel
WHERE {
  {?item passagedt:P5 passagee:Q134}
  UNION
  {?item passagedt:P48 ?translator}
  UNION
  {?item passagedt:P117 ?translatedFrom }
  ?item passagedt:P13 ?author.
  OPTIONAL {?item passagedt:P5 ?type}
  OPTIONAL {?item passagedt:P30 ?title}
  OPTIONAL {?item passagedt:P128 ?language}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?itemLabel

Try it!

Potential Exercises

List of Items with translator claims, but no translation of claims

List of Pseudonyms and the corresponding people