Skip to content

Commit

Permalink
Merge pull request #1 from riddhihalade/riddhihalade-patch-1
Browse files Browse the repository at this point in the history
Add files via upload
  • Loading branch information
riddhihalade authored Oct 5, 2024
2 parents ce94d2c + b3a1ee5 commit f3418e6
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/scribe_data/cli/auto.py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import argparse
import sys
from scribe_data.process_wiki import gen_autosuggestions
from SPARQLWrapper import SPARQLWrapper, JSON

# Define the SPARQL endpoint
SPARQL_ENDPOINT = "https://query.wikidata.org/sparql"

def fetch_language_data(language, data_type):
"""
Fetch language data from Wikidata using SPARQL.
"""
sparql = SPARQLWrapper(SPARQL_ENDPOINT)
query = f"""
SELECT ?item ?itemLabel
WHERE {{
?item wdt:P31 wd:{data_type}.
?item wdt:P407 wd:{language}.
SERVICE wikibase:label {{ bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }}
}}
"""
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

return results["results"]["bindings"]

def handle_autosuggestions(language):
"""
Generate autosuggestions using Wikipedia data.
"""
suggestions = gen_autosuggestions(language) # Call the autosuggestions generator
return suggestions

def main():
parser = argparse.ArgumentParser(description="Get language data from Wikidata.")

# Define the arguments
parser.add_argument("-lang", required=True, help="Language code (e.g., en for English).")
parser.add_argument("-dt", required=True, help="Data type (e.g., Q5 for human).")
parser.add_argument("--autosuggest", action="store_true", help="Enable autosuggestions.")

args = parser.parse_args()

language = args.lang
data_type = args.dt

# Fetch language data from Wikidata
print(f"Fetching data for language: {language}, data type: {data_type}...")
data = fetch_language_data(language, data_type)

# Display fetched data
if data:
print(f"Data fetched for language {language} and type {data_type}:")
for item in data:
print(f" - {item['itemLabel']['value']} ({item['item']['value']})")
else:
print(f"No data found for language {language} and type {data_type}.")

# Handle autosuggestions if enabled
if args.autosuggest:
print(f"Generating autosuggestions for {language}...")
suggestions = handle_autosuggestions(language)
if suggestions:
print("Autosuggestions:")
for suggestion in suggestions:
print(f" - {suggestion}")
else:
print("No autosuggestions generated.")

if __name__ == "__main__":
main()

0 comments on commit f3418e6

Please sign in to comment.