harshag11 commited on
Commit
6f8f74a
·
verified ·
1 Parent(s): fef0858

Upload wikidata.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. 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"]]