Patterns in modeling and querying a knowledge graph for literary history

Here you can find the queries that are shown in the paper “Patterns in modeling and querying a knowledge graph for literary history” (in print).

Query 0, Fig 3: Authors (here: ‘Voltaire’) and works (here: ‘Le Micromégas’) in subject position shown as Graph

PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/>

#defaultView:Graph
SELECT ?item1 ?item1Label ?item2 ?item2Label ?edgeLabel 
WITH {
  SELECT ?item1 WHERE {
    VALUES ?item1 { mmd:Q1011 mmd:Q981 mmd:Q2 mmd:Q3039 mmd:Q38 mmd:Q3126 mmd:Q3335 mmd:Q3259 mmd:Q3906}
  }
} AS %item1
WITH {
  SELECT (?item1 AS ?item2) WHERE {
    INCLUDE %item1.
  }
} AS %item2
WHERE {
  INCLUDE %item1.
  INCLUDE %item2.
  ?item1 ?wdt ?item2.
  ?edge wikibase:directClaim ?wdt;
        a wikibase:Property.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Query 1: Count of novels, authors, thematic concepts and spatial concepts

PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIx mmdt:<http://data.mimotext.uni-trier.de/prop/direct/>
SELECT ?subject (COUNT(?o) as ?counts)
WHERE {
  VALUES (?p ?o) { (mmdt:P2 mmd:Q2) (mmdt:P11 mmd:Q11) (mmdt:P2 mmd:Q20) (mmdt:P2 mmd:Q26) }
  ?item ?p ?o.
  BIND(IF(?o = mmd:Q2, "novels", 
          IF(?o = mmd:Q11, "authors",
             IF(?o = mmd:Q20, "thematic concepts",
               IF(?o = mmd:Q26, "spatial concepts", "none")
           )
          )
         ) as ?subject)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}GROUP BY ?o ?subject

3.1 Basic triple patterns and their combinations

Query 2: Narrative locations of literary works

#title: Narrative locations of literary works
PREFIX mmd: <http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt: <http://data.mimotext.uni-trier.de/prop/direct/>

SELECT ?item ?itemLabel ?narrativeLoc ?narrativeLocLabel
WHERE {
  ?item mmdt:P2 mmd:Q2; #item is instance of literary work
        mmdt:P32 ?narrativeLoc. #item has narrative location(s)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  }

Query 3: Novels set in ‘imaginary place’

#title: Novels set in 'imaginary place'
PREFIX mmd: <http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt: <http://data.mimotext.uni-trier.de/prop/direct/>

SELECT DISTINCT ?item ?itemLabel
WHERE {
  ?item mmdt:P2 mmd:Q2; #item is instance of literary work
        mmdt:P32 mmd:Q3371. # item has narrative location 'imaginary place'
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }  

}

Query 4: Novels that have theme ‘miracle’

#title:Novels that have theme 'miracle'
PREFIX mmd: <http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt: <http://data.mimotext.uni-trier.de/prop/direct/>

SELECT DISTINCT ?item ?itemLabel
WHERE {
  ?item mmdt:P2 mmd:Q2; #item is instance of literary work
        mmdt:P36 mmd:Q2990. # item is about miracle
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }  

}

Query 5: Novels published in Paris between 1780 and 1790 that have theme ‘philosophy’

#title:Novels published in Paris between 1780 and 1790 that have theme 'philosophy' 
PREFIX mmd: <http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt: <http://data.mimotext.uni-trier.de/prop/direct/>

SELECT ?item ?itemLabel ?year
WHERE {
  ?item mmdt:P2 mmd:Q2; #item is instance of literary work
        mmdt:P10 mmd:Q3521; #item was published in Paris
        mmdt:P36 mmd:Q3039; #item is about philosophy
        mmdt:P9 ?date. #item has publication date.

  FILTER(?date >= "1780"^^xsd:dateTime && ?date <= "1790"^^xsd:dateTime).
  BIND(YEAR(?date) as ?year).

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }  

}

3.2 Further analysis and exploration

Query 6: Query to retrieve some data about the MiMoTextBase such as Authors, Novels, publicationyears, tonality etc.

