您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 在rdflib中使用上下文

5b51 2022/1/14 8:23:20 python 字数 2673 阅读 593 来源 www.jb51.cc/python

我很难找到一个明确,明智的rdflib使用上下文的例子. ConjunctiveGraph不接受上下文,并且不推荐使用Graph.我应该如何在同一个全局ConjunctiveGraph中的不同上下文中创建和操作? 是.这是代码 import rdflib from rdflib.Graph import Graph conj=rdflib.ConjunctiveGraph() NS=rdfli

概述

import rdflib
from rdflib.Graph import Graph

conj=rdflib.ConjunctiveGraph()

NS=rdflib.Namespace("http://example.com/#")
NS_CTX=rdflib.Namespace("http://example.com/context/#")

alice=NS.alice
bob=NS.bob
charlie=NS.charlie

pizza=NS.pizza
meat=NS.meat
chocolate=NS.chocolate

loves=NS.loves
hates=NS.hates
likes=NS.likes
dislikes=NS.dislikes

love_ctx=Graph(conj.store,NS_CTX.love)
food_ctx=Graph(conj.store,NS_CTX.food)

love_ctx.add( (alice,loves,bob) )
love_ctx.add( (alice,charlie) )
love_ctx.add( (bob,hates,charlie) )
love_ctx.add( (charlie,bob) )

food_ctx.add( (alice,likes,chocolate) )
food_ctx.add( (alice,meat) )
food_ctx.add( (alice,dislikes,pizza) )

print "Full context"
for t in conj:
    print t

print ""
print "Contexts"
for c in conj.contexts():
    print c

print "love context"
for t in love_ctx:
    print t

print "food context"
for t in food_ctx:
    print t

这是输出

Full context
(rdflib.URIRef('http://example.com/#bob'),rdflib.URIRef('http://example.com/#hates'),rdflib.URIRef('http://example.com/#charlie'))
(rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#likes'),rdflib.URIRef('http://example.com/#chocolate'))
(rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#meat'))
(rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#dislikes'),rdflib.URIRef('http://example.com/#pizza'))
(rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#loves'),rdflib.URIRef('http://example.com/#bob'))
(rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#charlie'))
(rdflib.URIRef('http://example.com/#charlie'),rdflib.URIRef('http://example.com/#bob'))

Contexts
<http://example.com/context/#food> a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory'].
<http://example.com/context/#love> a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory'].
love context
(rdflib.URIRef('http://example.com/#bob'),rdflib.URIRef('http://example.com/#bob'))
food context
(rdflib.URIRef('http://example.com/#alice'),rdflib.URIRef('http://example.com/#pizza'))

总结

以上是编程之家为你收集整理的python – 在rdflib中使用上下文全部内容,希望文章能够帮你解决python – 在rdflib中使用上下文所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