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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================
|
| 2 |
+
# Create Train / Validation / Test CSV splits
|
| 3 |
+
# NADRA/HBL Transliteration Dataset
|
| 4 |
+
# ============================================================
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# ============================================================
|
| 12 |
+
# Paths
|
| 13 |
+
# ============================================================
|
| 14 |
+
|
| 15 |
+
INPUT_FILE = "hbl_nadra_clean.csv"
|
| 16 |
+
|
| 17 |
+
TRAIN_FILE = "train.csv"
|
| 18 |
+
VAL_FILE = "val.csv"
|
| 19 |
+
TEST_FILE = "test.csv"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# ============================================================
|
| 23 |
+
# Load Dataset
|
| 24 |
+
# ============================================================
|
| 25 |
+
|
| 26 |
+
print("Loading dataset...")
|
| 27 |
+
|
| 28 |
+
df = pd.read_csv(
|
| 29 |
+
INPUT_FILE,
|
| 30 |
+
encoding="utf-8",
|
| 31 |
+
dtype=str
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
print("\nOriginal shape:")
|
| 36 |
+
print(df.shape)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ============================================================
|
| 40 |
+
# Basic Validation
|
| 41 |
+
# ============================================================
|
| 42 |
+
|
| 43 |
+
required_columns = {"urdu", "roman"}
|
| 44 |
+
|
| 45 |
+
if not required_columns.issubset(df.columns):
|
| 46 |
+
raise ValueError(
|
| 47 |
+
f"Dataset must contain columns: {required_columns}"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Remove completely empty rows
|
| 52 |
+
|
| 53 |
+
df = df.dropna()
|
| 54 |
+
|
| 55 |
+
df["urdu"] = df["urdu"].str.strip()
|
| 56 |
+
df["roman"] = df["roman"].str.strip()
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# Remove empty fields
|
| 60 |
+
|
| 61 |
+
df = df[
|
| 62 |
+
(df["urdu"] != "") &
|
| 63 |
+
(df["roman"] != "")
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# Remove duplicate pairs (safety)
|
| 68 |
+
|
| 69 |
+
before = len(df)
|
| 70 |
+
|
| 71 |
+
df = df.drop_duplicates(
|
| 72 |
+
subset=["urdu", "roman"]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
after = len(df)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
print("\nDuplicates removed:", before - after)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# ============================================================
|
| 82 |
+
# Shuffle Dataset
|
| 83 |
+
# ============================================================
|
| 84 |
+
|
| 85 |
+
df = df.sample(
|
| 86 |
+
frac=1,
|
| 87 |
+
random_state=42
|
| 88 |
+
).reset_index(drop=True)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
print("\nFinal dataset:")
|
| 92 |
+
print(df.shape)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# ============================================================
|
| 96 |
+
# Train / Validation / Test Split
|
| 97 |
+
#
|
| 98 |
+
# Train: 90%
|
| 99 |
+
# Validation: 5%
|
| 100 |
+
# Test: 5%
|
| 101 |
+
# ============================================================
|
| 102 |
+
|
| 103 |
+
train_df, temp_df = train_test_split(
|
| 104 |
+
df,
|
| 105 |
+
test_size=0.10,
|
| 106 |
+
random_state=42
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
val_df, test_df = train_test_split(
|
| 111 |
+
temp_df,
|
| 112 |
+
test_size=0.50,
|
| 113 |
+
random_state=42
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
print("\nSplit sizes:")
|
| 118 |
+
print("----------------")
|
| 119 |
+
print("Train:", len(train_df))
|
| 120 |
+
print("Validation:", len(val_df))
|
| 121 |
+
print("Test:", len(test_df))
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# ============================================================
|
| 125 |
+
# Save CSV files
|
| 126 |
+
# ============================================================
|
| 127 |
+
|
| 128 |
+
train_df.to_csv(
|
| 129 |
+
TRAIN_FILE,
|
| 130 |
+
index=False,
|
| 131 |
+
encoding="utf-8"
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
val_df.to_csv(
|
| 135 |
+
VAL_FILE,
|
| 136 |
+
index=False,
|
| 137 |
+
encoding="utf-8"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
test_df.to_csv(
|
| 141 |
+
TEST_FILE,
|
| 142 |
+
index=False,
|
| 143 |
+
encoding="utf-8"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
print("\nFiles saved:")
|
| 148 |
+
print(TRAIN_FILE)
|
| 149 |
+
print(VAL_FILE)
|
| 150 |
+
print(TEST_FILE)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
# ============================================================
|
| 155 |
+
# Print samples for verification
|
| 156 |
+
# ============================================================
|
| 157 |
+
|
| 158 |
+
print("\n\n==============================")
|
| 159 |
+
print("TRAIN FIRST 10 ROWS")
|
| 160 |
+
print("==============================")
|
| 161 |
+
|
| 162 |
+
print(
|
| 163 |
+
train_df.head(10).to_string()
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
print("\n\n==============================")
|
| 168 |
+
print("VALIDATION FIRST 10 ROWS")
|
| 169 |
+
print("==============================")
|
| 170 |
+
|
| 171 |
+
print(
|
| 172 |
+
val_df.head(10).to_string()
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
print("\n\n==============================")
|
| 177 |
+
print("TEST FIRST 10 ROWS")
|
| 178 |
+
print("==============================")
|
| 179 |
+
|
| 180 |
+
print(
|
| 181 |
+
test_df.head(10).to_string()
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
print("\nDONE")
|