首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用owlready (python)获取用于生成本体的维基数据?

如何使用owlready (python)获取用于生成本体的维基数据?
EN

Stack Overflow用户
提问于 2021-02-11 21:48:08
回答 1查看 125关注 0票数 1

wikidata_query(...)的帮助下,从python对维基数据执行查询很简单:

例如。

代码语言:javascript
复制
q = """
SELECT ?item ?itemLabel 
WHERE 
{
  ?item wdt:P279 wd:Q125977.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
"""

wikidata_query(q)

检索Q125977 (向量空间)的所有子类。然而,结果是一个普通的json结构。相反,我想要一个owlready2-ontology (更准确地说,只有subclassOf-relations)。

有没有代码可以(部分地)完成这项任务,或者我必须自己编写代码?在后一种情况下:最聪明的方法是什么(例如,使用一些可定制的深度,处理多重继承)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-11 23:59:47

下面迭代您的输出DataFrame,并用子类断言填充本体:

代码语言:javascript
复制
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']]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66155991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档