You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在owlready2中获取对象属性的comment注释?

OWLready2获取对象属性rdfs:comment为空的解决办法

问题场景

使用OWLready2处理OWL本体的对象属性时,通过点属性可正常获取label、isDefinedBy等注释,但调用property.comment始终返回空列表,尽管目标属性在OWL文件中明确包含rdfs:comment断言。

代码示例

>>> print(property.name)
actsFor
>>> print(property.label)
[locstr('acts for', 'en'), locstr('agisce per', 'it')]
>>> print(property.comment)
[]

对应OWL片段

<!-- http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#actsFor -->

<owl:ObjectProperty rdf:about="http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#actsFor">
    <rdfs:subPropertyOf rdf:resource="http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#associatedWith"/>
    <owl:inverseOf rdf:resource="http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#actsThrough"/>
    <rdfs:domain rdf:resource="http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#Agent"/>
    <rdfs:range rdf:resource="http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#SocialAgent"/>
    <rdfs:comment>The relation holding between any Agent, and a SocialAgent. In principle, a SocialAgent requires at least one PhysicalAgent in order to act, but this dependency can be &apos;delegated&apos;; e.g. a university can be acted for by a department, which on its turm is acted for by physical agents.</rdfs:comment>
    <rdfs:isDefinedBy rdf:resource="http://www.ontologydesignpatterns.org/ont/dul/DUL.owl"/>
    <rdfs:label xml:lang="en">acts for</rdfs:label>
    <rdfs:label xml:lang="it">agisce per</rdfs:label>
</owl:ObjectProperty>

解决方法

OWLready2默认仅为部分常用RDFS注释属性(如label、isDefinedBy)创建了直接的Python点属性映射,rdfs:comment不在默认映射范围内,可通过以下两种方式获取:

方法1:手动绑定属性映射

将rdfs:comment注释属性绑定到ObjectProperty类的comment属性,之后即可通过点属性直接访问:

from owlready2 import *

# 加载目标本体
onto = get_ontology("http://www.ontologydesignpatterns.org/ont/dul/DUL.owl").load()

# 获取rdfs:comment注释属性
rdfs_comment = onto.get_annotation_property("http://www.w3.org/2000/01/rdf-schema#comment")

# 绑定到ObjectProperty类的comment属性
ObjectProperty.comment = rdfs_comment

# 之后即可正常访问
property = onto.actsFor
print(property.comment)

方法2:使用通用注释获取方法

无需绑定,直接通过get_annotations方法或遍历annotations属性筛选指定注释:

from owlready2 import *

onto = get_ontology("http://www.ontologydesignpatterns.org/ont/dul/DUL.owl").load()
rdfs_comment = onto.get_annotation_property("http://www.w3.org/2000/01/rdf-schema#comment")
property = onto.actsFor

# 方式1:使用get_annotations批量获取
comments = property.get_annotations(rdfs_comment)
print([anno.value for anno in comments])

# 方式2:遍历所有注释筛选
for anno in property.annotations:
    if anno.property == rdfs_comment:
        print(anno.value)

内容的提问来源于stack exchange,提问作者Xin Feng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:49:55