rdf - 以 Google RDF 格式表达句子
问题描述
我想写一个python代码来表达这样的图:
吉姆→正在吃→一个苹果
苹果→在→厨房
以 RDF 格式。我已经在 python 中尝试了 RDFlib,但我对如何做到这一点感到困惑。如果有人能在这件事上提供帮助,我将不胜感激。
编辑 1:
我在第一句话中为 Apple 和 Jim 定义了两个 URI 节点。所以我仍然很困惑我如何在两个节点之间连接我的谓词“正在吃”并将它们添加到图 g。如果有人也可以就此提供指导,我将不胜感激。
from rdflib.namespace import FOAF , XSD
# create a Graph
g = Graph()
# Create an RDF URI node to use as the subject for multiple triples
apple = URIRef("http://example.org/apple")
# Add another node
jim = URIRef("http://example.org/jim")```
解决方案
以下是使用命名空间时所需三元组的更好表述:
from rdflib import Graph, Namespace
EG = Namespace("http://example.org/")
# create a Graph, bind the namespace
g = Graph()
g.bind("eg", EG)
# Create an RDF URI nodes
apple = EG.Apple
jim = EG.Jim
kitchen = EG.Kitchen
# Create URI-based predicates
is_eating = EG.isEating
is_in = EG.isIn
g.add((jim, is_eating, apple))
g.add((apple, is_in, kitchen))
# print graph data in the Notation3 format
print(g.serialize())
# equivalent graph, using the Namespace objects directly:
g2 = Graph()
g2.bind("eg", EG)
g2.add((EG.Jim, EG.isEating, EG.Apple))
g2.add((EG.Apple, EG.isIn, EG.Kitchen))
print(g2.serialize())
请注意,除了为它们分配示例 URI 之外,这里没有真正定义任何节点或边(谓词)。如果您真的想使用诸如 的关系isEating
,您将需要定义一个定义该谓词的本体,如下所示:
# ontology document snippet
...
<http://example.org/isEating>
a owl:ObjectProperty ;
rdfs:label "is eating" ;
rdfs:comment "The act of consuming a resource for a living organism's energy and nutrition requirement."@en ;
rdfs:domain foaf:Person ;
rdfs:range ex:FoodItem ;
.
...
推荐阅读
- android - 获取 android(aosp) 设备中的当前状态转换状态
- javascript - 打印组件时 Chrome 和 Angular 8 应用程序出现问题,加载打印预览需要太多时间
- ios - 如何在单独的飞镖隔离中从@ryanheise 运行 just_audio
- r - 获得最大法术之间的时间分布
- linux - Find a user defined keyword in real-time log (text) file using shell script
- prolog - Prolog - 定义规则的更简洁方式
- java - HBase java 错误 - 预期 HEADER=HBas 但收到 HEADER=\x00\x00\x01\x0B
- python - Xcode 上 python 的颜色语法
- python - 整数与整数的比较或存在于集合或元组中
- javascript - 如何将添加的输入值动态推送到数组?