Upload wikidata.py with huggingface_hub
Browse files- wikidata.py +15 -0
wikidata.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
def get_wikidata_labels(topic, limit=10):
|
| 4 |
+
"""Fetch related labels from Wikidata"""
|
| 5 |
+
query = f"""
|
| 6 |
+
SELECT DISTINCT ?label WHERE {{
|
| 7 |
+
?item rdfs:label ?label .
|
| 8 |
+
FILTER(CONTAINS(LCASE(?label), "{topic.lower()}"))
|
| 9 |
+
FILTER(LANG(?label) = "en")
|
| 10 |
+
}} LIMIT {limit}
|
| 11 |
+
"""
|
| 12 |
+
url = "https://query.wikidata.org/sparql"
|
| 13 |
+
response = requests.get(url, params={"query": query, "format": "json"})
|
| 14 |
+
return [result["label"]["value"]
|
| 15 |
+
for result in response.json()["results"]["bindings"]]
|