当前位置: 首页 > 工具软件 > Python OWL > 使用案例 >

python解析owl文件

洪楷
2023-12-01

参考网站:http://rdflib.readthedocs.io/en/stable/

# -*- coding: utf-8 -*-
import rdflib
import re
from rdflib import Namespace,RDF


# q = "select ?Subject ?Object where { ?Subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#" + \
#     Predicate + ">  ?Object}"
g1 = rdflib.Graph()
g1.parse("./yyyy.owl", format="xml")
ns = Namespace('http://www.semanticweb.org/liu/ontologies/2018/4/untitled-ontology-8#')  # 命名空间

# q = "select ?Subject ?Object where { ?Subject is_located ?Object}"

# for s,_,o in g1.triples((None,ns["is_located"],None)):
#     print("s_O",s,o)
#     print('\n\n')



# for s in g1.subjects(ns['is_located'],ns['A513']):
#     print('s: ',s)

ans1 = []
for s in g1.subjects(ns['firstname_is'],ns['王']):
    ans1.append(s)
# print(ans1)

ans2 = []
for s in g1.subjects(RDF.type,ns['教授']):
    ans2.append(s)
# print(ans2)

ans3 = list(set(ans1).intersection(set(ans2)))

for res in ans3:
    for s in g1.objects(res,ns['is_located']):
        print('s  ',str(s).split('#')[-1])


 类似资料: