一 背景知识
知识图谱方向目前越来越火,顺带着,表示知识图谱的语言,如rdf,owl等也跟着火了起来。但是这些语言都有一定的门槛,没接触过xml的初学者不可能在短时间内掌握,这些语言的熟练工也无法通过快速浏览文件来判断图谱的结构。因此,需要将这些语言所表示的结构用复杂网络的形式表现出来。这样,我们就可以根据可视化后网络的结构直观地认识这个图谱,也可以通过网络的各种系数(集聚系数等)了解图谱的一些特性。
rdf即resource defined framework,资源定义框架,是用来表示知识图谱中的实体及其关系的。owl即ontology web language,本体网络语言,可以简单理解为rdf的进化版本。它既可以表示实体之上的一层概念--类,又可以表示类的实例(individual)。但owl使得实例更加规范。所谓的“本体”,是指某个领域的一些概念的集合,比如大学本体,葡萄酒本体等。这些本体可以用owl来表示。本体中包含着很多类(class),每个类都有自己的名字(vocabulary)。具体如下图表示:(图片转自知乎,稍后给出链接):
二 转化方式
那么,怎样才能把一个rdf或者owl或者其他表示实体或本体的文件转化成复杂网络呢?想想可能有些头疼,还好大牛们已经为我们写好了现成的工具--一个特定的jar包。Stack Overflow网站上有人提出了这个问题,别人的回答中给出了jar包的链接。原问题和答案链接如下:
https://stackoverflow.com/questions/45362802/using-jena-to-convert-an-owl-file-to-n-triple-from-terminal-returns-an-empty-fil
jar包的下载地址如下:
http://www.l3s.de/~minack/rdf2rdf/
上面的链接中介绍了jar包的用法,需要在Linux下(或者cmd下)运行:
Run the jar file like this: java -jar rdf2rdf-1.0.1-2.3.1.jar INPUTFILE(S) OUTPUTFILE
三 实践
从网上下载了wine.owl,进行转化。
进入wine.owl所在的目录后,将jar包也放入同一个文件夹,在终端输入java -jar rdf2rdf-1.0.1-2.3.1.jar wine.owl wine.nt
然后就能在当前目录下找到对应的nt文件了。
下面将两个文件的部分内容对应起来讲解。如,wine.owl(1)与wine.nt(1)相对应,wine.nt(1)表示的是wine.owl(1)转化而成的三元组。
wine.owl(1):
//下面一小段代码是对当前wine本体的描述
<owl:Ontology rdf:about="">//对owl本体的声明,为空表示用owl的默认URI
<rdfs:comment>An example OWL ontology</rdfs:comment>
//这一段是owl本体的一个例子
<owl:priorVersion>//版本控制
<owl:Ontology rdf:about="http://www.example.org/wine-020303"/>
</owl:priorVersion>
<owl:imports rdf:resource="http://www.w3.org/2001/sw/WebOnt/guide-src/food.owl"/>//其他本体的嵌入
<rdfs:comment>Derived from the DAML Wine ontology at
http://ontolingua.stanford.edu/doc/chimaera/ontologies/wines.daml
Substantially changed, in particular the Region based relations.
</rdfs:comment>//注释
<rdfs:label>Wine Ontology</rdfs:label>//标注
</owl:Ontology>
wine.nt(1)
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .//wine是一个本体
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/2000/01/rdf-schema#comment> "An example OWL ontology" .//对wine的一个注释是:An example OWL ontology
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/2002/07/owl#priorVersion> <http://www.example.org/wine-020303> .//wine的版本控制为。。(此处object是一个匿名节点)
<http://www.example.org/wine-020303> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .//这个版本是一个本体
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/2002/07/owl#imports> <http://www.w3.org/2001/sw/WebOnt/guide-src/food.owl> .//wine嵌入food本体
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/2000/01/rdf-schema#comment> "Derived from the DAML Wine ontology at \n http://ontolingua.stanford.edu/doc/chimaera/ontologies/wines.daml\n Substantially changed, in particular the Region based relations." .//wine的一个注释是。。
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/2000/01/rdf-schema#label> "Wine Ontology" .
//wine的一个标签是Wine Ontology
wine.owl(2)
<owl:Class rdf:ID="Wine">
<rdfs:subClassOf rdf:resource="&food;PotableLiquid" />
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="#hasMaker" />
<owl:cardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:cardinality>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="#hasMaker" />
<owl:allValuesFrom rdf:resource="#Winery" />
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="#madeFromGrape" />
<owl:minCardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:minCardinality>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="#hasSugar" />
<owl:cardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:cardinality>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="#hasFlavor" />
<owl:cardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:cardinality>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="#hasBody" />
<owl:cardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:cardinality>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="#hasColor" />
<owl:cardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:cardinality>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="#locatedIn"/>
<owl:someValuesFrom rdf:resource="&vin;Region"/>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:label xml:lang="en">wine</rdfs:label>
<rdfs:label xml:lang="fr">vin</rdfs:label>
</owl:Class>
下面是对应的转化成三元组的owl.nt文件的部分内容:
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/2000/01/rdf-schema#comment> "An example OWL ontology" .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/2002/07/owl#priorVersion> <http://www.example.org/wine-020303> .
<http://www.example.org/wine-020303> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Ontology> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/2002/07/owl#imports> <http://www.w3.org/2001/sw/WebOnt/guide-src/food.owl> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/2000/01/rdf-schema#comment> "Derived from the DAML Wine ontology at \n http://ontolingua.stanford.edu/doc/chimaera/ontologies/wines.daml\n Substantially changed, in particular the Region based relations." .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine> <http://www.w3.org/2000/01/rdf-schema#label> "Wine Ontology" .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/2001/sw/WebOnt/guide-src/food#PotableLiquid> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#subClassOf> _:node1c80be0kux1 .
_:node1c80be0kux1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:node1c80be0kux1 <http://www.w3.org/2002/07/owl#onProperty> <http://www.w3.org/2001/sw/WebOnt/guide-src/wine#hasMaker> .
_:node1c80be0kux1 <http://www.w3.org/2002/07/owl#cardinality> "1"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#subClassOf> _:node1c80be0kux2 .
_:node1c80be0kux2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:node1c80be0kux2 <http://www.w3.org/2002/07/owl#onProperty> <http://www.w3.org/2001/sw/WebOnt/guide-src/wine#hasMaker> .
_:node1c80be0kux2 <http://www.w3.org/2002/07/owl#allValuesFrom> <http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Winery> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#subClassOf> _:node1c80be0kux3 .
_:node1c80be0kux3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:node1c80be0kux3 <http://www.w3.org/2002/07/owl#onProperty> <http://www.w3.org/2001/sw/WebOnt/guide-src/wine#madeFromGrape> .
_:node1c80be0kux3 <http://www.w3.org/2002/07/owl#minCardinality> "1"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#subClassOf> _:node1c80be0kux4 .
_:node1c80be0kux4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:node1c80be0kux4 <http://www.w3.org/2002/07/owl#onProperty> <http://www.w3.org/2001/sw/WebOnt/guide-src/wine#hasSugar> .
_:node1c80be0kux4 <http://www.w3.org/2002/07/owl#cardinality> "1"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#subClassOf> _:node1c80be0kux5 .
_:node1c80be0kux5 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:node1c80be0kux5 <http://www.w3.org/2002/07/owl#onProperty> <http://www.w3.org/2001/sw/WebOnt/guide-src/wine#hasFlavor> .
_:node1c80be0kux5 <http://www.w3.org/2002/07/owl#cardinality> "1"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#subClassOf> _:node1c80be0kux6 .
_:node1c80be0kux6 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:node1c80be0kux6 <http://www.w3.org/2002/07/owl#onProperty> <http://www.w3.org/2001/sw/WebOnt/guide-src/wine#hasBody> .
_:node1c80be0kux6 <http://www.w3.org/2002/07/owl#cardinality> "1"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#subClassOf> _:node1c80be0kux7 .
_:node1c80be0kux7 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:node1c80be0kux7 <http://www.w3.org/2002/07/owl#onProperty> <http://www.w3.org/2001/sw/WebOnt/guide-src/wine#hasColor> .
_:node1c80be0kux7 <http://www.w3.org/2002/07/owl#cardinality> "1"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#subClassOf> _:node1c80be0kux8 .
_:node1c80be0kux8 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Restriction> .
_:node1c80be0kux8 <http://www.w3.org/2002/07/owl#onProperty> <http://www.w3.org/2001/sw/WebOnt/guide-src/wine#locatedIn> .
_:node1c80be0kux8 <http://www.w3.org/2002/07/owl#someValuesFrom> <http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Region> .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#label> "wine"@en .
<http://www.w3.org/2001/sw/WebOnt/guide-src/wine#Wine> <http://www.w3.org/2000/01/rdf-schema#label> "vin"@fr .