Sparql using wikiPageRedirects

I use sparql to find the location of an object. I have URLs from dbpedia-spootlight and you want to find a place for them. Thus, I use the following query:

PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> PREFIX dbo: <http://dbpedia.org/ontology/> SELECT DISTINCT * WHERE { ?uri rdfs:label ?label . OPTIONAL { ?uri geo:lat ?lat . ?uri geo:long ?long } . OPTIONAL { ?uri dbpedia-owl:country ?dbpediaContry . ?dbpediaContry dbpprop:cctld ?ccTLD } . FILTER (?uri = <URL> && lang(?label) = "en" ) } 

and everything was fine until I got this URL: http://dbpedia.org/resource/Valencia,_Spain . It has wikiPageRedirects for http://dbpedia.org/resource/Valencia and no other data. I’m lost how I can create a request to check for redirect cases.

Can someone help me?

+4
source share
2 answers

Try changing the first part of your request to use UNION for example.

 PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> PREFIX dbo: <http://dbpedia.org/ontology/> PREFIX dbp: <http://dbpedia.org/property/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT * WHERE { { ?uri rdfs:label ?label . } UNION { [] dbo:wikiPageRedirects ?uri . ?uri rdfs:label ?label . } OPTIONAL { ?uri geo:lat ?lat ; geo:long ?long } OPTIONAL { ?uri dbo:country ?dbpediaContry . ?dbpediaContry dbp:cctld ?ccTLD } FILTER (SAMETERM(?uri, <URL>) && lang(?label) = "en" ) } 

UNION allows you to find objects that have URIs and those that have URIs through redirection, and then apply the rest of your query. Note. I also changed your FILTER to use SAMETERM() , which should speed up the request.

In general, if you have such a request, when you do this type of FILTER to set a constant, I would strongly recommend that you replace all use of the variable ?uri <URL> instead, which should see much better performance

+2
source

Hmm, I found a solution, but I don’t think this is great. I added extra columns:

 ... OPTIONAL { ?uri dbpedia-owl:wikiPageRedirects ?red } . OPTIONAL { ?red geo:lat ?rlat . ?red geo:long ?rlong } ... 

but now I have 2 more columns to check the answer.

Another way I tried:

 ... OPTIONAL { ?uri dbpedia-owl:wikiPageRedirects ?red } . {?uri geo:lat ?lat . ?uri geo:long ?long} UNION { ?red geo:lat ?lat . ?red geo:long ?long } ... 

but then I have some answer if the object does not have geo: long and geo: lat

So, does anyone know a better solution?

0
source

Source: https://habr.com/ru/post/1436091/


All Articles