2

In a Wikidata SPARQL query such as the following, I want to be able to use a custom delimiter for the returns for ?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.

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

Thanks!

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
fjhj2
  • 293
  • 1
  • 2
  • 9
  • 1
    if you query explicitly for `skos:altLabel`, you could define the separator when you use `group by ?item ?placeOfBirth` + `group_concat` – UninformedUser Jul 25 '18 at 14:29
  • 1
    Comma is [hardcoded](https://phabricator.wikimedia.org/diffusion/WDQR/browse/master/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/label/LabelService.java$567), use `GROUP_CONCAT`, as AKSW has pointed out. – Stanislav Kralin Jul 25 '18 at 14:33
  • 1
    Thanks both. Do you mean a query like this: SELECT ?item ?itemLabel (GROUP_CONCAT(?placeOfBirthAltLabel; separator = "; ") AS ?placeOfBirthAlt) WHERE { ?item wdt:P106 wd:Q10833314. OPTIONAL { ?item wdt:P19 ?placeOfBirth . } SERVICE wikibase:label { bd:serviceParam wikibase:language "en". ?item rdfs:label ?itemLabel . ?placeOfBirth skos:altLabel ?placeOfBirthAltLabel . } } GROUP BY ?item ?itemLabel LIMIT 500 – fjhj2 Jul 25 '18 at 15:07
  • Unfortunately couldn't work out how to parse it using group_concat either. Apologies a bit new to this... – fjhj2 Jul 25 '18 at 15:08

1 Answers1

1

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" }
}

Try it!

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

Try it!


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

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

should work, whereas

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

shouldn't.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58