转载自:SimmerChan 的 知识图谱基础之RDF,RDFS和OWL
本文主要介绍 RDF、RDFS 和 OWL。
一、RDF
RDF(资源描述框架),Resource Description Framework,是 W3C 制定的用于描述资源/实体的标准数据模型。RDF 形式上表示为 SPO 三元组,在知识图谱中又称为一条知识。RDF 由节点和边组成,节点表示实体或属性,边表示节点之间的关系。
RDF 的序列化方式有如下五种:
- RDF/XML
- 用 XML 格式表示 RDF 数据。因为 XML 技术比较成熟,有很多现成的工具可以用来存储、解析 XML。然而,对于 RDF 而言,XML 格式数据太冗长,通常不使用这种方式。
- N-Triples
- 用三元组形式表示 RDF 数据集,是最直观的表示方式。在文件中,一行表示一个三元组,方便机器解析。
- Turtle
- 比 XML 格式紧凑,比 N-Triples 格式可读性好。
- RDFa
- the Resource Description Framework in attributes,是 H5 的扩展,在不改变任何显示效果的情况下,在页面中标记实体。也就是说,将 RDF 数据嵌入网页中,搜索引擎能够更好的解析非结构化页面。可以去 这个页面 感受一下。
- JSON-LD
- JSON for Linked Date,以键值对的形式存储 RDF 数据,可以参考 此网站。
下面以罗纳尔多知识图为例,给出其 N-Triples 和 Turtle 表现形式。
N-Triples 形式:
<http://www.kg.com/person/1> <http://www.kg.com/ontology/chineseName> "罗纳尔多·路易斯·纳萨里奥·德·利马"^^string. <http://www.kg.com/person/1> <http://www.kg.com/ontology/career> "足球运动员"^^string. <http://www.kg.com/person/1> <http://www.kg.com/ontology/fullName> "Ronaldo Luís Nazário de Lima"^^string. <http://www.kg.com/person/1> <http://www.kg.com/ontology/birthDate> "1976-09-18"^^date. <http://www.kg.com/person/1> <http://www.kg.com/ontology/height> "180"^^int. <http://www.kg.com/person/1> <http://www.kg.com/ontology/weight> "98"^^int. <http://www.kg.com/person/1> <http://www.kg.com/ontology/nationality> "巴西"^^string. <http://www.kg.com/person/1> <http://www.kg.com/ontology/hasBirthPlace> <http://www.kg.com/place/10086>. <http://www.kg.com/place/10086> <http://www.kg.com/ontology/address> "里约热内卢"^^string. <http://www.kg.com/place/10086> <http://www.kg.com/ontology/coordinate> "-22.908333, -43.196389"^^string.
Turtle 形式:
@prefix person: <http://www.kg.com/person/> . @prefix place: <http://www.kg.com/place/> . @prefix : <http://www.kg.com/ontology/> . person:1 :chineseName "罗纳尔多·路易斯·纳萨里奥·德·利马"^^string. person:1 :career "足球运动员"^^string. person:1 :fullName "Ronaldo Luís Nazário de Lima"^^string. person:1 :birthDate "1976-09-18"^^date. person:1 :height "180"^^int. person:1 :weight "98"^^int. person:1 :nationality "巴西"^^string. person:1 :hasBirthPlace place:10086. place:10086 :address "里约热内卢"^^string. place:10086 :coordinate "-22.908333, -43.196389"^^string.
当一个实体拥有多个属性(数据属性)或关系(对象属性)时,可以只用一个 Subject 表示,使得文件更紧凑。
@prefix person: <http://www.kg.com/person/> . @prefix place: <http://www.kg.com/place/> . @prefix : <http://www.kg.com/ontology/> . person:1 :chineseName "罗纳尔多·路易斯·纳萨里奥·德·利马"^^string; :career "足球运动员"^^string; :fullName "Ronaldo Luís Nazário de Lima"^^string; :birthDate "1976-09-18"^^date; :height "180"^^int; :weight "98"^^int; :nationality "巴西"^^string; :hasBirthPlace place:10086. place:10086 :address "里约热内卢"^^string; :coordinate "-22.908333, -43.196389"^^string.
即一个实体用一个句子(以.结束)表示,属性通过“;”隔开。
上一篇文章提到过 RDF 的表达能力有限,无法区分类和对象,无法定义类的关系/属性。以罗纳尔多为例,RDF 能够很好的表示罗纳尔多和里约热内卢这两个实体有哪些属性,以及这两个实体之间的关系。但 RDF 无法定义罗纳尔多是人,里约热内卢是地点,以及人有哪些属性,地点有哪些属性,人和地点有哪些关系等等。在现实应用中,这种泛化抽象能力都是相当重要的,RDFS 和 OWL 这两种模式语言/本体语言解决了上述问题。
二、RDFS 和 OWL
RDFS 和 OWL 序列化方式和 RDF 没什么不同,在表现形式上它们就是 RDF。RDFS 和 OWL 本质上是一些预定义词汇构成的集合,对 RDF 进行类定义和属性定义。通常我们用小写字母开头的单词或词组表示属性,大写字母开头的单词或词组表示类。数据属性(data property),实体和字面量的关系,通常选择名词;对象属性(object property),实体和实体的关系,通常选择动词。剩下部分符合驼峰命名,类似 Java 中的变量和方法的命名规范。
RDFS
Resource Description Framework Schema,是最基础的模式语言。下面定义了人和地点两个类及其属性。
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix : <http://www.kg.com/ontology/> . ### 这里我们用词汇rdfs:Class定义了“人”和“地点”这两个类。 :Person rdf:type rdfs:Class. :Place rdf:type rdfs:Class. ### rdfs当中不区分数据属性和对象属性,词汇rdf:Property定义了属性,即RDF的“边”。 :chineseName rdf:type rdf:Property; rdfs:domain :Person; rdfs:range xsd:string . :career rdf:type rdf:Property; rdfs:domain :Person; rdfs:range xsd:string . :fullName rdf:type rdf:Property; rdfs:domain :Person; rdfs:range xsd:string . :birthDate rdf:type rdf:Property; rdfs:domain :Person; rdfs:range xsd:date . :height rdf:type rdf:Property; rdfs:domain :Person; rdfs:range xsd:int . :weight rdf:type rdf:Property; rdfs:domain :Person; rdfs:range xsd:int . :nationality rdf:type rdf:Property; rdfs:domain :Person; rdfs:range xsd:string . :hasBirthPlace rdf:type rdf:Property; rdfs:domain :Person; rdfs:range :Place . :address rdf:type rdf:Property; rdfs:domain :Place; rdfs:range xsd:string . :coordinate rdf:type rdf:Property; rdfs:domain :Place; rdfs:range xsd:string
这里介绍 RDFS 常用的几个词汇:
属性 | 作用 | 示例 |
rdfs:Class | 定义类 | :Person rdf:type rdfs:Class |
rdfs:domain | 定义属性属于哪个类 | :fullName rdfs:domain :Person |
rdfs:range | 定义属性的数据类型 | :hasBirthPlace rdfs:range :Place :fullName rdfs:range xsd:string |
rdfs:subClassOf | 定义该类的父类 | :Manager rdfs:subClassOf :Employee |
rdfs:subProperty | 定义该属性的父属性 | :englishName rdfs:subProperty :name :chineseName rdfs:subProperty :name |
OWL
RDFS 本质是 RDF 的一个扩展,RDFS 的表达能力还是有限,因此提出了 OWL。所以 OWL 也可以看作是 RDFS 的一个扩展,添加了额外的预定义词汇。OWL,Web Ontology Lanuage,是语义网技术栈的核心,有两个主要的功能:
- 提供快速灵活的建模能力
- 高效的自动推理
通过 OWL 可以对人和地点如此建模:
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix : <http://www.kg.com/ontology/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . ### 这里我们用词汇owl:Class定义了“人”和“地点”这两个类。 :Person rdf:type owl:Class. :Place rdf:type owl:Class. ### owl区分数据属性和对象属性(对象属性表示实体和实体之间的关系)。词汇owl:DatatypeProperty定义了数据属性,owl:ObjectProperty定义了对象属性。 :chineseName rdf:type owl:DatatypeProperty; rdfs:domain :Person; rdfs:range xsd:string . :career rdf:type owl:DatatypeProperty; rdfs:domain :Person; rdfs:range xsd:string . :fullName rdf:type owl:DatatypeProperty; rdfs:domain :Person; rdfs:range xsd:string . :birthDate rdf:type owl:DatatypeProperty; rdfs:domain :Person; rdfs:range xsd:date . :height rdf:type owl:DatatypeProperty; rdfs:domain :Person; rdfs:range xsd:int . :weight rdf:type owl:DatatypeProperty; rdfs:domain :Person; rdfs:range xsd:int . :nationality rdf:type owl:DatatypeProperty; rdfs:domain :Person; rdfs:range xsd:string . :hasBirthPlace rdf:type owl:ObjectProperty; rdfs:domain :Person; rdfs:range :Place . :address rdf:type owl:DatatypeProperty; rdfs:domain :Place; rdfs:range xsd:string . :coordinate rdf:type owl:DatatypeProperty; rdfs:domain :Place; rdfs:range xsd:string .
这里介绍 OWL 几个常用的预定义词汇:
属性 | 作用 | 示例 |
owl:TransitiveProperty | 表示该属性有传递性 | A位于B,B位于C,则A位于C |
owl:SymmetricProperty | 表示该属性有对称性 | A是B的同事,则B是A的同事 |
owl:FunctionalProperty | 表示该属性有唯一性 | A的母亲是B,A的母亲也是C,则B和C是同一个人 |
owl:inverseOf | 定义某个属性的相反关系 | A是B的丈夫,则B是A的妻子 :isHusbandOf owl:inverseOf :isWifeOf |
owl:equivalentClass | 表示两个类等价 | :Person owl:equivalentClass :Human |
owl:equivalentProperty | 表示两个属性是相同的 | :coverPicUrl owl:equivalentProperty :picUrl |
owl:sameAs | 表示两个实体是相同的 | http://www.domain.com/person/1 owl:sameAs http://www.domain.com/human/10086 |
上面4个词汇是描述属性特征的词汇,下面3个词汇是本体映射词汇。本体映射词汇主要用于多源数据融合时,比如张三构建一个本体结构,用 :Person 类定义人,李四也构建了一个本体结构,用 :Human 类定义人。当融合这两个本体时,就可以用本体映射词汇。没有 OWL 的本体映射词汇,就无法融合本体,就无法构建链接数据。更多 OWL 词汇和特性请参考 W3C官网文档。
推理能力
知识图谱的推理能力主要分为基于本体的推理和基于规则的推理,这里主要谈的是基于本体的推理。上面的几个词汇其实已经体现了基于本体的推理能力,如果我们只单向存储谁是谁的丈夫信息,只要将丈夫和妻子两个对象属性定义为相反关系,就可以自动推导出谁是谁的妻子信息,而不需要手动补全这个信息。
三、小结
本文主要介绍了 RDF 的序列化方式,如何利用 RDFS/OWL 进行 schema 层的建模,和OWL的推理功能。