gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:29:16 28 4
gpt4 key购买 nike

我想将 CSV 转换为 RDF。

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

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

col1,col2,col3
"A","B C D","John"
"M","X Y Z","Jack"

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

:A :aProperty :B, :C, :D; :anotherProperty "John".
:M :aProperty :X, :Y, :Z; :anotherProperty "Jack".

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

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

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

最佳答案

您可以通过 RML 完成此操作和 FnO .

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

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

实际上三元组是在 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";
];
].

col3 CSV 列用于创建以下三元组:

<http://example.org/A> <http://example.org/ns#anotherProperty> "John".

但是,CSV 列 col2 中的字符串需要先拆分。这可以通过 Fno(功能本体)和 RML 处理器来实现支持FnO函数的执行。这种 RML 处理器可以是 RML Mapper , 但其他处理器可以也可以使用。调用拆分输入的 FnO 函数需要以下 RDF以空格作为分隔符的字符串,我们的 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
];
];
].

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

输出

<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".

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

关于csv - 将 CSV 转换为 RDF,其中一列是一组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53715353/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com