Filter by language only if the object is a literal

丶灬走出姿态 提交于 2019-12-06 01:51:38

问题


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.



来源:https://stackoverflow.com/questions/21685776/filter-by-language-only-if-the-object-is-a-literal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!