在 SPARQL 查询中解析来自 AltLabel 的返回 [英] Parsing returns from AltLabel in a SPARQL query

查看:60
本文介绍了在 SPARQL 查询中解析来自 AltLabel 的返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在如下所示的 Wikidata SPARQL 查询中,我希望能够对 ?placeOfBirthAltLabel 的返回使用自定义分隔符.

In a Wikidata SPARQL query such as the following, I want to be able to use a custom delimiter for the returns for ?placeOfBirthAltLabel.

问题是?placeOfBirthAltLabel下的某些值包含逗号

The problem is that some values under ?placeOfBirthAltLabel contain commas

例如纽约"的同义词包括纽约,美国"作为一个条目.

e.g. synonyms for "New York" include "New York, USA" as a single entry.

但是,由于返回是逗号分隔的,因此该单个条目将被解析为两个单独的字符串.

However, as the returns are comma delimited, this single entry will be parsed as two separate strings.

所以换句话说,我需要返回 [New York, USA ;纽约市;NYC, USA ] 而不是 [New York, USA, NYC, NYC, USA]

So in other words I need the return to be [New York, USA ; NYC ; NYC, USA ] as opposed to [New York, USA, NYC, NYC, USA]

SELECT ?item ?itemLabel ?placeOfBirthLabel ?placeOfBirthAltLabel 
WHERE
{
  ?item wdt:P106 wd:Q10833314.
  OPTIONAL { ?item wdt:P19 ?placeOfBirth }

  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

LIMIT 100

谢谢!

推荐答案

您不需要解析替代标签.它们的值由标签服务连接在一起.只是不要将标签服务用于替代标签:

You do not need to parse alternative labels. Their values are concatenated by the label service. Just do not use the label service for alternative labels:

SELECT ?item ?itemLabel ?placeLabel ?place_alt_label WHERE { 
    ?item wdt:P106 wd:Q10833314. 
    OPTIONAL { 
        ?item wdt:P19 ?place .
        OPTIONAL {
            ?place skos:altLabel ?place_alt_label .
            FILTER (lang(?place_alt_label)='en')
            }
    }  
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

试试吧!

如果你还想解析...逗号是 硬编码,使用分组和GROUP_CONCAT 和自定义分隔符:

If you still want to parse... The comma is hardcoded, use grouping and GROUP_CONCAT with custom separator instead:

SELECT ?item ?itemLabel ?placeLabel
    (GROUP_CONCAT(?place_alt_label; separator='; ') AS ?4) WHERE { 
    ?item wdt:P106 wd:Q10833314. 
    OPTIONAL { 
        ?item wdt:P19 ?place .
        OPTIONAL {
            ?place skos:altLabel ?place_alt_label .
            FILTER (lang(?place_alt_label)='en')
            }
    }  
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
GROUP BY ?item ?itemLabel ?placeLabel

试试吧!

注意标签服务预测的变量.例如,

Be carefull with variables projected by the label service. For example,

SELECT ?item ?itemLabel ?placeLabel   {...}
GROUP BY ?item ?itemLabel ?placeLabel

应该可以,而

SELECT ?item ?itemLabel (SAMPLE(?placeLabel) AS ?3)   {...}
GROUP BY ?item ?itemLabel

不应该.

这篇关于在 SPARQL 查询中解析来自 AltLabel 的返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