1. 程式人生 > >python解析owl檔案

python解析owl檔案

# -*- 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])