Sparql skos:更广泛
我正在 DBpediaset 上进行 SPARQL 查询,但我遇到了一些查询限制问题(由于缺乏详细的 SPARQL 知识):
我首先“获取”所有音乐艺术家:
?person rdf:type <http://dbpedia.org/ontology/MusicalArtist> .
但我想将其限制为更广泛的类别Category:American_musicians
(通过遍历skos:broader
?):如何?
*= 虽然问题很具体,但我在想要运行 sparql 查询时多次遇到此任务。
I'm doing a SPARQL query on the DBpediaset, but I am having some issues (due to lack of detailed SPARQL knowledge) with a query limitation:
I first 'get' all music artists:
?person rdf:type <http://dbpedia.org/ontology/MusicalArtist> .
But I want to limit this to the broader category Category:American_musicians
(via traversing skos:broader
?): how?
*= while the question is specific, I've encountered this quest many times when wanting to running sparql queries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 SPARQL 1.1 中的属性路径可以使这变得更容易,
这里显示了可以通过 skos:broader 属性访问的所有祖先。
This can be made easier with property paths in SPARQL 1.1
Here it displays all the ancestors that could be reached via the
skos:broader
property.我很惊讶这个简单的问题三年来都没有得到正确的答案,人们传播了多少不确定性和怀疑。
选择 * {
?person a dbo:音乐艺术家。
过滤器存在 {?person dct:subject/skos:broader* dbc:American_musicians}
}
dbo
代替了长的dbpedia-owl
,dbc
代替了category
。这些短前缀是 DBpedia 内置的,skos:subject
(不存在此类属性)更正为dct:subject
/< /code>
skos:broader
不是传递性的,skos:broaderTransitive
是传递性的。然而,DBpedia 没有用后者(没有传递推理)DISTINCT
和速度更快的FILTER EXISTS
。FILTER
可以停在它找到的第一个相关子类别,而原始查询首先找到每个艺术家的所有此类子类别,然后丢弃它们(DISTINCT
),排序记忆中的艺术家并删除重复项。I'm amazed this simple question hasn't been answered correctly in 3 years, and how much uncertainty and doubt people spread.
SELECT * {
?person a dbo:MusicalArtist .
filter exists {?person dct:subject/skos:broader* dbc:American_musicians}
}
dbo
instead of the longdbpedia-owl
,dbc
instead ofcategory
. These short prefixes are builtin to DBpediaskos:subject
(no such prop exists) todct:subject
/
skos:broader
is not transitive,skos:broaderTransitive
is. However, DBpedia doesn't have the latter (no transitive reasoning)DISTINCT
which is expensive withFILTER EXISTS
which is much faster. TheFILTER
can stop at the first relevant sub-category it finds, while the original query first finds all such sub-cats per artist, then discards them (DISTINCT
), sorts the artists in memory and removes duplicates.没有真正好的方法可以做到这一点,但这里有一个详细的方法:
为了弄清楚您需要多少级别,您可以将 SELECT DISTINCT 更改为 SELECT COUNT DISTINCT 并在计数停止增加时停止添加级别。
There's no really good way to do this, but here's a verbose way:
For figuring out how many levels you need, you can change SELECT DISTINCT to SELECT COUNT DISTINCT and stop adding levels when the count stops going up.
这在 neo4j 中非常容易执行。在 SPARQL 中完成任务的另一种方法是通过子类别上的代码进行迭代来提取“Category:American_musicians”下的所有子图。
例如。 java中的伪代码类似于:
那么遍历函数将是:
希望这有帮助,
- 丹
This is really easy to perform in neo4j. An alternative to accomplish your task in SPARQL could be to extract all the subgraph under "Category:American_musicians" by iterating via code on subcategories.
Eg. pseudo code in java would be something like:
then the traversal function would be:
Hope this helps,
- Dan