将CSV转换为RDF,其中一列是一组值

将CSV转换为RDF,其中一列是一组值,csv,rdf,semantic-web,linked-data,sparql-generate,Csv,Rdf,Semantic Web,Linked Data,Sparql Generate,我想将CSV转换为RDF CSV的其中一列实际上是一组值,它们与分隔符(在我的例子中是空格符)连接在一起 以下是一个示例CSV(带标题): 我希望转换过程创建类似以下内容的RDF: :A :aProperty :B, :C, :D; :anotherProperty "John". :M :aProperty :X, :Y, :Z; :anotherProperty "Jack". 我通常使用Tarql进行CSV转换。 每行迭代即可。 但它没有在列值的“内部”进行子迭代的功能 SPARQL-G

我想将CSV转换为RDF

CSV的其中一列实际上是一组值,它们与分隔符(在我的例子中是空格符)连接在一起

以下是一个示例CSV(带标题):

我希望转换过程创建类似以下内容的RDF:

:A :aProperty :B, :C, :D; :anotherProperty "John".
:M :aProperty :X, :Y, :Z; :anotherProperty "Jack".
我通常使用Tarql进行CSV转换。
每行迭代即可。
但它没有在列值的“内部”进行子迭代的功能

SPARQL-Generate可能会有所帮助(据我所知,使用iter:regex和sub-Generate)。但是我找不到任何与我的用例相匹配的例子


附言:也许RML也能帮上忙。但我对这项技术一无所知。

您可以在操场上测试此查询,并检查其是否按预期运行:

BASE <http://data.example.com/> 
PREFIX : <http://example.com/> 
PREFIX iter: <http://w3id.org/sparql-generate/iter/>
PREFIX fun: <http://w3id.org/sparql-generate/fn/>

