本文整理汇总了Python中rdflib.Graph.subject_predicates方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.subject_predicates方法的具体用法?Python Graph.subject_predicates怎么用?Python Graph.subject_predicates使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rdflib.Graph
的用法示例。
在下文中一共展示了Graph.subject_predicates方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import subject_predicates [as 别名]
class Model:
def __init__(self):
self.graph = Graph()
self.loaded = set()
def load(self, source, format=None):
if source not in self.loaded:
self.loaded.add(source)
try:
self.graph.parse(source, format=format)
except Exception as e:
print e
return False
return True
def size(self):
return len(self.graph)
def pred(self, subj):
return list(set(self.graph.predicates(subj)))
def types(self):
return set(self.graph.objects(predicate=RDF.type))
def contains_resource(self, ref):
resources = filter(lambda x: type(x) == URIRef, self.graph.all_nodes())
return ref in resources
def get_resource_objects(self, subj, pred):
return filter(lambda x: type(x) == URIRef, self.graph.objects(subj, pred))
def get_objects(self, subj, pred):
return list(self.graph.objects(subj, pred))
def get_subjects(self, pred, obj):
return list(self.graph.subjects(pred, obj))
def get_properties(self, subj):
properties = {}
for pred, obj in self.graph.predicate_objects(subj):
if pred in properties:
properties[pred].append(obj)
else:
properties[pred] = [obj]
return properties
def get_reverse_properties(self, obj):
properties = {}
for subj, pred in self.graph.subject_predicates(obj):
if pred in properties:
properties[pred].append(subj)
else:
properties[pred] = [subj]
return properties
def norm(self, ref):
return self.graph.namespace_manager.normalizeUri(ref) if ref else None
def to_uriref(self, string):
"""Expand QName to UriRef based on existing namespaces."""
if not string:
return None
elif re.match('[^:/]*:[^:/]+', string):
prefix, name = string.split(':')
try:
namespace = dict(self.graph.namespaces())[prefix]
return namespace + name
except:
return None
else:
return URIRef(string)
示例2: isinstance
# 需要导入模块: from rdflib import Graph [as 别名]
# 或者: from rdflib.Graph import subject_predicates [as 别名]
# out.add((res,RDF.type,cl))
if isinstance(p, URIRef) and p not in seen and is_domainont(p):
to_search.add(p)
if o not in seen:
if isinstance(o, BNode) or (isinstance(o, URIRef) and is_domainont(o)):
to_search.add(o)
# res as predicate
# for s,o in g.subject_objects(res):
# out.add((s,res,o))
# if isinstance(s, URIRef) and s not in seen and is_domainont(s):
# to_search.add(s)
# if isinstance(o, URIRef) and o not in seen and is_domainont(o):
# to_search.add(o)
# res as object
for s,p in g.subject_predicates(res):
# if not res.startswith(OWL): # ignore spurious owl:Class instances
out.add((s,p,res))
# for cl in g.objects(s, RDF.type):
# out.add((s,RDF.type,cl))
if s not in seen:
if isinstance(s, BNode) or (isinstance(s, URIRef) and is_domainont(s)):
to_search.add(s)
if isinstance(p, URIRef) and p not in seen and is_domainont(p):
to_search.add(p)
if args.old is not None:
old = Graph()
# copy namespace defs
for prefix, ns in g.namespaces():
old.namespace_manager.bind(prefix, ns)