<<返回python首页 python

《Python 应用案例》

知识图谱基础之三元组:Python RDFLib 实例入门

logo-rdflib

本节将简要地带您入门Python中的RDF操作库rdflib,三元组是一个在知识图谱中重要的基础性概念,通过在线安装该库,并在线运行两个示例,来理解该库的使用。理解本节的前提,您需要有Python基础,RDF基础。

什么是RDF?

RDF是W3C的资源描述框架(RDF)。RDF提供了一种表达数据图并与其他人(可能更重要的是与机器)共享的标准方法。由于它是W3C“推荐”(通过任何其他措施得出的行业标准),因此围绕RDF出现了大量工具和服务。

RDF的历史可以追溯到1990年,当时 蒂姆·伯纳斯·李(Tim Berners-Lee)撰写了一项提案,文档之间存在不同类型的链接,这使超文本更易于计算机自动理解。输入的链接未包含在第一个HTML规范中,但在元内容框架(Meta Content Framework(MCF)),用于描述元数据和组织Web的系统,由 Ramanathan Guha于1990年代后期在Apple和Netscape任职时,由Tim Bray开发了XML表示形式。W3C一直在寻找通用的元数据表示形式,并且MCF中的许多想法都在1999年的第一个RDF W3C建议书中找到了自己的方法。此后,对标准进行了修订,如今的软件和工具反映了这些改进。

安装RDFLib

RDFLib是开源的,并在GitHub存储库中维护 。PyPi列出了RDFLib的当前版本和先前版本

The best way to install RDFLib is to use pip3

!pip3 install rdflib -i https://pypi.tuna.tsinghua.edu.cn/simple/

它是如何工作

该软件包使用了各种Python习惯用法,这些惯用法提供了将RDF引入以前从未使用过RDF的Python程序员的合适方法。

RDFLib公开的用于RDF的主要接口是 Graph

RDFLib图不是已排序的容器。它们具有普通set 操作(例如add()添加三元组)以及搜索三元组并以任意顺序返回它们的方法。

RDFLib图还重新定义了某些内置的Python方法,以便以可预测的方式运行。它们模拟容器类型,最好将其视为一组3项元组(在RDF中为“三元组”):

[
    (subject0, predicate0, object0),
    (subject1, predicate1, object1),
    ...
    (subjectN, predicateN, objectN)
 ]

示例1:

import rdflib

# create a Graph
g = rdflib.Graph()

# parse in an RDF file hosted on the Internet
result = g.parse("http://www.w3.org/People/Berners-Lee/card")

# loop through each triple in the graph (subj, pred, obj)
for subj, pred, obj in g:
    # check if there is at least one triple in the Graph
    if (subj, pred, obj) not in g:
       raise Exception("It better be!")

# print the number of "triples" in the Graph
print("graph has {} statements.".format(len(g)))
# prints graph has 86 statements.

# print out the entire Graph in the RDF Turtle format
print(g.serialize(format="turtle").decode("utf-8"))

在这里Graph创建了一个,然后在线创建了一个RDF文件,即Tim Berners-Lee的社交网络详细信息,并将其解析到该图中。该print()语句使用该len()函数对图中的三元组数进行计数。

示例2:

from rdflib import Graph, Literal, RDF, URIRef
# rdflib knows about some namespaces, like FOAF
from rdflib.namespace import FOAF , XSD

# create a Graph
g = Graph()

# Create an RDF URI node to use as the subject for multiple triples
donna = URIRef("http://example.org/donna")

# Add triples using store's add() method.
g.add((donna, RDF.type, FOAF.Person))
g.add((donna, FOAF.nick, Literal("donna", lang="ed")))
g.add((donna, FOAF.name, Literal("Donna Fales")))
g.add((donna, FOAF.mbox, URIRef("mailto:donna@example.org")))

# Add another person
ed = URIRef("http://example.org/edward")

# Add triples using store's add() method.
g.add((ed, RDF.type, FOAF.Person))
g.add((ed, FOAF.nick, Literal("ed", datatype=XSD.string)))
g.add((ed, FOAF.name, Literal("Edward Scissorhands")))
g.add((ed, FOAF.mbox, URIRef("mailto:e.scissorhands@example.org")))

# Iterate over triples in store and print them out.
print("--- printing raw triples ---")
for s, p, o in g:
    print((s, p, o))

# For each foaf:Person in the store, print out their mbox property's value.
print("--- printing mboxes ---")
for person in g.subjects(RDF.type, FOAF.Person):
    for mbox in g.objects(person, FOAF.mbox):
        print(mbox)

# Bind the FOAF namespace to a prefix for more readable output
g.bind("foaf", FOAF)

# print all the data in the Notation3 format
print("--- printing mboxes ---")
print(g.serialize(format='n3').decode("utf-8"))

通过本节您应该可以完成rdflib库的安装并成功在线运行两个示例,后续会有关于更多的关于rdflib的文章。

移动端设备除iPad Pro外,其它移动设备仅能阅读基础的文本文字。
建议使用PC或笔记本电脑,浏览器使用Chrome或FireFox进行浏览,以开启左侧互动实验区来提升学习效率,推荐使用的分辨率为1920x1080或更高。
我们坚信最好的学习是参与其中这一理念,并致力成为中文互联网上体验更好的学练一体的IT技术学习交流平台。
您可加QQ群:575806994,一起学习交流技术,反馈网站使用中遇到问题。
内容、课程、广告等相关合作请扫描右侧二维码添加好友。

狐狸教程 Copyright 2021

进入全屏