gpt4 book ai didi

RDF 在一行中列出主题及其对象

转载 作者:行者123 更新时间:2023-12-02 00:05:46 26 4
gpt4 key购买 nike

我有一个 RDF 文件,我需要从中提取一些信息并将其写入文件。我了解它的基本工作原理,但我坚持这样做:

String queryString = "select ?person ?children where { ?person ?hasChildren ?children}";
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
TupleQueryResult result = tupleQuery.evaluate();
while (result.hasNext()) {
BindingSet bindingSet = result.next();
Value p1 = bindingSet.getValue("person");
Value p2 = bindingSet.getValue("child");
println(p1 + " has children " + p2 +"");
}
result.close();

我得到的输出是这样的:

http://example.org/people/person1 has children http://example.org/people/child1
http://example.org/people/person1 has children http://example.org/people/child2

我看不出如何以这种格式列出所有人及其对象:

person1 has children child1 and child2

如何做到这一点?

最佳答案

您可能会发现这个描述 SPARQL 的 group_concat 的答案很有用:

在 SPARQL 中,当您有查询解决方案的结果集时,您可以对一个或多个变量进行分组,合并具有这些共同变量的解决方案。例如,考虑数据

@prefix : <http://example.org/people/>.

:person1 :hasChild :child1, :child2, :child3 .
:person2 :hasChild :child4, :child5 .
:person3 :hasChild :child6 .

如果你在上面运行下面的查询

prefix : <http://example.org/people/>

select ?person ?child where {
?person :hasChild ?child .
}

你得到这样的结果:

$ arq --data data.n3 --query query.sparql
----------------------
| person | child |
======================
| :person3 | :child6 |
| :person2 | :child5 |
| :person2 | :child4 |
| :person1 | :child3 |
| :person1 | :child2 |
| :person1 | :child1 |
----------------------

迭代问题中的结果将产生您当前获得的输出类型。我们想要做的是实际获得如下结果:

$ arq --data data.n3 --query query.sparql
----------------------------------------
| person | child |
========================================
| :person3 | :child6 |
| :person2 | :child4, :child5 |
| :person1 | :child1, :child2, :child3 |
----------------------------------------

而这正是 group_by 让我们做的。像这样的查询:

prefix : <http://example.org/people/>

select ?person (group_concat(?child;separator=' and ') as ?children) where {
?person :hasChild ?child .
}
group by ?person

产生(注意结果中的变量是 ?children,而不是 ?child,因为我们使用 group_concat(...) 作为 ? children 创建新变量 ?children):

$ arq --data data.n3 --query query.sparql
---------------------------------------------------------------------------------------------------------------------------
| person | children |
===========================================================================================================================
| :person3 | "http://example.org/people/child6" |
| :person1 | "http://example.org/people/child3 and http://example.org/people/child2 and http://example.org/people/child1" |
| :person2 | "http://example.org/people/child5 and http://example.org/people/child4" |
---------------------------------------------------------------------------------------------------------------------------

如果您使用这样的查询并遍历结果,按您的方式打印它们,您将获得您想要的输出。如果您确实想从人员和 child 中去除领先的 http://example.org/people/,您将需要更多的字符串处理。例如,使用 STRAFTER要删除 http://example.org/people/ 前缀,您可以使用如下查询:

prefix : <http://example.org/people/>

select
(strafter(str(?personX),"http://example.org/people/") as ?person)
(group_concat(strafter(str(?child),"http://example.org/people/");separator=' and ') as ?children)
where {
?personX :hasChild ?child .
}
group by ?personX

得到如下结果:

$ arq --data data.n3 --query query.sparql
----------------------------------------------
| person | children |
==============================================
| "person3" | "child6" |
| "person2" | "child5 and child4" |
| "person1" | "child3 and child2 and child1" |
----------------------------------------------

当你进行打印时,它会给你这样的结果

person3 has children child6
person2 has children child5 and child4
person1 has children child3 and child2 and child1

关于RDF 在一行中列出主题及其对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18485461/

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