python-3.x 基于owlready的本体实体标注访问

s4n0splo  于 2023-03-20  发布在  Python
关注(0)|答案(1)|浏览(108)

我想使用owlready库从本体访问实体的注解属性。在下面的屏幕截图中,我想访问所选实体的定义,即冲动控制障碍。

from owlready2 import *
onto = get_ontology("./HumanDO.owl").load()
for subclass_of_mental_health in list(onto.search(label ="disease of mental health")[0].subclasses()):
    print(subclass_of_mental_health.id, subclass_of_mental_health.label) # This gives outputs (see below)
    print(subclass_of_mental_health.definition) # This results in error

上面是访问冲动控制障碍实体的代码。我能够通过简单地使用点符号(<entity>.id)访问id,但当我尝试<entity.definition>时,得到

['DOID:10937'] ['impulse control disorder']
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_14348\3127299132.py in <module>
      1 for subclass_of_mental_health in list(onto.search(label ="disease of mental health")[0].subclasses()):
      2     print(subclass_of_mental_health.id, subclass_of_mental_health.label)
----> 3     print(subclass_of_mental_health.definition)

E:\Python\PythonInstall\envs\new\lib\site-packages\owlready2\entity.py in __getattr__(Class, attr)
    592     else:
    593       Prop = Class.namespace.world._props.get(attr)
--> 594       if not Prop: raise AttributeError("'%s' property is not defined." % attr)
    595       if issubclass_python(Prop, AnnotationProperty):
    596         attr = "__%s" % attr # Do NOT cache as such in __dict__, to avoid inheriting annotations

AttributeError: 'definition' property is not defined.

本体看起来像

,您可以从这里下载实际的文件Github link
我的最终目标是读取一些注解并存储它们,但我只是停留在阅读它们的阶段,为了说明这一点,我不能访问除id和label之外的任何属性

oxalkeyp

oxalkeyp1#

加载完数据后,您需要添加sync_reasoner(),如下所示:

human_onto = get_ontology("./HumanDO.owl").load()
sync_reasoner()

然后,您可以访问所有属性,如下所示:

human_results = human_onto.search(label =name , _case_sensitive = False)
for res in human_results:
    print("# ",res)
        
    for prop in res.get_properties(res):
        print("Property: ", prop)
        print("Value: ", prop[res])

相关问题