w3c / sparql-dev

SPARQL dev Community Group
https://w3c.github.io/sparql-dev/
Other
121 stars 19 forks source link

Ability to use Turtle-like syntax for language filtering #17

Open tfrancart opened 5 years ago

tfrancart commented 5 years ago

Why ?

Currently if I want to filter a literal based on its language, I need to write:

?x skos:prefLabel ?pref .
FILTER(langMatches(lang(?pref), "fr"))

or, if I am lazy (which happens often) :

?x skos:prefLabel ?pref .
FILTER(lang(?pref) = "fr")

This is tedious for a common use-case.

How could it look like ?

I'd like to be able to use Turtle-like syntax for language filtering:

?x skos:prefLabel ?pref@fr .

Considerations for backward-compatibility

This would make the @ character a special character in variable names.

tfrancart commented 5 years ago

Could be related to #13

VladimirAlexiev commented 5 years ago

Previous work

Wikidata has a special service that returns a single label and/or description per result entity, using a list of languages, with fallback:

select ?x ?xLabel ?xDescription {
  ?x foo bar.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr,en,it". }
}

If my user lang is bg then it will look for label & descr in bg,fr,en,it in that order; if none found then it will return ?xLabel=Qnnnn


@tfrancart

cygri commented 5 years ago

In SPARQL 1.1, variable names may not contain @, so the @en syntax would not make any SPARQL 1.1 queries invalid.

One could imagine ?x@en for exact match, and ?x@~en for the langMatches logic.

Something like ?x@en,fr would be problematic as the comma could be parsed as the beginning of a comma-separated list of objects.

tfrancart commented 5 years ago

I think my initial point was really really a point of pure syntactic sugar, to mimic Turtle syntax and have a easier learning curve for SPARQL beginners to whom we have teached Turtle already. So:

do you want a language fallback? Eg for my Canadian clients a fallback list like 'fr-CA,fr' would be very useful

I want the same behavior as langMatches()

does this need to be put in OPTIONAL? If there's no prefLabel in fr, should it skip that result?

It should skip that result if there is no prefLabel in fr. I wan the same behavior as when using a FILTER with langMatches().

One could imagine ?x@en for exact match, and ?x@~en for the langMatches logic.

I like that idea.

VladimirAlexiev commented 5 years ago

langMatches() is not a fallback: ?x@~fr will return all labels in fr, fr-CA, fr-FR etc.

A fallback would pick one value. This is what SERVICE wikibase:label does, and what #13 asks for. To do that with the current proposal would need something like:

optional {?x skos:prefLabel ?label_fr_CA@fr-CA}
optional {?x skos:prefLabel ?label_fr@fr}
bind(coalesce(?label_fr_CA,?label_fr) as ?label)

with the inefficiency that all wanted languages are fetched, not just the first one matching.

jindrichmynarz commented 5 years ago

In case preference order in language tags is desired, would a similar syntax as in the Accept-Language HTTP header do?

joernhees commented 5 years ago

this is definitely intertwined with #13

a practial remark though: langMatches(lang(?l), "en") is (currently) a lot slower than lang(?l) = "en" in many implementations :-/

tfrancart commented 5 years ago

@VladimirAlexiev

A fallback would pick one value. This is what SERVICE wikibase:label does, and what #13 asks for.

I did not ask for a fallback mechanism; this is, as you wrote, what #13 asks for, and this is a difference with this issue. The 2 issues are related, but not the same (but it could turn out they are solved by some simultaneously). Again, this issue here is really syntactic : I want the same syntax than Turtle for language filtering.