SPAQRL:选择项目并计算其标签的出现次数

发布于 2025-01-10 17:07:54 字数 1692 浏览 4 评论 0 原文

我有<一个href="https://www.orkg.org/orkg/triplestore?query=%0APREFIX%20orkgr%3A%20%3Chttp%3A%2F%2Forkg.org%2F orkg%2Fresource%2F%3E%0APREFIX%20orkgc%3A%20%3Chttp%3A%2F%2Forkg.org%2Forkg%2Fclass%2F%3E%0APREFIX%20 orkgp%3A%20%3Chttp%3A%2F%2Forkg.org%2Forkg%2Fpredicate%2F%3E%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fww w.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX%20xsd%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSc hema%23%3E%0APREFIX%20rdf%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%3E%0A%0A SELECT%20%3Fo1Label%20(COUNT(%3Fo1Label)%20AS%20%3Fo1LabelCount)%0AWHERE%20%7B%0A%20%20%3Fo1%20a%20或kgc%3APaper.%0A%20%20%3Fo1%20rdfs%3Alabel%20%3Fo1Label.%0A%20%20%0A%20%20FILTER%20(strlen(%3Fo1Label )%20%3E%201).%0A%20%20%0A%7D%0A%0AGROUP%20BY%20%3Fo1Label%0AORDER%20BY%20DESC(%3Fo1LabelCount)%0A%0A" rel="nofollow noreferrer">此 SPARQL 查询 定向到开放研究知识图 (ORKG):

PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?o1Label (COUNT(?o1Label) AS ?o1LabelCount)
WHERE {
  ?o1 a orkgc:Paper.
  ?o1 rdfs:label ?o1Label.
  
  FILTER (strlen(?o1Label) > 1).
}

GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)

结果是标签 (?o1Label) 以及该标签出现的次数 (<代码>?o1LabelCount)。

如何扩展此查询以包含实际项目的列 (?o1)?

由于可能有多个候选项(当 o1LabelCount > 1 时),因此每个项目都应占一行(具有相同的标签和相同的标签计数)。

I have this SPARQL query directed to the Open Research Knowledge Graph (ORKG):

PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?o1Label (COUNT(?o1Label) AS ?o1LabelCount)
WHERE {
  ?o1 a orkgc:Paper.
  ?o1 rdfs:label ?o1Label.
  
  FILTER (strlen(?o1Label) > 1).
}

GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)

Which results in labels (?o1Label) and the number of occurrences of this label (?o1LabelCount).

How can I extend this query to also include a column for the actual item (?o1)?

Because there might be multiple candidates (when o1LabelCount is > 1), there should be one row for each of these items (with the same label and the same label count).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

神回复 2025-01-17 17:07:54

我看到两个选择:

第一个(可能更好)是使用 GROUP_CONCAT< /code> 并将实体收集到一个字段中,以便在应用程序端再次解析。这可能看起来像这样(链接):

PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?o1Label (GROUP_CONCAT(?o1, "\t") AS ?o1s) (COUNT(?o1Label) AS ?o1LabelCount)
WHERE {
  ?o1 a orkgc:Paper.
  ?o1 rdfs:label ?o1Label.
  
  FILTER (strlen(?o1Label) > 1).
}

GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)

另一种方法是使用 嵌套查询并收到您所描述的结果(链接):

PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?o1Label ?o1 ?o1LabelCount
WHERE {
  ?o1 rdfs:label ?o1Label .

  {
    SELECT ?o1Label (COUNT(?o1Label) AS ?o1LabelCount)
    WHERE {
      [
        a orkgc:Paper;
        rdfs:label ?o1Label
      ]
      FILTER (strlen(?o1Label) > 1).
    }
  }
}

GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)

I see two options:

First (and probably better) is to use GROUP_CONCAT and collect the entities into one field to be parsed again on application side. this could look like this (link):

PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?o1Label (GROUP_CONCAT(?o1, "\t") AS ?o1s) (COUNT(?o1Label) AS ?o1LabelCount)
WHERE {
  ?o1 a orkgc:Paper.
  ?o1 rdfs:label ?o1Label.
  
  FILTER (strlen(?o1Label) > 1).
}

GROUP BY ?o1Label
ORDER BY DESC(?o1LabelCount)

An alternative would be using nested queries and receive a result as you described (link):

PREFIX orkgr: <http://orkg.org/orkg/resource/>
PREFIX orkgc: <http://orkg.org/orkg/class/>
PREFIX orkgp: <http://orkg.org/orkg/predicate/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?o1Label ?o1 ?o1LabelCount
WHERE {
  ?o1 rdfs:label ?o1Label .

  {
    SELECT ?o1Label (COUNT(?o1Label) AS ?o1LabelCount)
    WHERE {
      [
        a orkgc:Paper;
        rdfs:label ?o1Label
      ]
      FILTER (strlen(?o1Label) > 1).
    }
  }
}

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