GENERATE { 
  <{?col1}> :anotherProperty ?col3.
  GENERATE{
      <{?col1}> :aProperty <{ ?value }> ; 
  }
  ITERATOR iter:Split( ?col2 , " " ) AS ?value .
}
ITERATOR iter:CSVStream("http://example.com/file.csv", 20, "*") AS ?col1 ?col2 ?col3
BASE
前缀:
国际热核试验堆前缀:
前缀乐趣:
生成{
:其他属性?col3。
产生{
:不动产;
}
迭代器iter:将(?col2,“”)拆分为?值。
}
迭代器iter:CSVStream(“http://example.com/file.csv,20,“*”)作为?col1?col2?col3
和相关规范针对这个用例,尽管我记得,我们没有提供组合
valueUrl
分隔符来让子列生成多个URI

描述这一点的元数据如下所示:

{
  "@context": "http://www.w3.org/ns/csvw",
  "url": "test.csv",
  "tableSchema": {
    "columns": [{
      "name": "col1",
      "titles": "col1",
      "datatype": "string",
      "required": true
    }, {
      "name": "col2",
      "titles": "col2",
      "datatype": "string",
      "separator": " "
    }, {
      "name": "col3",
      "titles": "col3",
      "datatype": "string",
      "propertyUrl": "http://example.com/anotherProperty",
      "valueUrl": "http://example.com/{col3}"
    }],
    "primaryKey": "col1",
    "aboutUrl": http://example.com/{col1}"
  }
}

您可以使用和来完成此操作

首先,我们需要访问可以使用RML完成的每一行。 RML允许您使用 . 指定(
rml:iterator
) 不需要,因为RML中的默认迭代器是基于行的迭代器。 这将导致以下RDF(海龟):

但是,需要首先拆分CSV列
col2
中的字符串。 这可以通过Fno(功能本体)和RML处理器实现 支持FnO函数的执行。这样的RML处理器可以是 ,但其他处理器可以 也可以使用。 需要以下RDF来调用FnO函数来拆分输入 字符串,空格作为分隔符,LogicalSource作为输入数据:

<#FunctionMap>
    fnml:functionValue [
        rml:logicalSource <#LogicalSource>; # our LogicalSource
        rr:predicateObjectMap [
            rr:predicate fno:executes; 
            rr:objectMap [ 
                rr:constant grel:string_split # function to use
            ];
        ];
        rr:predicateObjectMap [
            rr:predicate grel:valueParameter;
            rr:objectMap [ 
                rml:reference "col2" # input string
            ];
        ];
        rr:predicateObjectMap [
            rr:predicate grel:p_string_sep;
            rr:objectMap [ 
                rr:constant " "; # space separator
            ];
        ];
    ].

fnml:函数值[
rml:logicalSource;#我们的logicalSource
rr:谓词对象映射[
rr:谓词fno:执行;
rr:objectMap[
rr:常量grel:string_split#要使用的函数
];
];
rr:谓词对象映射[
rr:谓词grel:valueParameter;
rr:objectMap[
rml:引用“col2”#输入字符串
];
];
rr:谓词对象映射[
rr:谓词grel:p_string_sep;
rr:objectMap[
rr:常量“;#空格分隔符
];
];
].
RML映射器支持的FnO功能可在以下位置获得: 您可以在该页面上找到函数名及其参数

映射规则

@base <http://example.org> .
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix fnml: <http://semweb.mmlab.be/ns/fnml#> .
@prefix fno: <https://w3id.org/function/ontology#> .
@prefix grel: <http://users.ugent.be/~bjdmeest/function/grel.ttl#> .
@prefix ex: <http://example.org/ns#> .

<#LogicalSource>
    a rml:LogicalSource;
    rml:source "data.csv";
    rml:referenceFormulation ql:CSV.


<#MyTriplesMap>
    a rr:TriplesMap;
    rml:logicalSource <#LogicalSource>;

    rr:subjectMap [
        rr:template "http://example.org/{col1}";
    ];

    rr:predicateObjectMap [
        rr:predicate ex:aProperty;
        rr:objectMap <#FunctionMap>;
    ];

    rr:predicateObjectMap [
        rr:predicate ex:anotherProperty;
        rr:objectMap [
            rml:reference "col3";
        ];
    ].

<#FunctionMap>
    fnml:functionValue [
        rml:logicalSource <#LogicalSource>;
        rr:predicateObjectMap [
            rr:predicate fno:executes; 
            rr:objectMap [ 
                rr:constant grel:string_split 
            ];
        ];
        rr:predicateObjectMap [
            rr:predicate grel:valueParameter;
            rr:objectMap [ 
                rml:reference "col2" 
            ];
        ];
        rr:predicateObjectMap [
            rr:predicate grel:p_string_sep;
            rr:objectMap [ 
                rr:constant " ";
            ];
        ];
    ].

@base。
@前缀rml:。
@前缀rr:。
@前缀ql:。
@前缀rdf:。
@前缀fnml:。
@前缀fno:。
@前缀grel:。
@前缀ex:。
rml:逻辑源;
rml:源“data.csv”;
rml:referenceFormulation ql:CSV。
a rr:triplemap;
rml:逻辑源;
主题图[
rr:模板“http://example.org/{col1}”;
];
rr:谓词对象映射[
rr:谓词ex:aProperty;
rr:对象映射;
];
rr:谓词对象映射[
rr:谓词ex:另一个属性;
rr:objectMap[
rml:参考“col3”;
];
].
fnml:函数值[
rml:逻辑源;
rr:谓词对象映射[
rr:谓词fno:执行;
rr:objectMap[
rr:常量grel:字符串\u拆分
];
];
rr:谓词对象映射[
rr:谓词grel:valueParameter;
rr:objectMap[
rml:参考“col2”
];
];
rr:谓词对象映射[
rr:谓词grel:p_string_sep;
rr:objectMap[
rr:常数“;
];
];
].
输出

<http://example.org/A> <http://example.org/ns#aProperty> "B".
<http://example.org/A> <http://example.org/ns#aProperty> "C".
<http://example.org/A> <http://example.org/ns#aProperty> "D".
<http://example.org/A> <http://example.org/ns#anotherProperty> "John".
<http://example.org/M> <http://example.org/ns#aProperty> "X".
<http://example.org/M> <http://example.org/ns#aProperty> "Y".
<http://example.org/M> <http://example.org/ns#aProperty> "Z".
<http://example.org/M> <http://example.org/ns#anotherProperty> "Jack".
“B”。
“C”。
“D”。
“约翰”。
“X”。
“Y”。
“Z”。
“杰克”。
:我对RML及其技术做出了贡献

<http://example.org/A> <http://example.org/ns#anotherProperty> "John".
<#FunctionMap>
    fnml:functionValue [
        rml:logicalSource <#LogicalSource>; # our LogicalSource
        rr:predicateObjectMap [
            rr:predicate fno:executes; 
            rr:objectMap [ 
                rr:constant grel:string_split # function to use
            ];
        ];
        rr:predicateObjectMap [
            rr:predicate grel:valueParameter;
            rr:objectMap [ 
                rml:reference "col2" # input string
            ];
        ];
        rr:predicateObjectMap [
            rr:predicate grel:p_string_sep;
            rr:objectMap [ 
                rr:constant " "; # space separator
            ];
        ];
    ].
@base <http://example.org> .
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix fnml: <http://semweb.mmlab.be/ns/fnml#> .
@prefix fno: <https://w3id.org/function/ontology#> .
@prefix grel: <http://users.ugent.be/~bjdmeest/function/grel.ttl#> .
@prefix ex: <http://example.org/ns#> .

<#LogicalSource>
    a rml:LogicalSource;
    rml:source "data.csv";
    rml:referenceFormulation ql:CSV.


<#MyTriplesMap>
    a rr:TriplesMap;
    rml:logicalSource <#LogicalSource>;

    rr:subjectMap [
        rr:template "http://example.org/{col1}";
    ];

    rr:predicateObjectMap [
        rr:predicate ex:aProperty;
        rr:objectMap <#FunctionMap>;
    ];

    rr:predicateObjectMap [
        rr:predicate ex:anotherProperty;
        rr:objectMap [
            rml:reference "col3";
        ];
    ].

<#FunctionMap>
    fnml:functionValue [
        rml:logicalSource <#LogicalSource>;
        rr:predicateObjectMap [
            rr:predicate fno:executes; 
            rr:objectMap [ 
                rr:constant grel:string_split 
            ];
        ];
        rr:predicateObjectMap [
            rr:predicate grel:valueParameter;
            rr:objectMap [ 
                rml:reference "col2" 
            ];
        ];
        rr:predicateObjectMap [
            rr:predicate grel:p_string_sep;
            rr:objectMap [ 
                rr:constant " ";
            ];
        ];
    ].

<http://example.org/A> <http://example.org/ns#aProperty> "B".
<http://example.org/A> <http://example.org/ns#aProperty> "C".
<http://example.org/A> <http://example.org/ns#aProperty> "D".
<http://example.org/A> <http://example.org/ns#anotherProperty> "John".
<http://example.org/M> <http://example.org/ns#aProperty> "X".
<http://example.org/M> <http://example.org/ns#aProperty> "Y".
<http://example.org/M> <http://example.org/ns#aProperty> "Z".
<http://example.org/M> <http://example.org/ns#anotherProperty> "Jack".