介紹一個好用的jar包:可將owl文件轉爲三元組

一 背景知識

知識圖譜方向目前越來越火,順帶着,表示知識圖譜的語言,如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 .

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章