Skip to content

Instantly share code, notes, and snippets.

@UncleCJ
Last active February 29, 2024 09:32
Show Gist options
  • 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

@UncleCJ
Copy link
Author

UncleCJ commented Dec 9, 2019

@barripdmx Search in Spanish, or something regarding Spain?
Sounds like you should get acquainted with SPARQL, this is a good tutorial:
https://m.youtube.com/watch?feature=youtu.be&v=b3ft3CzkLYk
If you want Spanish, it's as simple as changing "en" to "es" in my example. Wikidata is multilingual and serve wikipedias of all language versions, that's a point of the project.
If you're interested in Spain, start with checking out the properties of its Wikidata item, and what is related to it (too many items). Load and modify a suitable example from Wikidata query service, get it to work, then modify it as I did and you can plug it into Gephi Semantic Web Importer. Easy :-D

https://www.wikidata.org/wiki/Q29
https://www.wikidata.org/wiki/Special:WhatLinksHere/Q29
https://query.wikidata.org/

@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