File size: 6,795 Bytes
805087b d248431 805087b c10d5df 10b9478 c10d5df 805087b c10d5df 805087b c10d5df 805087b c10d5df 805087b d248431 805087b c10d5df 10b9478 1c94de7 07a48c1 1c94de7 10b9478 805087b 10b9478 805087b 10b9478 c10d5df 805087b c10d5df 10b9478 c10d5df 10b9478 c10d5df 10b9478 c10d5df 1c94de7 ada721f 1c94de7 c10d5df 07a48c1 805087b ada721f 07a48c1 805087b ada721f 805087b ada721f 07a48c1 805087b 07a48c1 805087b 07a48c1 805087b c10d5df 805087b d248431 805087b |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
import tqdm
import re
import requests
from huggingface_hub import login
from datasets import Dataset
from datasets import load_dataset
from google.colab import userdata
login(userdata.get('HF_TOKEN'))
data = load_dataset("Ibrahemqasim/categories_en2ar", split="train")
# nationalities.keys() "nat_en","man","men","women","womens","country_en","country_ar",
nationalities = load_dataset("Ibrahemqasim/nationalities", split="train")
nationalities_pattern = r'\b(' + '|'.join(map(re.escape, [n["nat_en"].lower() for n in sorted(nationalities, key=lambda x: -x["nat_en"].count(' '))])) + r')\b'
nationalities_pattern_ar = r'(' + '|'.join(map(re.escape, [n["man"].lower() for n in sorted(nationalities, key=lambda x: -x["man"].count(' '))])) + r')'
# print(nationalities_pattern)
countries = load_dataset("Ibrahemqasim/countries", split="train")
countries_pattern = r'\b(' + '|'.join(map(re.escape, [n["en"] for n in sorted(countries, key=lambda x: -x["en"].count(' '))])) + r')\b'
# ---
countries_dict = {cc["en"]: cc for cc in countries}
nationalities_dict = {cc["nat_en"].lower(): cc for cc in nationalities}
# ---
to_work = [
"categories_with_nationalities",
"categories_with_NAT_pattern",
"categories_with_YEAR_NAT_pattern",
]
data_lists = {
"categories_with_nationalities" : {},
"categories_with_NAT_pattern" : {},
"categories_with_YEAR_NAT_pattern" : {},
}
YEAR_PATTERN = "{YEAR}"
NAT = "{NAT}"
EN_NAT_PATTERN = "{EN_NAT}"
COUNTRY_PATTERN = "{COUNTRY}"
# data = [{"en": "Category:1970s yemeni peoples", "ar": "تصنيف: يمنيون في عقد 1970"}]
match1_done = 0
def new_func(value, ar_tab):
# List of possible keys and their corresponding tags
patterns = [
("men", "{NAT_MEN}"),
("womens", "{NAT_WOMENS}"),
("women", "{NAT_WOMEN}"),
("man", "{NAT_MAN}"),
]
# Iterate through the patterns
for key, tag in patterns:
country = ar_tab.get(key, "")
if not country:
continue
# ---
country2 = f"ال{country}".replace(" ", " ال")
# ---
if country2 in value:
return country2, tag.replace("}", "_AL}")
elif country in value:
return country, tag
return "", ""
for tab in tqdm.tqdm(data):
# ---
key = tab["en"]
value = tab["ar"]
# ---
match_en = re.search(nationalities_pattern, key, re.IGNORECASE)
match_ar = re.search(nationalities_pattern_ar, value, re.IGNORECASE)
# ----
if match_en or match_ar:
# ---
match1_done += 1
# ---
if key in data_lists["categories_with_nationalities"]:
data_lists["categories_with_nationalities"][key]["count"] += 1
else:
data_lists["categories_with_nationalities"][key] = {"ar": value, "count": 1}
# ---
if not match_en:
continue
# ---
en_country = match_en.group(1)
ar_tab = nationalities_dict.get(en_country.lower(), {})
# ---
if not ar_tab:
continue
# ---
ar_country, NAT_PATTERN = new_func(value, ar_tab)
# ---
if not NAT_PATTERN:
continue
# ---
key1 = re.sub(rf'\b{re.escape(en_country)}\b', EN_NAT_PATTERN, f" {key} ", re.IGNORECASE)
key1 = key1.strip()
# ---
if EN_NAT_PATTERN in key1 and key1 in data_lists["categories_with_NAT_pattern"]:
data_lists["categories_with_NAT_pattern"][key1]["count"] += 1
# ---
value1 = re.sub(rf'\b{re.escape(ar_country)}\b', NAT_PATTERN, f" {value} ", re.IGNORECASE)
value1 = value1.strip()
# ---
if EN_NAT_PATTERN in key1 and NAT_PATTERN in value1:
# ---
if key1 not in data_lists["categories_with_NAT_pattern"]:
data_lists["categories_with_NAT_pattern"][key1] = {"ar": value1, "count": 1}
# ---
# continue
# ---
# Add if key and value has 4 digits and they are the same
reg_year = r"(\d+[–-]\d+|\d{4})"
# ---
key_digits = re.search(reg_year, key, re.IGNORECASE)
value_digits = re.search(reg_year, value1, re.IGNORECASE)
# ----
if not key_digits:
continue
# ----
key2 = key1.replace(key_digits.group(), YEAR_PATTERN)
# ---
if key2 in data_lists["categories_with_YEAR_NAT_pattern"]:
data_lists["categories_with_YEAR_NAT_pattern"][key2]["count"] += 1
# ---
if not value_digits:
continue
# ----
value2 = value1.replace(value_digits.group(), YEAR_PATTERN)
# ----
if key_digits.group() == value_digits.group():
# ---
if key2 not in data_lists["categories_with_YEAR_NAT_pattern"]:
data_lists["categories_with_YEAR_NAT_pattern"][key2] = {"ar": value2, "count": 1}
# ----
continue
# ----
# البحث عن اسم الدولة في key2
match = re.search(countries_pattern, key2, re.IGNORECASE)
# ----
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, re.IGNORECASE)
value3 = re.sub(rf'\b{re.escape(ar_country)}\b', COUNTRY_PATTERN, value2, re.IGNORECASE)
# ---
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"{match1_done=}")
# for x, data_list in data_lists.items():
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()]
# sort data_list by count
data_list = sorted(data_list, key=lambda x: x["count"], reverse=True)
# ---
print("______________")
print(f"len of {x} : {len(data_list)}.")
# ---
print("____________________________")
# ---
if len(data_list) == 0:
continue
# ---
# إنشاء Dataset
dataset = Dataset.from_list(data_list)
# رفع Dataset إلى Hugging Face
dataset.push_to_hub(f"Ibrahemqasim/{x}")
# ---
print(f"dataset: Ibrahemqasim/{x} push_to_hub successfully!")
|