#title:Query to retrieve some data about the MiMoTextBase such as Authors, Novels, publicationyears, tonality etc.
PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/>

SELECT ?bgrf ?item ?itemLabel ?authorLabel ?year ?narrpers ?tonality ?pages ?dist_format WHERE {
  ?item mmdt:P2 mmd:Q2. #item is instance of literary work
  OPTIONAL { ?item mmdt:P5 ?author} #item has author (optionally)
  OPTIONAL { ?item mmdt:P4 ?title} #item has title (optionally)
  OPTIONAL { ?item mmdt:P22 ?bgrf}  #item has identifier in the bibliographic metadata? (optionally)
  OPTIONAL { ?item mmdt:P9 ?date} #item has publication date (optionally)
  OPTIONAL { ?item mmdt:P27 ?narrpers} #item has narrative form (optionally)
  OPTIONAL { ?item mmdt:P31 ?tonality} #item has tonality (optionally)
  OPTIONAL { ?item mmdt:P25 ?pages} #item has page information (optionally)
  OPTIONAL { ?item mmdt:P26 ?dist_format} #item has distribution format (optionally)
  BIND(YEAR(?date) as ?year)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE], fr". }
} ORDER BY ?year

Query 7, Fig. 5: Themes occuring in novels of intention satire

#title:Themes occuring in novels of intention satire
#defaultView:BubbleChart
PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/>
SELECT ?theme (SAMPLE(?themeLabel) as ?name)  (count(*) as ?count)
WHERE {
  ?item mmdt:P2 mmd:Q2; # item is instance of literary work
        mmdt:P39 mmd:Q3906; #novel has intention satire
        mmdt:P36 ?theme. #novel has theme
      ?theme rdfs:label ?themeLabel .
      FILTER (LANG(?themeLabel) = "en") .
}
GROUP BY ?theme
ORDER BY DESC(?count)

Query 8: Ratio of the theme ‘nature’ over time

#defaultView:BarChart
#title:Ratio of the theme 'nature' over time
#count of 'nature' (countNature) as a novel theme in relation to the number of novels published per year (countAll); ratio, as novel production is rising strongly
PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/>
SELECT ?date (?countNature / ?countAll AS ?rel)  ?countNature ?countAll  WHERE {
  {
	SELECT ?date (COUNT(*) AS ?countNature) WHERE {
  	?item mmdt:P9 ?date;
          mmdt:P36 mmd:Q3005.
    BIND(YEAR(?date) as ?year)
	}
	GROUP BY ?date
  }
  {
	SELECT ?date (COUNT(*) AS ?countAll) WHERE {
  	?item mmdt:P9 ?date;
    	  mmdt:P36 ?topic.
	}
	GROUP BY ?date
  }
}

Query 9: Thematic concepts of novels referenced by ‘Topic Modeling’

#title:Thematic concepts of novels referenced by ‘Topic Modeling’
#defaultView:BubbleChart
PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/>
PREFIX mmpr:<http://data.mimotext.uni-trier.de/prop/reference/>
PREFIX mmps:<http://data.mimotext.uni-trier.de/prop/statement/>
PREFIX prov: <http://www.w3.org/ns/prov#>

