Sparql skos:更广泛

发布于 2024-09-10 15:30:36 字数 341 浏览 11 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

友谊不毕业 2024-09-17 15:30:36

使用 SPARQL 1.1 中的属性路径可以使这变得更容易,

SELECT DISTINCT ( ?person )
WHERE
{
  ?person rdf:type dbpedia-owl:MusicalArtist .
  ?person skos:subject  skos:broader* category:American_musicians  .
}

这里显示了可以通过 skos:broader 属性访问的所有祖先。

This can be made easier with property paths in SPARQL 1.1

SELECT DISTINCT ( ?person )
WHERE
{
  ?person rdf:type dbpedia-owl:MusicalArtist .
  ?person skos:subject  skos:broader* category:American_musicians  .
}

Here it displays all the ancestors that could be reached via the skos:broader property.

东北女汉子 2024-09-17 15:30:36

我很惊讶这个简单的问题三年来都没有得到正确的答案,人们传播了多少不确定性和怀疑。

选择 * {
?person a dbo:音乐艺术家。
过滤器存在 {?person dct:subject/skos:broader* dbc:American_musicians}
}

  • 更正了一些前缀:dbo 代替了长的 dbpedia-owldbc 代替了 category 。这些短前缀是 DBpedia 内置的,
  • 已将 skos:subject (不存在此类属性)更正为 dct:subject
  • 使用属性路径更正了查询,缺少 /< /code>
  • skos:broader 不是传递性的,skos:broaderTransitive 是传递性的。然而,DBpedia 没有用后者(没有传递推理)
  • 来替换昂贵的 DISTINCT 和速度更快的 FILTER EXISTSFILTER 可以停在它找到的第一个相关子类别,而原始查询首先找到每个艺术家的所有此类子类别,然后丢弃它们(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}
}

  • corrected a few prefixes: dbo instead of the long dbpedia-owl, dbc instead of category. These short prefixes are builtin to DBpedia
  • corrected skos:subject (no such prop exists) to dct:subject
  • corrected the query with property paths, it was missing /
  • skos:broader is not transitive, skos:broaderTransitive is. However, DBpedia doesn't have the latter (no transitive reasoning)
  • replaced DISTINCT which is expensive with FILTER EXISTS which is much faster. The FILTER 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.
︶葆Ⅱㄣ 2024-09-17 15:30:36

没有真正好的方法可以做到这一点,但这里有一个详细的方法:

SELECT DISTINCT ( ?person )
WHERE
{
  ?person rdf:type dbpedia-owl:MusicalArtist .
  {
    ?person skos:subject [ skos:broader category:American_musicians ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader category:American_musicians ] ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader [ skos:broader category:American_musicians ] ] ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader [ skos:broader [ skos:broader category:American_musicians ] ] ] ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader category:American_musicians ] ] ] ] ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader category:American_musicians ] ] ] ] ] ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader category:American_musicians ] ] ] ] ] ] ] .
  }
}

为了弄清楚您需要多少级别,您可以将 SELECT DISTINCT 更改为 SELECT COUNT DISTINCT 并在计数停止增加时停止添加级别。

There's no really good way to do this, but here's a verbose way:

SELECT DISTINCT ( ?person )
WHERE
{
  ?person rdf:type dbpedia-owl:MusicalArtist .
  {
    ?person skos:subject [ skos:broader category:American_musicians ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader category:American_musicians ] ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader [ skos:broader category:American_musicians ] ] ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader [ skos:broader [ skos:broader category:American_musicians ] ] ] ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader category:American_musicians ] ] ] ] ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader category:American_musicians ] ] ] ] ] ] .
  } UNION {
    ?person skos:subject [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader [ skos:broader category:American_musicians ] ] ] ] ] ] ] .
  }
}

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.

夜夜流光相皎洁 2024-09-17 15:30:36

这在 neo4j 中非常容易执行。在 SPARQL 中完成任务的另一种方法是通过子类别上的代码进行迭代来提取“Category:American_musicians”下的所有子图。

例如。 java中的伪代码类似于:

String startCategory = "<http://dbpedia.org/resource/Category:American_musicians>";
iterateTraversalFunction(startCategory);

那么遍历函数将是:

public void iterateTraversalFunction(String startCategory){
     ArrayList<String> artistsURI = // SPARQL query ?person skos:subject startCategory . ?person rdf:type MusicalArtist 

    ArrayList<String> subCategoriesURI = // SPARQL query ?subCat skos startCategory
    // Repeat recursively
   for(String subCatURI: subCategoriesURI){
       iterateTraversalFunction(subCatURI);
   }
}

希望这有帮助,
- 丹

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:

String startCategory = "<http://dbpedia.org/resource/Category:American_musicians>";
iterateTraversalFunction(startCategory);

then the traversal function would be:

public void iterateTraversalFunction(String startCategory){
     ArrayList<String> artistsURI = // SPARQL query ?person skos:subject startCategory . ?person rdf:type MusicalArtist 

    ArrayList<String> subCategoriesURI = // SPARQL query ?subCat skos startCategory
    // Repeat recursively
   for(String subCatURI: subCategoriesURI){
       iterateTraversalFunction(subCatURI);
   }
}

Hope this helps,
- Dan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文