Search code examples
pythonowlontologywikidataowlready

How to harvest wikidata for generating an ontology with owlready (python)?


With the help e.g. of wikidata_query(...) it is simple to perform queries to wikidata from python:

E.g.

q = """
SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P279 wd:Q125977.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
"""

wikidata_query(q)

retrieves all subclasses of Q125977 (vector space). However, the result is a plain json structure. I instead want to have an owlready2-ontology (more precisely only the subclassOf-relations).

Is there code available that (partially) does this task or do I have to code this by myself? In the latter case: what would be the smartest way to do (e.g. use some customizable depth, handle multiple inheritance)?


Solution

  • The following iterates over your output DataFrame and populates the ontology with subclass assertions:

    from owlready2 import *
    import re
    
    def get_entity_id(iri) :
        return re.sub(r".*[#/\\]", "", iri)
    
    def get_subclass_assertions(wd_superclass_id) :
        q = """
        SELECT ?item ?itemLabel 
        WHERE 
        {
          ?item wdt:P279 wd:%s.
          SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
        }
        """ % wd_superclass_id
        
        ontology = get_ontology("http://www.wikidata.org/entity/")
        with ontology:
            SuperClass = types.new_class(wd_superclass_id, (Thing,))
            assert SuperClass.name == wd_superclass_id
            for _, row in wikidata_query(q).iterrows():
                new_class_id = get_entity_id(row['item'])
                NewClass = types.new_class(new_class_id, (SuperClass,))
                NewClass.label = row['itemLabel']
        
        return ontology
    
    wd_id = "Q125977"
    
    ontology = get_subclass_assertions(wd_id)       # get the ontology
    subclasses = list(ontology[wd_id].subclasses()) # now you can accede to subclasses
    
    print(subclasses)                               # [entity.Q190109, entity.Q289411, entity.Q305936, entity.Q464794, entity.Q577835, entity.Q726210, entity.Q728435, entity.Q752487, entity.Q766774, entity.Q1000660, entity.Q1393796, entity.Q1455249, entity.Q1503340, entity.Q1777803, entity.Q2133608, entity.Q2344309, entity.Q3058206, entity.Q4131086, entity.Q4131105, entity.Q5156614, entity.Q7642975, entity.Q15834383, entity.Q42797338, entity.Q46996054, entity.Q63793693, entity.Q77595632, entity.Q77604597, entity.Q91474415, entity.Q98929437]
    print(list(map(lambda c: c.iri, subclasses)))   # ['http://www.wikidata.org/entity/Q190109', 'http://www.wikidata.org/entity/Q289411', 'http://www.wikidata.org/entity/Q305936', 'http://www.wikidata.org/entity/Q464794', 'http://www.wikidata.org/entity/Q577835', 'http://www.wikidata.org/entity/Q726210', 'http://www.wikidata.org/entity/Q728435', 'http://www.wikidata.org/entity/Q752487', 'http://www.wikidata.org/entity/Q766774', 'http://www.wikidata.org/entity/Q1000660', 'http://www.wikidata.org/entity/Q1393796', 'http://www.wikidata.org/entity/Q1455249', 'http://www.wikidata.org/entity/Q1503340', 'http://www.wikidata.org/entity/Q1777803', 'http://www.wikidata.org/entity/Q2133608', 'http://www.wikidata.org/entity/Q2344309', 'http://www.wikidata.org/entity/Q3058206', 'http://www.wikidata.org/entity/Q4131086', 'http://www.wikidata.org/entity/Q4131105', 'http://www.wikidata.org/entity/Q5156614', 'http://www.wikidata.org/entity/Q7642975', 'http://www.wikidata.org/entity/Q15834383', 'http://www.wikidata.org/entity/Q42797338', 'http://www.wikidata.org/entity/Q46996054', 'http://www.wikidata.org/entity/Q63793693', 'http://www.wikidata.org/entity/Q77595632', 'http://www.wikidata.org/entity/Q77604597', 'http://www.wikidata.org/entity/Q91474415', 'http://www.wikidata.org/entity/Q98929437']
    print(list(map(lambda c: c.label, subclasses))) # [['field'], ['sequence space'], ['Lp space'], ['Minkowski spacetime'], ['field extension'], ['normed vector space'], ['linear subspace'], ['dual space'], ['symplectic vector space'], ['algebra over a field'], ['quotient space'], ['topological vector space'], ['ordered vector space'], ['coalgebra'], ['coordinate space'], ['pseudo-Euclidean space'], ['graded vector space'], ['row space'], ['column space'], ['complex vector space'], ['super vector space'], ['matrix space'], ['function vector space'], ['real vector space'], ['seminormed space'], ['real or complex vector space'], ['finite-dimensional vector space'], ['quadratic space'], ['space of linear maps']]