SELECT ?themeLabel (COUNT(*) AS ?count) WHERE {
  ?statement mmps:P36 ?theme;
	prov:wasDerivedFrom ?refnode.
  ?refnode mmpr:P18 mmd:Q21.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?themeLabel

Query 10, Fig. 6: Thematic statements referenced by ‘topic modeling’ and the bibliography

#title:Thematic statements derived from ‘topic modeling’ and the bibliography
#Statements on consistent topics in novels from both sources: Topic Modeling & bibliographic metadata
PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/> 
PREFIX mmps:<http://data.mimotext.uni-trier.de/prop/statement/>
PREFIX mmpr: <http://data.mimotext.uni-trier.de/prop/reference/>
PREFIX mmp: <http://data.mimotext.uni-trier.de/prop/>

SELECT ?novel ?novelLabel ?topicLabel ?BGRF_plot_theme WHERE
{   
    ?novel mmp:P36 ?statement.
    ?statement mmps:P36 ?topic.
    ?statement prov:wasDerivedFrom/mmpr:P18 mmd:Q21. #reference statement uses 'P18'='stated in' topic modeling
    ?statement prov:wasDerivedFrom/mmpr:P18 mmd:Q1. #reference statement uses 'P18'='stated in' bibliographie  
    ?novel mmdt:P30 ?BGRF_plot_theme. #statement in the BGRF about the plot theme.
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Query 11: Overview over controlled vocabularies

#title:Overview over controlled vocabularies
# Query to retrieve the different controlled vocabularies
PREFIX mmd: <http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt: <http://data.mimotext.uni-trier.de/prop/direct/>

SELECT ?voc ?vocLabel (COUNT(?vocLabel) as ?count) (COUNT(?wikimatch) as ?wikimatch)
WHERE {
   ?item mmdt:P37 ?voc.
   ?voc mmdt:P2/mmdt:P1 mmd:Q17.
   OPTIONAL{
     values ?wiki {mmdt:P13 mmdt:P16}
     ?item ?wiki ?wikimatch}
   SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  }
GROUP BY ?voc ?vocLabel
ORDER BY DESC(?count)
  

Query 12: Combinations of narrative Form and intention per year

# get combinations of narrative Form and intention per year
#defaultView:BarChart
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/> 

SELECT ?year (COUNT(*) as ?count) ?narrFormTone
  WHERE{
    ?item mmdt:P9 ?publicationdate. # get publication date
    ?item mmdt:P38 ?tone. # get intention
    BIND(str(YEAR(?publicationdate)) as ?year). # extract year from pubdate
    ?item mmdt:P33 ?narrForm. # get narrative form   
    ?tone rdfs:label ?toneLabel.
    FILTER(LANG(?toneLabel) = "en").
    ?narrForm rdfs:label ?narrFormLabel.
    FILTER(LANG(?narrFormLabel) = "en"). 
    BIND(CONCAT(STR(?narrFormLabel), " -- " , STR(?toneLabel)) as ?narrFormTone).
  }
GROUP BY ?year ?narrFormTone
HAVING (?count > 1)
ORDER BY ?year

Query 13: Topic labels in English, French and German using rdfs:label and Filter

#title:Topic labels in English, French and German
PREFIX mmdt: <http://data.mimotext.uni-trier.de/prop/direct/>
PREFIX mmd: <http://data.mimotext.uni-trier.de/entity/>

SELECT DISTINCT ?topic ?topicLabel_EN ?topicLabel_FR ?topicLabel_DE WHERE {
  
  ?item mmdt:P2 mmd:Q2;
        mmdt:P36 ?topic.
 
  ?topic rdfs:label ?topicLabel_EN.
  ?topic rdfs:label ?topicLabel_FR.
  ?topic rdfs:label ?topicLabel_DE.
  
  FILTER(LANG(?topicLabel_EN) = "en").
  FILTER(LANG(?topicLabel_FR) = "fr").
  FILTER(LANG(?topicLabel_DE) = "de").

}

Query 14: Topic labels in English, French and German using Wikibase Label Service

#title: Topic labels in English, French and German
PREFIX mmd: <http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt: <http://data.mimotext.uni-trier.de/prop/direct/>

SELECT DISTINCT ?topic ?topicLabel_EN ?topicLabel_FR ?topicLabel_DE
   WHERE {
   ?item mmdt:P2 mmd:Q2; 
         mmdt:P36 ?topic.  
     
     SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
            ?topic rdfs:label ?topicLabel_EN.
     }
     SERVICE wikibase:label { bd:serviceParam wikibase:language "de".
            ?topic rdfs:label ?topicLabel_DE.
     } 
     SERVICE wikibase:label { bd:serviceParam wikibase:language "fr".
            ?topic rdfs:label ?topicLabel_FR.
     } 
   
   }

Query 15: Labels entered on wikidata-entry ‘Voltaire’

#title:Labels entered on wikidata-entry 'Voltaire'
PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/>

SELECT (LANG(?VoltaireLabels) as ?lang) ?VoltaireLabels 
WHERE { 
  
  mmd:Q981 mmdt:P13 ?wikiDataEntity. # Voltaire has a wikidata match.

  #Federated Query -> Wikidata
  SERVICE <https://query.wikidata.org/sparql> {
  ?wikiDataEntity rdfs:label ?VoltaireLabels.
  }      	 
}
ORDER BY ?lang

