0

I have the following query getting the terminal leaf nodes from a parent category

select distinct ?subcat where {
  ?subcat  skos:broader* category:Buildings_and_structures_in_France_by_city .
  optional { ?subsubcat skos:broader ?subcat }
} 
group by ?subcat
having count(?subsubcat) = 0

How do I get the path length between the child node ?subcat and the parent node category:Buildings_and_structures_in_France_by_city such that the output would be something like?

enter image description here

user1848018
  • 1,086
  • 1
  • 14
  • 33
  • If there's a unique path, then you can do this. If there are multiple paths, then you can't. – Joshua Taylor Apr 02 '15 at 16:45
  • I don't think your link to another post goes where you intend it to. – Joshua Taylor Apr 02 '15 at 16:46
  • I removed it. Regarding the unique path, how could it be done ? – user1848018 Apr 02 '15 at 16:51
  • See [Calculate length of path between nodes?](http://stackoverflow.com/q/5198889/1281433) – Joshua Taylor Apr 02 '15 at 16:56
  • Here is the real problem with skos:broader*, let say the root category is Category:Buildings_and_structures_in_France, and I want to get all french POIs. one of the subcats is category:Category:Ancient_Roman_buildings_and_structures_in_France, the skos:broader* will go down on this one and I end up fgetting POIs not in France but in Italy. That is why I need to filter by some path length – user1848018 Apr 02 '15 at 16:56
  • Also, rather than doing `optional { ... }` and `having ...`, you could just do `filter not exists { ?subsubcat skos:broader ?subcat }`. – Joshua Taylor Apr 02 '15 at 16:56
  • Thanks, but any suggestion regarding my problem. It had caused me a big headache how to obtain French POIs and not being deviated to other countries when I use skos:broader* – user1848018 Apr 02 '15 at 17:03
  • "regarding my problem": the title of the question is "How to get the path length between a child and parent node using skos:broader*" and I think my comments have been relevant there. But if the real task is to find buildings of interest, why not find places/buildings that are in the category category:Buildings_and_structures_in_France_by_city or a subcategory of it, and then, since some of those might not actually be in France, filter the buildings for ones that *are* in France? – Joshua Taylor Apr 02 '15 at 17:05
  • Have already done this but how do I know which one is not in France. I was hoping to use the path length as a filtering tool. – user1848018 Apr 02 '15 at 17:08
  • I don't think that path length will be a particularly helpful filtering tool there. the categories aren't intended to form a hierarchy, as they're just topics, and there can be cycles, so lengths aren't well-defined anyhow. For filtering, you could find some geographic properties and filter based on what's available. it won't be perfect, but you may have some luck. – Joshua Taylor Apr 02 '15 at 17:09
  • 1
    Alright, will do, Thanks again. How about the other question I posted today? It is a similar problem. http://stackoverflow.com/questions/29414522/finding-corresponding-state-for-pois-using-skosbroader – user1848018 Apr 02 '15 at 17:14

1 Answers1

2

If the real task is finding buildings and structures in France, then you can ask for things in that category of some appropriate types. E.g.,

select distinct ?building where {
  values ?type { dbpedia-owl:ArchitecturalStructure
                 dbpedia-owl:Building
                 dbpedia-owl:Place }
  ?building a ?type ;
            dcterms:subject/skos:broader* category:Buildings_and_structures_in_France_by_city 
} 

SPARQL results

That gets just about 700 results. If you find some that aren't France, take a look at their values and see what you could exclude them based on. Perhaps you could add a filter to restrict latitude and longitude, or country values, etc.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353