-->

How to retrieve the categorical details in wikidat

2019-08-09 02:01发布

问题:

I have a list of instances as follows.

myinstances = ['word2vec', 'tf-idf', 'dijkstra's algorithm']

For each myinstance in the above list, I want to find:

 1. What are the other instances of `myinstance`'s category (i.e. only one hop)
 2. What are the instances of `myinstance`'s category's category (i.e. two hops)

For example, if we consider myinstance = word2vec

  1. What are the other instances of myinstance's category (i.e. only one hop)? As shown in the figure below the other instances of its immediade ancestor is GloVe.

  2. What are the instances of myinstance's category's category (i.e. two hops)? In other words, what are the instances of embedding (which is two hops away from word2vec) as shown the below figure.

I am just wondering if such query searches can be performed in sparql?

My current code is as follows.

SELECT * {
    VALUES ?searchTerm { "word2vec" "tf-idf" "dijkstra's algorithm" }
    SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:api "EntitySearch".
        bd:serviceParam wikibase:endpoint "www.wikidata.org".
        bd:serviceParam wikibase:limit 1 .
        bd:serviceParam mwapi:search ?searchTerm.
        bd:serviceParam mwapi:language "en".
        ?item wikibase:apiOutputItem mwapi:item.
        ?num wikibase:apiOrdinal true.
    }
    ?item (wdt:P279|wdt:P31) ?type
}

I am happy to provide more details if needed.