【问题标题】:SPARQL: Querying Wikidata labels for more than one languageSPARQL:查询多于一种语言的 Wikidata 标签
【发布时间】:2018-03-05 20:15:02
【问题描述】:

我正在尝试从 Wikidata 的 SPARQL 端点获取多种语言的标签。下面的例子给出here

SELECT ?country ?country_EN ?country_DE ?country_FR
   WHERE {
     ?country wdt:P31 wd:Q185441. # member state of the European Union
     SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
            ?country rdfs:label ?country_EN.
     }
     SERVICE wikibase:label { bd:serviceParam wikibase:language "de".
            ?country rdfs:label ?country_DE.
     }
     SERVICE wikibase:label { bd:serviceParam wikibase:language "fr".
            ?country rdfs:label ?country_FR.
     }
}

Try it here

但是,这会返回以下错误:

未知错误:任何组中只能有一个“最后运行”加入

有没有一种解决方案可以让标签使用一种以上的语言?

【问题讨论】:

    标签: sparql wikidata blazegraph


    【解决方案1】:

    rdfs:label可以不用wikibase:label服务直接使用:

    SELECT ?country ?country_en ?country_de ?country_fr
       WHERE {
         ?country wdt:P31 wd:Q185441. # member state of the European Union
         OPTIONAL {?country rdfs:label ?country_en filter (lang(?country_en) = "en")}.
         OPTIONAL {?country rdfs:label ?country_de filter (lang(?country_de) = "de")}.
         OPTIONAL {?country rdfs:label ?country_fr filter (lang(?country_fr) = "fr")}.
    }
    

    Try it here

    【讨论】:

    • 仅供参考,这不提供 Q-id 后备。
    • 什么是“Q-id fallback”?
    • 例如,Q29999 的立陶宛语标签缺失。标签服务将返回"Q29999"
    • SPARQL 1.1 COALESCE
    【解决方案2】:

    标签服务优化器 adds a hint:Prior hint:runLast true hint 到标签服务,除非有其他明确提示:

    LabelServiceUtils.getLabelServiceNodes(op).forEach(service -> {
        if (service.getProperty(QueryHints.RUN_LAST)  != null ||
            service.getProperty(QueryHints.RUN_FIRST) != null) {
            return;
        }
        service.setProperty(QueryHints.RUN_LAST, TRUE);
    });
    

    应该在第一个之后的所有标签服务调用中添加hint:Prior hint:runLast false

    您的查询应该是:

    SELECT ?country ?country_EN ?country_DE ?country_FR
       WHERE {
         ?country wdt:P463 wd:Q458. # member state of the European Union
         SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
                ?country rdfs:label ?country_EN.
         }
         SERVICE wikibase:label { bd:serviceParam wikibase:language "de".
                ?country rdfs:label ?country_DE.
         } hint:Prior hint:runLast false.
         SERVICE wikibase:label { bd:serviceParam wikibase:language "fr".
                ?country rdfs:label ?country_FR.
         } hint:Prior hint:runLast false.
    }
    

    Try it!

    很明显,可以使用常规 SPARQL 获取多种语言的标签,而且这样不那么冗长。然而,标签服务提供语言回退,包括 Q-id 的最后一个。

    来源:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多