3.3 Federated Queries

Query 16, Fig. 9: Narrative location with geo coordinate locations via federated query

#title:Narrative locations of the novels on a map
#defaultView:Map{"markercluster":"true"}
PREFIX wd: <http://www.wikidata.org/entity/> #wikidata wd
PREFIX wdt: <http://www.wikidata.org/prop/direct/> #wikidata wdt
PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/>
SELECT DISTINCT ?item ?itemLabel ?narr_loc ?narr_locLabel ?WikiDataEntity ?coordinateLocation
WHERE { ?item mmdt:P32 ?narr_loc.
  ?narr_loc mmdt:P13 ?WikiDataEntity.
  #Federated Query -> Wikidata
  SERVICE <https://query.wikidata.org/sparql> {
	?WikiDataEntity wdt:P625 ?coordinateLocation
  }      	 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}

Query 17: Alternative Labels of author names via ‘federated’ queries’

#title:Alternative Labels of author names via ‘federated’ queries’
PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/>

SELECT DISTINCT ?author ?authorLabel ?wikidataEntity ?altname
WHERE {
	 ?item mmdt:P5 ?author.
      ?author mmdt:P13 ?wikidataEntity.  #exact match
 
      #Federated Query -> Wikidata
SERVICE <https://query.wikidata.org/sparql> {
   	 ?wikidataEntity skos:altLabel ?altname
      }     
 		 
      SERVICE wikibase:label {
   	 bd:serviceParam wikibase:language "en" .
      }
}
LIMIT 1000

Query 18, Fig. 10: Finding Identifiers on other Knowledge Graphs, for example Bibliothèque nationale de France ID

#title:MiMoText novels with URL to Bibliothèque nationale de France 
#defaultView:ImageGrid
PREFIX wd: <http://www.wikidata.org/entity/> #wikidata prefix definition for entity
PREFIX wdt: <http://www.wikidata.org/prop/direct/> #wikidata prefix definition for property
PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/> #mimotext prefix for entity
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/> #mimotext prefix for property

SELECT ?item ?itemLabel ?wikidata ?bnfurl ?image 
WHERE {
  ?item mmdt:P2 mmd:Q2.
  ?item mmdt:P13 ?wikidata.
  ?item rdfs:label ?itemLabel .
  FILTER(lang(?itemLabel) = "en")
  SERVICE <https://query.wikidata.org/sparql> {
    ?wikidata wdt:P268 ?bnfid.
    OPTIONAL{ ?wikidata wdt:P18 ?image.}
   OPTIONAL{ wd:P268 wdt:P1630 ?formatterurl.}
   BIND(IRI(REPLACE(?bnfid, '^(.+)$', ?formatterurl)) AS ?bnfurl).
  }         
}

Query 19, Fig. 11: Influence networks of authors via ‘federated query’

#title:Influence networks of authors via 'federated query'
#defaultView:Graph
PREFIX wd:<http://www.wikidata.org/entity/> #wikidata entity
PREFIX wdt:<http://www.wikidata.org/prop/direct/> #wikidata property

PREFIX mmd:<http://data.mimotext.uni-trier.de/entity/>
PREFIX mmdt:<http://data.mimotext.uni-trier.de/prop/direct/>

SELECT DISTINCT ?item ?itemLabel ?authorLabel ?influencedby ?image ?name
WHERE {
      ?item mmdt:P5 ?author.
      ?author mmdt:P13 ?WikidataEntity.  #exact match
      
#Federated Query -> Wikidata
      SERVICE <https://query.wikidata.org/sparql> {
   	 ?WikidataEntity wdt:P737/wdt:P737 ?influencedby.
     	 # ?influencedby widt:P734 ?name.  
  		 OPTIONAL {  ?influencedby wdt:P18 ?image.}
      }     
 		 
      SERVICE wikibase:label {
   	 bd:serviceParam wikibase:language "en" .
      }
}