Skip to content

Instantly share code, notes, and snippets.

@UncleCJ
Last active February 29, 2024 09:32
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save UncleCJ/2408aef8eab09cc1da3404c5af43537b to your computer and use it in GitHub Desktop.
Save UncleCJ/2408aef8eab09cc1da3404c5af43537b 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

@thgie
Copy link

thgie commented Feb 29, 2024

Thank you, this is fantastic. I needed to add a Gephi prefix to run the query properly.

PREFIX gephi: <http://gephi.org/#>

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