Re: Help with simple SPARQL query getting strings of selected languages

Thank you! This works great, just needed to filter out resources that 
don't have label at all:

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 }
     FILTER ( EXISTS { ?s <http://schema.org/name> ?s_label } )
}

On 22.3.2018 16:12, Gregory Williams wrote:
>
>> 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
>

-- 
Lingsoft - 30 years of Leading Language Management

www.lingsoft.fi

Speech Applications - Language Management - Translation - Reader's and Writer's Tools - Text Tools - E-books and M-books

Mikael Pesonen
System Engineer

e-mail: mikael.pesonen@lingsoft.fi
Tel. +358 2 279 3300

Time zone: GMT+2

Helsinki Office
Eteläranta 10
FI-00130 Helsinki
FINLAND

Turku Office
Kauppiaskatu 5 A
FI-20100 Turku
FINLAND

Received on Thursday, 22 March 2018 16:00:35 UTC