Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: sparql_wikidata_label_query.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script retrieves labels from Wikidata using SPARQL query and presents them in a tabular format.
- Requirements:
- - Python 3.x
- - requests library (install via `pip install requests`)
- - tabulate library (install via `pip install tabulate`)
- Functions:
- - None
- Usage:
- 1. Run the script using Python 3.x interpreter.
- 2. The script will retrieve labels for the item Q100924304 from Wikidata.
- 3. It will print the labels in a tabular format.
- Additional Notes:
- - The script uses SPARQL query to retrieve data from Wikidata, so an internet connection is required.
- - It relies on the requests library to make HTTP requests to the Wikidata Query Service endpoint.
- - The tabulate library is used to format the output in a tabular form for better readability.
- - It's recommended to run the script in an environment with Python 3.x installed along with the required dependencies.
- """
- import requests
- from tabulate import tabulate
- # SPARQL query to retrieve the labels using the property "transliteration or transcription (P2440)"
- query = """
- SELECT DISTINCT ?label
- WHERE {
- wd:Q100924304 p:P2440 ?statement.
- ?statement ps:P2440 ?label.
- BIND(LANG(?label) AS ?lang)
- }
- """
- response = requests.get(
- "https://query.wikidata.org/sparql",
- params={"query": query, "format": "json"},
- headers={"Accept": "application/sparql-results+json"},
- )
- response_json = response.json()
- results = response_json["results"]["bindings"]
- # Concatenate label data properly
- labels = ["".join(result["label"]["value"].split()) for result in results]
- # Print all retrieved labels
- print(tabulate([(label,) for label in labels], headers=["Label"]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement