python上下文是什么_python – 在rdflib中使用上下文

是.这是代码

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

a rdfg:Graph;rdflib:storage [a rdflib:Store;rdfs:label 'IOMemory'].

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'))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值