| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| from SPARQLWrapper import SPARQLWrapper, JSON |
| import requests |
|
|
| def get_superclasses(entity_id): |
| sparql = SPARQLWrapper("https://query.wikidata.org/sparql") |
| query = f""" |
| SELECT ?super ?superLabel WHERE {{ |
| wd:{entity_id} wdt:P279* ?super . |
| SERVICE wikibase:label {{ bd:serviceParam wikibase:language "en". }} |
| }} |
| """ |
| sparql.setQuery(query) |
| sparql.setReturnFormat(JSON) |
| results = sparql.query().convert() |
| return [(r["super"]["value"], r["superLabel"]["value"]) for r in results["results"]["bindings"]] |
|
|
| def get_wikidata_id(term): |
| url = f"https://www.wikidata.org/w/api.php" |
| params = { |
| 'action': 'wbsearchentities', |
| 'language': 'en', |
| 'format': 'json', |
| 'search': term |
| } |
| response = requests.get(url, params=params) |
| return response.json()['search'][0]['id'] |
|
|
| def print_superclasses_for_term(term): |
| entity_id = get_wikidata_id(term) |
| print(f"{term} -> Wikidata ID: {entity_id}") |
| results = get_superclasses(entity_id) |
| print(f"Superclasses for {term}:") |
| for uri, label in results: |
| print(label) |
|
|
| |
| |
|
|
| for term in ["group","Entropy", "Matrix", "Integration"]: |
| print_superclasses_for_term(term) |
| print("-" * 30) |
|
|