Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daniel-Mietchen/9033341988b42762c05345f84cdd9f44 to your computer and use it in GitHub Desktop.
Save Daniel-Mietchen/9033341988b42762c05345f84cdd9f44 to your computer and use it in GitHub Desktop.

How to use Gephi to visualize from Wikidata

I'm a long-time fan of the graph visualization tool Gephi and since Wikimania 2019 I got involved with Wikidata. I was aware of the Gephi plugin "Semantic Web Importer", but when I check it out, I only find old tutorials connecting to DBpedia, not Wikidata:

It took a tiny bit of fiddling, but turns out Wikidata seems perfectly compatible with the Semantic Web Importer plugin. Get the REST endpoint for the Wikidata:SPARQL query service (EndPoint URL: https://query.wikidata.org/sparql, query tag name: query), explicitly supply the needed prefixes and then it should work to adapt the WDQS example "Groups of characters in the Marvel universe" for Gephi (it requires a CONSTRUCT rather than a SELECT):

#defaultView:Graph
SELECT ?char ?charLabel ?group ?groupLabel ("7FFF00" as ?rgb)
WHERE {
	?group wdt:P31 wd:Q14514600 ;  # group of fictional characters
          wdt:P1080 wd:Q931597.  # from Marvel universe
 ?char wdt:P463 ?group # member of group
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".}
}

Example Gephi Semantic Web Import SPARQL query:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX bd: <http://www.bigdata.com/rdf#>

#Groups of characters in the Marvel universe
CONSTRUCT {
  ?group gephi:label ?groupLabel .
  ?group gephi:category "group" .
  ?char gephi:label ?charLabel .
  ?char gephi:category "character" .
  ?char wdt:P463 ?group
} WHERE {
  ?group wdt:P31 wd:Q14514600 ;  # group of fictional characters
    wdt:P1080 wd:Q931597.  # from Marvel universe
  ?char wdt:P463 ?group # member of group
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en".}
}

From there on, you should be able to fetch, process and analyse Wikidata straight into Gephi!

Wikidata Marvel example through Gephi Semantic Web Import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment