Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter by language only if the object is a literal

I've written the following query:

SELECT DISTINCT
  ?predicate
  ?object
  ?label
WHERE {
  VALUES        ?subject     { <http://dbpedia.org/resource/Hercules_(1997_film)> }
  ?subject      ?predicate   ?object .
  ?predicate    rdfs:label   ?label .
  FILTER(langMatches(lang(?object), "EN"))
}
LIMIT 100

When I write the FILTER line this way, I've essentially filtered out all non-literals (side question: are literals the only type that can have a language tag?)

So, how do I keep all of my results and filter out non-english literals only?

like image 815
Kristian Avatar asked Feb 10 '14 19:02

Kristian


1 Answers

You could use the isLiteral function to apply the language restriction only to objects that are literals.

x implies y can be expressed with SPARQL operators as !x || y, so you could write your query like this:

SELECT DISTINCT
  ?predicate
  ?object
  ?label
WHERE {
  VALUES        ?subject     { <http://dbpedia.org/resource/Hercules_(1997_film)> }
  ?subject      ?predicate   ?object .
  ?predicate    rdfs:label   ?label .
  FILTER(!isLiteral(?object) || langMatches(lang(?object), "EN"))
}
LIMIT 100

As for your secondary question, the description for language tags from the RDF concepts and abstracts only mentions language tags to appear as a part of plain literals. Likewise, the grammar in the SPARQL 1.1 specification uses the LANGTAG nonterminal only in the RDFLiteral production as an exclusive alternative instead of a datatype IRI.

like image 66
O. R. Mapper Avatar answered Nov 07 '22 14:11

O. R. Mapper