如何使用 owlready (python) 收集维基数据以生成 ontology?

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

在帮助下,例如wikidata_query(...) 的 python:

执行维基数据查询很简单

例如

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

wikidata_query(q)

检索 Q125977 (vector space). However, the result is a plain json structure. I instead want to have an owlready2-ontology 的所有子类(更准确地说,仅检索 subclassOf-关系)。

是否有可用代码(部分)执行此任务,或者我是否必须自己编写代码?在后一种情况下:最明智的做法是什么(例如,使用一些可定制的深度,处理多重继承)?

以下遍历您的输出 DataFrame 并使用子类断言填充 ontology:

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']]