Spaces:
Runtime error
Runtime error
File size: 2,371 Bytes
98fefd6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import requests
import json
def generate_lang_code_file():
# URL of the Flores README containing the language codes
url = 'https://raw.githubusercontent.com/openlanguagedata/flores/main/README.md'
# Fetch the page content
response = requests.get(url)
content = response.text
# Extract the table content by parsing the plain text
lines = content.split('\n')
# Initialize a flag to start capturing data
languages = []
start_parsing = False
for line in lines:
if "Language coverage" in line:
start_parsing = True
continue
if start_parsing:
if line.strip() == "":
continue
if '|' not in line:
continue
parts = line.split('|')
if len(parts) >= 2:
code = parts[1].strip()[1:-1]
identifier = parts[2].strip()[1:-1]
name = parts[3].strip()
languages.append({"code": code, "identifier": identifier, "name": name})
# Omit the labels and divider
languages = languages[2:]
# Convert to JSON
json_data = json.dumps(languages, indent=4)
# Save the JSON data to a file
file_path = '/teamspace/studios/this_studio/multi-lang-translator/flores_language_codes.json'
with open(file_path, 'w') as file:
file.write(json_data)
print(f"JSON data saved to {file_path}")
# generate_lang_code_file()
def get_language_code(language_name,
json_file_path='/teamspace/studios/this_studio/multi-lang-translator/flores_language_codes.json'):
# Load the JSON data from the file
with open(json_file_path, 'r') as file:
languages = json.load(file)
# Search for the language code by language name
for language in languages:
if language['name'].lower() == language_name.lower():
return language['code']
return None # Return None if the language name is not found
def get_language_list(
json_file_path='/teamspace/studios/this_studio/multi-lang-translator/flores_language_codes.json'):
# Load the JSON data from the file
with open(json_file_path, 'r') as file:
languages = json.load(file)
# extract language name
language_names = [language['name'] for language in languages]
return language_names |