How to match exact string literals in SPARQL?

不问归期 提交于 2019-12-21 12:27:30

问题


I have this query. It matches anything which has "South" in its name. But I only want the one whose foaf:name is exactly "South".

SELECT Distinct ?TypeLabel 
WHERE
{
    ?a     foaf:name   "South" .
    ?a     rdf:type    ?Type .
    ?Type  rdfs:label  ?TypeLabel .
}

回答1:


(Breaking out of comments for this)

Data issues

The issue is the data, not your query. If use the following query:

SELECT DISTINCT ?a 
WHERE { 
    ?a foaf:name "Imran Khan" . 
}

You find (as you say) "Imran Khan Niazy". But looking at the dbpedia entry for Imran Khan, you'll see both:

foaf:name "Imran Khan Niazy"
foaf:name "Imran Khan"

This is because RDF allows repeated use of properties.

Cause

"South" had the same issue (album, artist, and oddly 'South Luton'). These are cases where there are both familiar names ("Imran Khan", "South"), and more precise names ("Imran Khan Niazy", "South (album)") for the purposes of correctness or disambiguation.

Resolution

If you want a more precise match try adding a type (e.g. http://dbpedia.org/ontology/MusicalWork for the album).

Beware

Be aware that DBpedia derives from Wikipedia, and the extraction process isn't perfect. This is an area alive with wonky data, so don't assume your query has gone wrong.




回答2:


a bit late but anyway... I think this is what your looking for:

SELECT Distinct ?TypeLabel Where { 
?a foaf:name ?name . 
?a rdf:type ?Type . 
?Type rdfs:label ?TypeLabel . 
FILTER (?name="South"^^xsd:string)
}

you can use FILTER with the xsd types in order to restrict the result. hope this helps... cheers!




回答3:


That query should match exactly the literal South and not literals merely containing South as a substring. For partial matches you'd go to FILTER with e.g. REGEX(). Your query engine is broken in this sense - which query engine you are working with?



来源:https://stackoverflow.com/questions/2591719/how-to-match-exact-string-literals-in-sparql

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