-->

How to filter wikidata labels in concept search?

2019-07-17 04:01发布

问题:

I am using the following code to get the wikidata labels for the given concept (e.g., network analysis).

SELECT ?item {
    VALUES ?searchTerm { "network analysis" }
    SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:api "EntitySearch".
        bd:serviceParam wikibase:endpoint "www.wikidata.org".
        bd:serviceParam wikibase:limit 3 .
        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
}
ORDER BY ?searchTerm ?num

This returns the following wikidata labels.

 wd:Q618079  --> related to electronics
 wd:Q4417999  --> related to graph theory (computer science)
 wd:Q60640547 --> related to scholary article

I would like to get the wikidata labels that are only related to computer science (i.e. wd:Q4417999 in the above example).

In DBpedia I ran the below query to identify if a word is in computer science.

sparql.setQuery(" ASK { dbc:Network_analysis skos:broader{1,7} dbc:Computer_science } ")

Is it possible to do the same in wikidata (i.e. check if computer science is an ancestor of the given concept and returns only that wikidata label).

If there is a better solution than performing ancestoral search please do suggest me.

I am happy to provide more details if needed.

回答1:

The sparql query mentioned below solved my issue.

SELECT DISTINCT ?item {
VALUES ?searchTerm { "network analysis"}
SERVICE wikibase:mwapi {
    bd:serviceParam wikibase:api "EntitySearch".
    bd:serviceParam wikibase:endpoint "www.wikidata.org".
    bd:serviceParam wikibase:limit 3 .
    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
filter exists {?type wdt:P279*/wdt:P361* wd:Q21198}
}
ORDER BY ?searchTerm ?num