- From: Gregory Williams <greg@evilfunhouse.com>
- Date: Thu, 22 Mar 2018 10:12:38 -0400
- To: Mikael Pesonen <mikael.pesonen@lingsoft.fi>
- Cc: semantic-web@w3.org
> On Mar 22, 2018, at 8:45 AM, Mikael Pesonen <mikael.pesonen@lingsoft.fi> wrote:
>
>
> Hi,
>
> I'm having trouble contructing a basic query which selects a string so that it prefers one language, over others but always tries to return something.
>
> So I have names
>
> :item schema:name "name"@en
> :item schema:name "nimi"@fi
> :item schema:name "namn"
>
> I need query that returns the English name ("name"@en) , but if not found the name without language ("namn"), and as last resort name in Finnish ("nimi"@fi)
>
> This query returns one random name:
>
> SELECT
> ?s
> (SAMPLE(?s_label_g) as ?s_label)
> ?s_type
> FROM <http://some_graph/>
> WHERE
> {
> ?s a ?s_type .
> ?s <http://schema.org/name> ?s_label_g
> }
> GROUP BY ?s ?s_type
>
>
> Anyone can help?
Mikael,
You can use OPTIONAL to do this by ordering the optional blocks by the preferred language tags:
SELECT ?s ?s_label ?s_type
WHERE {
?s a ?s_type .
OPTIONAL { ?s <http://schema.org/name> ?s_label . FILTER(LANGMATCHES(LANG(?s_label), "en")) }
OPTIONAL { ?s <http://schema.org/name> ?s_label . FILTER(LANG(?s_label) = "") }
OPTIONAL { ?s <http://schema.org/name> ?s_label . FILTER(LANGMATCHES(LANG(?s_label), "fi")) }
}
If you want to fall back on *any* language for cases where there is no English, Finnish, or un-tagged literal, you can also add a fourth optional block to the end:
SELECT ?s ?s_label ?s_type
WHERE {
?s a ?s_type .
OPTIONAL { ?s <http://schema.org/name> ?s_label . FILTER(LANGMATCHES(LANG(?s_label), "en")) }
OPTIONAL { ?s <http://schema.org/name> ?s_label . FILTER(LANG(?s_label) = "") }
OPTIONAL { ?s <http://schema.org/name> ?s_label . FILTER(LANGMATCHES(LANG(?s_label), "fi")) }
OPTIONAL { ?s <http://schema.org/name> ?s_label . }
}
Hope that helps.
.greg
Received on Thursday, 22 March 2018 14:13:14 UTC