计算 SPARQL 中的个体数量 [英] Counting number of individuals in SPARQL

查看:36
本文介绍了计算 SPARQL 中的个体数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 SPARQL 完全陌生.

I am totally new to SPARQL.

我想算一下这个本体中的actor数量:http://data.linkedmdb.org/directory/actor

I would like to count the number of actors in this ontology : http://data.linkedmdb.org/directory/actor

我尝试了以下方法:

SELECT ?s (COUNT(*) AS ?count)
WHERE
   {
       ?a <http://data.linkedmdb.org/directory/actor> ?s}
 GROUP BY ?s

但我相信这不是正确的语法,因为它给了我 0 个结果..我知道在那个数据源中有几个结果!会不会是链接不对?

But i believe that's not the right syntax for it because it gives me 0 results.. Where I know there are several results in that data source! Could it be that the link is not the correct one?

推荐答案

计算每部电影的演员

在问题的最初表述中,您似乎试图计算每部电影的演员数.查询实际上与此非常接近,但我不确定您从何处获得属性 URI,但这是不对的.例如,如果您查看 http://data.linkedmdb.org/page/film/1 并右键单击 movie:actor 属性,您可以看到其 URI 为 http://data.linkedmdb.org/resource/movie/actor.因此,您的查询可能是:

Counting actors per film

In the original formulation of the question, it appears that you're trying to count actors per film. The query is actually very close to that, but I'm not sure where you got the property URI from, but it's not right. E.g., if you look at http://data.linkedmdb.org/page/film/1 and right click on the movie:actor property, you can see that its URI is http://data.linkedmdb.org/resource/movie/actor. Thus, your query could be:

SELECT ?film (count(*) as ?nActors) WHERE {
  ?film <http://data.linkedmdb.org/resource/movie/actor> ?actor .
}
group by ?film
limit 10

计算演员

现在,您可以修改此查询以通过运行相同的三元组模式来计算演员数,但不是按电影分组并计算演员数,只需计算所有电影中不同的演员数:

Counting actors

Now, you could modify this query to count actors, by running the same triple pattern, but instead of grouping by the film and counting actors, just count distinct actors over all the films:

select(count(distinct ?actor) as ?nActors) where {
  [] <http://data.linkedmdb.org/resource/movie/actor> ?actor .
}

现在,这似乎提供了 162 的答案,这似乎相当低,但在其他问题中,我们已经看到 LinkedMDB 的端点有一些奇怪的限制.可能是因为这些,但这也不是我们计算演员的唯一方法.您可以通过查看演员的页面来注意,例如,http://data.linkedmdb.org/page/actor/10,每个演员都有 rdf:type http://data.linkedmdb.org/resource/movie/actor,这意味着你可以只要求和计算那种类型的东西:

Now, that seems to provide an answer of 162, which seems rather low, but in other questions we've seen that LinkedMDB's endpoint has some strange limitations. It might be because of those, but this isn't the only way that we can count actors, either. You can note by looking at an actor's page, e.g., http://data.linkedmdb.org/page/actor/10, that each actor has the rdf:type http://data.linkedmdb.org/resource/movie/actor, which means that you can just ask for and count things with that type:

select(count(distinct ?actor) as ?nActors) where {
  ?actor a <http://data.linkedmdb.org/resource/movie/actor> .
}

该查询返回 2500,这似乎更合适(因为它可能在端点中达到 2500 的限制).

That query returns 2500, which seems more appropriate (since it's probably hitting a limit of 2500 in the endpoint).

这篇关于计算 SPARQL 中的个体数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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