File size: 1,709 Bytes
a80f6e6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | # from nltk.corpus import wordnet as wn
# # 你想查询的数学术语列表
# math_terms = ["ideal"]
# for term in math_terms:
# print(f"\nTerm: {term}")
# synsets = wn.synsets(term, pos=wn.NOUN)
# if not synsets:
# print(" No synsets found.")
# for syn in synsets:
# print(f" Synset: {syn.name()}, Definition: {syn.definition()}, Depth: {syn.min_depth()}")
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)
# 示例:查询"Entropy"的super classes
# print_superclasses_for_term("Entropy")
for term in ["group","Entropy", "Matrix", "Integration"]:
print_superclasses_for_term(term)
print("-" * 30)
|