0

I am trying to reproduce the Wikidata generic tree behaviour (like this) in SPARQL. The concept is that we have an initial tag (Fruit (Q3314483)) and we can change the depth of the tree to get the child of the initial. For example:

- Q3314483 (Fruit)
-- Q89 (Apple)
-- Q169 (Mango)
---- Q599314 (Osteen)
---- Q2663618 (Alphonso)
-- Q196 (Cherry)
...

I am stuck with SPARQL query as I never worked with it before. In particular, there is the thing that I looked for but the problem I can not add depth parameter to this snippet:

SELECT ?item ?itemLabel
WHERE {
  ?item wdt:P279* wd:Q3314483.
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
  }
}

Can you please help of how to change the script to limit the depth equals 1 to get only the following:

- Q3314483 (Fruit)
-- Q89 (Apple)
-- Q169 (Mango)
-- Q196 (Cherry)
...

Thanks!

logi-kal
  • 7,107
  • 6
  • 31
  • 43
Timebird
  • 149
  • 2
  • 4
  • 12
  • 1
    If you omit the `*`, you get only direct subclasses. Is that what you mean? – Stefan - brox IT-Solutions Jul 11 '23 at 13:12
  • 1
    the tool is incrementally getting the direct subclasses to expand the tree. SPARQL property path had a feature like `wdt:P279{1,3}` in the proposal, but it never made it to the final specs. So all you can do is to do `wdt:P279/wdt:P279?/wdt:P279?` - but property paths are really expensive. Also, keeping track of the depth of the path or the paths only works with limitations and is cumbersome, see https://stackoverflow.com/questions/18024413/finding-all-steps-in-property-path/18032019#18032019 - but to get just depth 1 is trivial as the comment above already said, just do `wdt:P279` – UninformedUser Jul 12 '23 at 06:06

1 Answers1

1

When you write wdt:P279*, you are asking for any (even empty) path from the subject and the object of your triple.

If you don't want to allow a generic path buth ask only for direct subclasses, you can just remove the asterisk *.

Then, if you wish to ask for path of specific lenght, you can build them as wdt:P279/wdt:P279 (length 2), wdt:P279/wdt:P279/wdt:P279 (length 3), etc.

logi-kal
  • 7,107
  • 6
  • 31
  • 43