すべてのエントリを反復せずに、件名を知ることでオブジェクトを検索する方法はありますか?
答えはイエスです。また、さまざまなメカニズムに使用できます。(a) 制限付きで反復します。または (b) SPARQL クエリを発行します。
(a) グラフを制約して繰り返す
triples
このソリューションは、Graph オブジェクトに対してRDFLib関数を使用します。このリファレンスを参照してください。
#Parse the file
g = rdflib.Graph()
g.parse("yourdata.nquads")
subject = article = rdflib.term.URIRef("http://www.someuri.org/for/your/subject")
# (subject,None,None) represents a constrain to iterate over the graph. By setting
# any of the three elements in the triple you constrain by any combination of subject,
# predicate or object. In this case we only constrain by subject.
for triple in g.triples((subject,None,None)):
print triple
(b) SPARQL クエリを発行する
SPARQL 標準を使用した、より標準的なソリューション。
rdflib.plugin.register('sparql', rdflib.query.Processor,
'rdfextras.sparql.processor', 'Processor')
rdflib.plugin.register('sparql', rdflib.query.Result,
'rdfextras.sparql.query', 'SPARQLQueryResult')
#Parse the file
g = rdflib.Graph()
g.parse("yourdata.nquads")
query = """
SELECT ?pred ?obj WHERE {
<http://www.someuri.org/for/your/subject> ?pred ?obj
}
"""
for row in g.query(query):
print "Predicate:%s Object:%s"%(row[0],row[1])