【问题标题】:How to use GroupBy and GroupConcat in SPARQL如何在 SPARQL 中使用 GroupBy 和 GroupConcat
【发布时间】:2020-06-07 16:33:44
【问题描述】:

我在使用 SPARQL 查询对项目进行分组时遇到问题。我正在使用http://kaiko.getalp.org/sparql 来编写我的查询。

我在一个字段上使用了 GROUP BY。由于有多个项目,我的结果是多行,我试图将它们连接起来。

这是我的原始查询。

SELECT distinct ?word ?poslexinfo ?posdbnary ?def ?syn ?ant
WHERE { 
 ?f dbnary:describes ?lf. 
 ?lf rdf:type ontolex:LexicalEntry .
 ?lf lime:language "de" . 
 ?lf ontolex:canonicalForm ?wo .
 ?wo ontolex:writtenRep ?word .
 OPTIONAL {?lf lexinfo:partOfSpeech ?poslexinfo} .
 OPTIONAL {?lf dbnary:partOfSpeech ?posdbnary} .
 ?lf ontolex:sense ?os .
 ?os rdf:type ontolex:LexicalSense .
 OPTIONAL {?os dbnary:antonym ?ant} .
 OPTIONAL {?os dbnary:synonym ?syn} .
 ?os skos:definition ?defl .
 ?defl rdf:value ?def
 } LIMIT 5

结果:

word    poslexinfo  posdbnary   def     syn     ant

"gay"   http://www.lexinfo.net/ontology/2.0/lexinfo#adjective   "Adjektiv"   "sexuelle Neigungen zum eigenen Geschlecht zeigend" http://kaiko.getalp.org/dbnary/deu/homosexuell http://kaiko.getalp.org/dbnary/deu/hetero

"gay"   http://www.lexinfo.net/ontology/2.0/lexinfo#adjective   "Adjektiv"   "sexuelle Neigungen zum eigenen Geschlecht zeigend" http://kaiko.getalp.org/dbnary/deu/homosexuell http://kaiko.getalp.org/dbnary/deu/bi

这两行仅在反义词上有所不同,我的预期输出是,

word    poslexinfo  posdbnary   def     syn     ant

"gay"   http://www.lexinfo.net/ontology/2.0/lexinfo#adjective   "Adjektiv"   "sexuelle Neigungen zum eigenen Geschlecht zeigend" http://kaiko.getalp.org/dbnary/deu/homosexuell http://kaiko.getalp.org/dbnary/deu/hetero|http://kaiko.getalp.org/dbnary/deu/bi

我使用 GroupBy 和 GroupConcat 尝试了以下查询,但不确定我在这里做错了什么。

查询:

SELECT distinct ?word ?poslexinfo ?posdbnary ?def ?syn (GROUP_CONCAT(?ant ; separator="|") AS ?ants)
WHERE { 
 ?f dbnary:describes ?lf. 
 ?lf rdf:type ontolex:LexicalEntry .
 ?lf lime:language "de" . 
 ?lf ontolex:canonicalForm ?wo .
 ?wo ontolex:writtenRep ?word .
 OPTIONAL {?lf lexinfo:partOfSpeech ?poslexinfo} .
 OPTIONAL {?lf dbnary:partOfSpeech ?posdbnary} .
 ?lf ontolex:sense ?os .
 ?os rdf:type ontolex:LexicalSense .
 OPTIONAL {?os dbnary:antonym ?ant} .
 OPTIONAL {?os dbnary:synonym ?syn} .
 ?os skos:definition ?defl .
 ?defl rdf:value ?def
 } GROUP BY ?word LIMIT 5

当我尝试上述查询时,我收到此错误。

Virtuoso 37000 错误 SP031:SPARQL 编译器:变量 ?syn 用于 结果集在聚合之外且未在 GROUP BY 子句中提及

【问题讨论】:

  • 您只是按word 分组 - 现在回到您的示例,三重存储为每个单词建立一个组,但现在三重存储如何知道它与所有单词有什么关系其他功能。您为ant 列执行了此操作,但通常每列可以有多个值 - 因此您也必须为这些值做类似的事情。例如使用group_concat。显然这是错误的,你还应该按词义或其他东西来区分同一个词的不同语义。

标签: sql group-by sparql group-concat


【解决方案1】:

感谢@Jeen Broekstra!这里的建议有助于解决问题https://stackoverflow.com/a/34033146/73137

SELECT ?word ?poslexinfo ?def (GROUP_CONCAT(?syn ; separator="||") AS ?syns) (GROUP_CONCAT(?ant ; separator="||") AS ?ants)
                WHERE {
                    ?f dbnary:describes ?lf.
                    ?lf rdf:type ontolex:LexicalEntry .
                    ?lf lime:language "de" .
                    ?lf ontolex:canonicalForm ?wo .
                    ?wo ontolex:writtenRep ?word .
                    OPTIONAL {?lf lexinfo:partOfSpeech ?poslexinfo} .
                    OPTIONAL {?lf dbnary:partOfSpeech ?posdbnary} .
                    ?lf ontolex:sense ?os .
                    ?os rdf:type ontolex:LexicalSense .
                    OPTIONAL {?os dbnary:antonym ?ant} .
                    OPTIONAL {?os dbnary:synonym ?syn} .
                    ?os skos:definition ?defl .
                    ?defl rdf:value ?def
                } GROUP BY ?word ?syn ?def ?posdbnary ?poslexinfo LIMIT 20

【讨论】:

    猜你喜欢
    • 2022-10-12
    • 1970-01-01
    • 2021-07-29
    • 2013-11-07
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多