turtle教程

Turtle格式介紹

在網上找了很久,就是沒有介紹turtle格式標準的中文教程。

所以就照着英文版的turtle格式說明,加上自己的理解,寫了這篇blog.

一般是用RDF(Resource Description Framework)模型,對知識圖譜進行描述。

目前有普遍的三種序列化RDF的標準RDF/XML,Turtle,N-Triples

Turtle由於簡單易讀,易於編寫,成爲常見的序列化RDF數據模型的標準

下面將詳細介紹Turtle的格式要求(閱讀本文前,建議先學習RDF/XML格式標準)

  • URI用 <> 描述

    <http://example.org/path/>
    <http://example.org/path/#fragment>
    
  • 前綴縮寫(類似於RDF/XML的命名空間)

    第二行定義了缺省前綴,之後應用可以用**:a**相當於 < http://example.org/ns1#a >

    @prefix foo:<http://example.org/ns#>
    @prefix  : <http://example.org/ns1#>
    :a :b :c
    
  • 字面量

    一行或者多行, @en限定其語言, ^^xsd:decimal限定其數據類型

    "string"
    """
    many lines of string
    many lines of string
    many lines of string
    """
    "chat"@en
    "chat"@fr
    "10"^^xsd:decimal
    
  • 空節點(RDF模型可能會存在未命名的空節點)

    _:me,_a1234分別代表一個空節點

    _:me
    _:a1234
    
  • base URI(這個感覺有點難理解)

    base URI定義後,接下來的URI, 前綴縮寫,qualified names 和base URI都要受其作用。

    # this is a complete turtle document
    
    @base <http://example.org/ns/> .
    # base URIs 是 http://example.org/ns/
    
    @base <foo/> .
    # base URI 是 http://example.org/ns/foo/
    
    @prefix : <bar#> .
    :a4 :b4 :c4.
    
  • 對三元組進行縮寫

    :a :b :c,
          :d.
    #the last triple is :a :b :d.
    
    :a :b :c;
       :d :e.
    #the last triple is :a :d :e.
    
  • 整數,單精度,雙精度,科學計數法,布爾值都能被直接表示

    -5
    0
    +1
    1.3e2
    -12.001
    true 
    false
    
  • 集合的縮寫2

    (Subject爲主語, predicate爲謂語)

    @prefix : <http://example.org/foo> .
    # the value of this triple is the RDF collection blank node
    :subject :predicate ( :a : b : c ) .
    
    # an empty collection value - rdf:nil
    :subject :predicate2 () .
    
  • 一個簡單的完整turtle標準文件

    下圖是一個知識圖譜的rdf描述模型

    對rdf graph進行turtle標準的序列化

@prefix info: <http://zy.example.com/info#>
@prefix rel: <http://zy.example.com/rel#>
@prefix person: <http://zy.example.com/person#>

person:Tom info:name "Tom";
   	   info:job "worker";
   	   info:age 56;
   	   rel:fatherof person:Jim.
   	   
person:Jim info:name "Jim";
   	   info:job "programmer";
   	   info:age 28;
   	   rel:fatherof person:Cherry.

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