domain_mapping/zero_shot/dbpedia.py

39 lines
1.1 KiB
Python
Raw Permalink Normal View History

# %%
from SPARQLWrapper import SPARQLWrapper, JSON
# %%
sparql = SPARQLWrapper("https://dbpedia.org/sparql")
sparql.setQuery("""
SELECT ?altLabel WHERE {
?item rdfs:label "Windows Server"@en.
?item skos:altLabel ?altLabel.
FILTER (LANG(?altLabel) = "en")
}
LIMIT 10
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print(result["label"]["value"])
# %%
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("https://query.wikidata.org/sparql")
sparql.setQuery("""
SELECT ?itemLabel ?altLabel WHERE {
?item ?label "Windows Server"@en.
OPTIONAL { ?item skos:altLabel ?altLabel. FILTER (LANG(?altLabel) = "en") }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 10
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print("Label:", result["itemLabel"]["value"])
if "altLabel" in result:
print("Alias:", result["altLabel"]["value"])
# %%