Advertisement
Python253

sparql_wikidata_label_query

May 3rd, 2024
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: sparql_wikidata_label_query.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script retrieves labels from Wikidata using SPARQL query and presents them in a tabular format.
  9.  
  10. Requirements:
  11. - Python 3.x
  12. - requests library (install via `pip install requests`)
  13. - tabulate library (install via `pip install tabulate`)
  14.  
  15. Functions:
  16. - None
  17.  
  18. Usage:
  19. 1. Run the script using Python 3.x interpreter.
  20. 2. The script will retrieve labels for the item Q100924304 from Wikidata.
  21. 3. It will print the labels in a tabular format.
  22.  
  23. Additional Notes:
  24. - The script uses SPARQL query to retrieve data from Wikidata, so an internet connection is required.
  25. - It relies on the requests library to make HTTP requests to the Wikidata Query Service endpoint.
  26. - The tabulate library is used to format the output in a tabular form for better readability.
  27. - It's recommended to run the script in an environment with Python 3.x installed along with the required dependencies.
  28. """
  29.  
  30. import requests
  31. from tabulate import tabulate
  32.  
  33. # SPARQL query to retrieve the labels using the property "transliteration or transcription (P2440)"
  34. query = """
  35. SELECT DISTINCT ?label
  36. WHERE {
  37.  wd:Q100924304 p:P2440 ?statement.
  38.  ?statement ps:P2440 ?label.
  39.  BIND(LANG(?label) AS ?lang)
  40. }
  41. """
  42.  
  43. response = requests.get(
  44.     "https://query.wikidata.org/sparql",
  45.     params={"query": query, "format": "json"},
  46.     headers={"Accept": "application/sparql-results+json"},
  47. )
  48.  
  49. response_json = response.json()
  50. results = response_json["results"]["bindings"]
  51.  
  52. # Concatenate label data properly
  53. labels = ["".join(result["label"]["value"].split()) for result in results]
  54.  
  55. # Print all retrieved labels
  56. print(tabulate([(label,) for label in labels], headers=["Label"]))
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement