Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL select optional with language

Tags:

I have some triples that look like this:

test:thing rdfs:label "Non-Language Label"
test:thing rdfs:label "English Label"@en
test:thing rdfs:label "French Label"@fr

I'd like to form a sparql query that gives me the "Non-Language Label" AND the "French Label", if any exists.

I tried this and it's not working:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label ?preferredLabel
WHERE {
   test:thing rdfs:label ?label 
   OPTIONAL {
     test:thing rdfs:label ?preferredLabel . 
     FILTER (regex(str(?preferredLabel), '(^|\\\\W)fr', 'i'))
   }
}

Thanks in advance!

like image 936
Devin McQueeney Avatar asked Oct 29 '11 06:10

Devin McQueeney


People also ask

What is optional SPARQL?

OPTIONAL is a binary operator that combines two graph patterns. The optional pattern is any group pattern and may involve any SPARQL pattern types. If the group matches, the solution is extended, if not, the original solution is given (q-opt3. rq).

What types of queries does SPARQL support select all that apply?

SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions. SPARQL also supports aggregation, subqueries, negation, creating values by expressions, extensible value testing, and constraining queries by source RDF graph.

How do I query using SPARQL?

A SPARQL query may specify the dataset to be used for matching by using the FROM clause and the FROM NAMED clause to describe the RDF dataset. If a query provides such a dataset description, then it is used in place of any dataset that the query service would use if no dataset description is provided in a query.

What is regex in SPARQL?

3.1 Restricting the Values of Strings SPARQL FILTER functions like regex can test RDF literals. regex matches only plain literals with no language tag. regex can be used to match the lexical forms of other literals by using the str function.


2 Answers

I don't see why you need OPTIONAL here at all. Jan's query is failing because there is no shared variable between the outer pattern and the optional so you are trying to calculate the cross product of every label for test:thing with every non/french labelled test:thing which may be huge and why the query processor is failing.

You simply want something like the following unless I've misunderstood your question

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label
WHERE 
{
   test:thing rdfs:label ?label 
   FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "fr"))
}

If you want the two labels separately then you could do something like:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT ?label ?preferredLabel
WHERE 
{
  {
   test:thing rdfs:label ?label . FILTER(LANG(?label) = "")
  }
  UNION
  {
   test:thing rdfs:label ?preferredLabel . FILTER(LANGMATCHES(LANG(?label), "fr"))
  }
}
like image 168
RobV Avatar answered Oct 18 '22 16:10

RobV


The easiest way to check the language of literals is to use the lang() function. Using this, your query can be written as:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX test: <http://test#> 
SELECT ?label ?preferredLabel 
WHERE { 
   test:thing rdfs:label ?label 
   OPTIONAL { 
     test:thing rdfs:label ?preferredLabel . 
     FILTER (lang(?preferredLabel) = "" || lang(?preferredLabel) = "fr") 
   } 
}
like image 37
Jan Avatar answered Oct 18 '22 16:10

Jan