|
import tqdm |
|
import re |
|
import json |
|
import requests |
|
from huggingface_hub import login |
|
from huggingface_hub import upload_file |
|
from datasets import Dataset |
|
|
|
from google.colab import userdata |
|
|
|
|
|
login(userdata.get('HF_TOKEN')) |
|
|
|
|
|
json_url = "https://huggingface.co/Ibrahemqasim/enwiki_to_arwiki_categories/resolve/main/langlinks.json" |
|
response = requests.get(json_url) |
|
data = response.json() |
|
|
|
|
|
json_url2 = "https://huggingface.co/Ibrahemqasim/enwiki_to_arwiki_categories/resolve/main/countries.json" |
|
response2 = requests.get(json_url2) |
|
countries = response2.json() |
|
|
|
|
|
|
|
to_work = [ |
|
"countries", |
|
"categories_with_years", |
|
"categories_with_YEAR_COUNTRY_pattern", |
|
"categories_with_YEAR_pattern", |
|
] |
|
|
|
data_lists = { |
|
"countries" : {}, |
|
"categories_with_years" : {}, |
|
"categories_with_YEAR_COUNTRY_pattern" : {}, |
|
"categories_with_YEAR_pattern" : {}, |
|
} |
|
|
|
|
|
|
|
sorted_keys = sorted(countries.keys(), key=lambda x: -x.count(' ')) |
|
data_lists["countries"] = dict(sorted(countries.items(), key=lambda x: -x[1].count(' '))) |
|
|
|
regex_pattern = r'\b(' + '|'.join(map(re.escape, sorted_keys)) + r')\b' |
|
|
|
YEAR_PATTERN = "{YEAR}" |
|
COUNTRY_PATTERN = "{COUNTRY}" |
|
|
|
for tab in tqdm.tqdm(data): |
|
|
|
key = tab["en"] |
|
value = tab["ar"] |
|
|
|
|
|
|
|
|
|
if key.startswith('"') and key.endswith('"'): |
|
key = key[1:-1] |
|
|
|
|
|
|
|
if value.startswith(':"') and value.endswith('",'): |
|
value = value[2:-2] |
|
|
|
|
|
reg_year = r"(\d+[–-]\d+|\d{4})" |
|
|
|
key_digits = re.search(reg_year, key) |
|
value_digits = re.search(reg_year, value) |
|
|
|
if key_digits and value_digits and key_digits.group() == value_digits.group(): |
|
|
|
if key in data_lists["categories_with_years"]: |
|
data_lists["categories_with_years"][key]["count"] += 1 |
|
else: |
|
data_lists["categories_with_years"][key] = {"ar": value, "count": 1} |
|
|
|
key2 = key.replace(key_digits.group(), YEAR_PATTERN) |
|
value2 = value.replace(value_digits.group(), YEAR_PATTERN) |
|
|
|
|
|
|
|
if key2 in data_lists["categories_with_YEAR_pattern"]: |
|
data_lists["categories_with_YEAR_pattern"][key2]["count"] += 1 |
|
else: |
|
data_lists["categories_with_YEAR_pattern"][key2] = {"ar": value2, "count": 1} |
|
|
|
|
|
match = re.search(regex_pattern, key2) |
|
|
|
if match: |
|
en_country = match.group(1) |
|
ar_country = countries.get(en_country) |
|
|
|
if ar_country and ar_country in value2: |
|
key3 = re.sub(rf'\b{re.escape(en_country)}\b', COUNTRY_PATTERN, key2) |
|
value3 = re.sub(rf'\b{re.escape(ar_country)}\b', COUNTRY_PATTERN, value2) |
|
|
|
if COUNTRY_PATTERN in key3 and COUNTRY_PATTERN in value3: |
|
|
|
if key3 in data_lists["categories_with_YEAR_COUNTRY_pattern"]: |
|
data_lists["categories_with_YEAR_COUNTRY_pattern"][key3]["count"] += 1 |
|
else: |
|
data_lists["categories_with_YEAR_COUNTRY_pattern"][key3] = {"ar": value3, "count": 1} |
|
|
|
|
|
print(f"{len(data_lists['categories_with_YEAR_COUNTRY_pattern'])=}") |
|
print(f"{len(data_lists['categories_with_YEAR_pattern'])=}") |
|
|
|
print(f"all data len: {len(data):,}.") |
|
|
|
|
|
for x in to_work: |
|
data_list = data_lists.get(x) |
|
|
|
if x == "countries": |
|
data_list = [{"en": key, "ar": value} for key, value in data_list.items()] |
|
else: |
|
data_list = [{"en": key, "ar": value["ar"], "count": value["count"]} for key, value in data_list.items()] |
|
|
|
data_list = sorted(data_list, key=lambda x: x["count"], reverse=True) |
|
|
|
print("______________") |
|
print(f"len of {x} : {len(data_list)}.") |
|
|
|
|
|
|
|
''' |
|
# حفظ القاموس المصحح في ملف JSON |
|
with open(f"{x}.json", "w", encoding="utf-8") as f: |
|
json.dump(data_list, f, ensure_ascii=False, indent=4) |
|
# --- |
|
print(f"file: {x} uploaded successfully!") |
|
# --- |
|
upload_file( |
|
path_or_fileobj=f"{x}.json", # اسم الملف الذي تم حفظه |
|
path_in_repo=f"{x}.json", # المسار داخل المستودع |
|
repo_id="Ibrahemqasim/enwiki_to_arwiki_categories", # معرف المستودع |
|
# repo_type="dataset", # نوع المستودع (نستخدم dataset للملفات) |
|
) |
|
''' |
|
|
|
print("____________________________") |
|
|
|
|
|
dataset = Dataset.from_list(data_list) |
|
|
|
|
|
dataset.push_to_hub(f"Ibrahemqasim/{x}") |
|
|
|
print(f"dataset: Ibrahemqasim/{x} push_to_hub successfully!") |