如何返回单个 foaf:name? [英] How to return a single foaf:name?

查看:54
本文介绍了如何返回单个 foaf:name?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 DBpedia SPARQL,其中展示了影响哲学家的哲学家.但是,当哲学家有多个 foaf:name:

I have the following DBpedia SPARQL that shows philosophers that influenced philosophers. However, it returns duplicates when the philosopher has more than one foaf:name:

SELECT ?name ?influencedName
  WHERE {
  ?philosopher a dbpedia-owl:Philosopher ;
    dbpedia-owl:influenced ?influenced ;
    foaf:name ?name .
  ?influenced  a dbpedia-owl:Philosopher ;
    foaf:name ?influencedName .
}

SPARQL 结果

如果 ?name?includedName 有多个值,我如何返回单个名称.我会对第一个或最少数量的字符感到满意,以选择要保留的字符.

How can I return a single name if there are multiple values for ?name and ?includedName. I would be happy with the first one, or the least number of characters to pick which to keep.

这是柏拉图影响伯特兰·罗素的另一个例子.我希望它返回一行,但我得到四行:

Here is another example for just Plato influencing Bertrand Russell. I would like this to return one row, but I get four:

SELECT ?name ?influencedName
  WHERE {
  ?philosopher a dbpedia-owl:Philosopher ;
    dbpedia-owl:influenced ?influenced ;
    foaf:name ?name , "Plato"@en .
  ?influenced  a dbpedia-owl:Philosopher ;
    foaf:name ?influencedName, "Bertrand Arthur William Russell, 3rd Earl Russell"@en .
}

SPARQL 结果

推荐答案

查询

听起来您想要这样的查询:

The Queries

It sounds like you want a query something like:

SELECT ?philosopher ?pName ?influence (SAMPLE(?iName) as ?iName)
WHERE {
  # This subquery selects all the philosophers and
  # selects just one of their names . 
  {
    SELECT ?philosopher (SAMPLE(?pName) as ?pName) WHERE {
      ?philosopher a dbpedia-owl:Philosopher ;
                   foaf:name ?pName .
    }
    GROUP BY ?philosopher
  }

  # This main query selects the influence of the 
  # philosophers and select their names.  The GROUP
  # BY on the outer query puts all the
  # (?philosopher,?pName,?influence,?iName) tuples 
  # that have the same ?philosopher, ?pName, and 
  # influence together, and the (SAMPLE(?iName) as ?iName)
  # in the outer SELECT combines them all, choosing an 
  # arbitrary representative ?iName.
  ?influence dbpedia-owl:influenced ?philosopher ;
             a dbpedia-owl:Philosopher ;
             foaf:name ?iName .
}
GROUP BY ?philosopher ?pName ?influence

SPARQL 结果

如果你只关心名字,而不关心选择实际资源,你不需要在最外层的?philosopher?influence>SELECT 并且可以做到

If you are only interested in the names, and do not care about selecting the actual resources, you do not need ?philosopher and ?influence in the outermost SELECT and can make it

SELECT ?pName (SAMPLE(?iName) as ?iName)
WHERE { …

SPARQL 结果

您可能还想在末尾添加 ORDER BY 以使结果更容易检查:

You also might want to add an ORDER BY at the end to make the results a little bit easier to check:

…
GROUP BY ?philosopher ?pName ?influence
ORDER BY ?pName

SPARQL 结果

对于柏拉图,这些最后的结果包括以下几行:

These last results include, for Plato, the following rows:

"Plato"@en  "Socrates"@en
"Plato"@en  "Parmenides"@en
"Plato"@en  "Zeno of Elea"@en
"Plato"@en  "Pythagoras"@en
"Plato"@en  "Gorgias"@en
"Plato"@en  "Protagoras"@en
"Plato"@en  "Heraclitus"@en

在我在这里写的查询中,我使用了 SAMPLE 可以任意选择哲学家的 foaf:name 之一,但 聚合代数,您可以使用它来选择一个值.Min 如果您想要'first' 值.

In the query I've written here, I've used SAMPLE to pick one of the foaf:names of a philosopher arbitrarily, but there are other functions in the aggregate algebra that you can use to select a value. Min might be interest to you if you want the ‘first‘ value in order.

这实际上与 SPARQL 规范的第 12 节,子查询.在该示例中,以下查询用于选择 Alice 认识的人,对于每个人,只选择其中一个人的姓名:

This is actually very similar to the example given for subqueries in Section 12, Subqueries of the SPARQL specification. In that example, the following query is used to select the people that Alice knows, and for each one, select just one of the names of the people:

PREFIX : <http://people.example/>
SELECT ?y ?minName
WHERE {
  :alice :knows ?y .
  {
    SELECT ?y (MIN(?name) AS ?minName)
    WHERE {
      ?y :name ?name .
    } GROUP BY ?y
  }
}

这并不难适应哲学影响问题.哲学家问题首先选择所有哲学家及其姓名,按实际哲学家资源分组,然后使用样本为每个哲学家选择一个代表姓名.外部查询执行相同的操作,但不是选择哲学家,而是选择影响每个哲学家的实体.将结果分组并选择影响的代表名称.

This was not to hard to adapt for the philosophical influence problem. The philosophers problem began by selecting all the philosophers and their names, grouping by the actual philosopher resource, and picking a representative name for each philosopher using sample. The outer query does the same, but rather than selecting philosophers, it is selecting the entities that influenced each philosopher. The results are grouped and a representative name for the influence is selected.

这篇关于如何返回单个 foaf:name?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