Sparql multi lang data compression in one line

I would like to select data property values ​​using sparql with some restrictions in their languages:

  • I have an ordered set of preferred languages ​​("ru", "en", ... etc.).
  • If an element has more than one language for the value, I would like to have only one value limited by my set of languages ​​(if ru is available, I want to see the ru value, otherwise, if en available I want to see en else if ... etc ., if there are no languages ​​available - no lang value).

Current request:

select distinct ?dataProperty ?dpropertyValue where { 
<http://dbpedia.org/resource/Blackmore's_Night> ?dataProperty ?dpropertyValue.
?dataProperty a owl:DatatypeProperty.
FILTER ( langmatches(lang(?dpropertyValue),"ru") || langmatches(lang(?    dpropertyValue),"en") || lang(?dpropertyValue)="" )
}

The problem with it: the results contain two lines for abstract (ru + en). I want only one line that should contain ru. In case ru is not available, I would like to get en, etc. How?

+1
1

, :

@prefix : <http://stackoverflow.com/q/21531063/1281433/> .

:a a :resource; 
   :p "a in english"@en, "a in russian"@ru .
:b a :resource ;
   :p "b in english"@en .

:

--------------------------------
| resource | label             |
================================
| :b       | "b in english"@en |
| :a       | "a in russian"@ru |
--------------------------------

.

, ,

, SPARQL 1.1, values. , values . , , ). , , .

prefix : <http://stackoverflow.com/q/21531063/1281433/>

select ?resource ?label where {
  # for each resource, find the rank of the 
  # language of the most preferred label.
  {
    select ?resource (min(?rank) as ?langRank) where {
      values (?lang ?rank) { ("ru" 1) ("en" 2) }
      ?resource :p ?label .
      filter(langMatches(lang(?label),?lang))
    }
    group by ?resource 
  }

  # ?langRank from the subquery is, for each 
  # resource, the best preference.  With the
  # values clause, we get just the language
  # that we want.
  values (?lang ?langRank) { ("ru" 1) ("en" 2) }
  ?resource a :resource ; :p ?label .
  filter(langMatches(lang(?label),?lang))
}

,

, coalesce ( , ) . , - , , .

prefix : <http://stackoverflow.com/q/21531063/1281433/>

select ?resource ?label where {
  # find resources
  ?resource a :resource .

  # grab a russian label, if available
  optional {
    ?resource :p ?rulabel .
    filter( langMatches(lang(?rulabel),"ru") )
  }

  # grab an english label, if available
  optional {
    ?resource :p ?enlabel .
    filter( langMatches(lang(?enlabel),"en") )
  }

  # take either as the label, but russian over english
  bind( coalesce( ?rulabel, ?enlabel ) as ?label )

}
+1

All Articles