Filter by language only if the object is a literal

2019-04-08 05:19发布

问题:

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?

回答1:

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.