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

lOl*_*ive 3 csv rdf semantic-web linked-data sparql-generate

我想将 CSV 转换为 RDF。

事实上,该 CSV 的一列是一组用分隔符(在我的例子中是空格字符)连接的值。

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

col1,col2,col3
"A","B C D","John"
"M","X Y Z","Jack"
Run Code Online (Sandbox Code Playgroud)

我希望转换过程创建一个类似于这样的 RDF:

:A :aProperty :B, :C, :D; :anotherProperty "John".
:M :aProperty :X, :Y, :Z; :anotherProperty "Jack".
Run Code Online (Sandbox Code Playgroud)

我通常使用 Tarql 进行 CSV 转换。
每行迭代都可以。
但是它没有在列值“内部”进行子迭代的功能。

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

PS:可能 RML 也可以提供帮助。但我对这项技术一无所知。

Dyl*_*che 5

您可以使用RMLFnO完成此操作

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

<#LogicalSource>
    a rml:LogicalSource;
    rml:source "data.csv";
    rml:referenceFormulation ql:CSV.
Run Code Online (Sandbox Code Playgroud)

实际上的三元组是在TriplesMap的帮助下生成的,它使用 LogicalSource 从每个 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";
        ];
    ].

Run Code Online (Sandbox Code Playgroud)

col3CSV列被用来创建以下三重:

<http://example.org/A> <http://example.org/ns#anotherProperty> "John".
Run Code Online (Sandbox Code Playgroud)

但是,col2需要先拆分CSV 列中的字符串。这可以通过 Fno(函数本体)和支持 FnO 函数执行的 RML 处理器来实现。这种 RML 处理器可以是 RML Mapper,但也可以使用其他处理器。需要以下 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
            ];
        ];
    ].
Run Code Online (Sandbox Code Playgroud)

RML 映射器支持的 FnO 函数可在此处获得:https ://rml.io/docs/rmlmapper/default-functions/ 您可以在该页面上找到函数名称及其参数。

映射规则

@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 " ";
            ];
        ];
    ].

Run Code Online (Sandbox Code Playgroud)

输出

<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".
Run Code Online (Sandbox Code Playgroud)

注意:我为 RML 及其技术做出了贡献。