3

I'm trying to query Wikidata using entity names. My goal is to get the Wikidata ID of the entity. Right now I have something like:

SELECT ?item
WHERE {
    ?item rdfs:label "name_of_thing"@en
}

and while this usually gets the job done I also want to check whether the label is contained in the alternative names on Wikidata.

For example, if I replace "name_of_thing" with "Philippines AirAsia" then I don't get any results, but the reason why is because "AirAsia Philippines" is the proper name of the entry, with "Philippines AirAsia" contained in the "Also known as" column in the Wikidata page.

I want to query the page and if the first fetch fails then check whether the name is contained in the "Also known as" column. How should I go about doing this? Thanks.

Sean
  • 2,890
  • 8
  • 36
  • 78
  • without precedence, it would simply be `SELECT ?item WHERE { ?item rdfs:label|skos:altLabel "Philippines AirAsia"@en }` - with precedence things are getting more complicated, maybe something like `SELECT (COALESCE(?item1, ?item2) as ?item) WHERE { {?item1 rdfs:label "Philippines AirAsia"@en } UNION {?item2 skos:altLabel "Philippines AirAsia"@en} }` - I didn't check if it really works for multiple matches. – UninformedUser Mar 01 '21 at 07:13
  • maybe [something like this](https://w.wiki/33Hv) is sufficient: `SELECT ?item ?prefLabel ?altLabel WHERE { values ?prefLabel {"AirAsia Philippines"@en} ?item rdfs:label ?prefLabel ; skos:altLabel ?altLabel . }` – lps Mar 01 '21 at 16:23

1 Answers1

1

The pipe operator works for me:

Query

SELECT ?item ?prefLabel WHERE {
        VALUES ?prefLabel { "AirAsia Philippines"@en }
        ?item rdfs:label|skos:altLabel ?prefLabel 
}
Matthias Winkelmann
  • 15,870
  • 7
  • 64
  • 76