Update example
Browse files
example
CHANGED
|
@@ -7,11 +7,13 @@ import unicodedata
|
|
| 7 |
# CONFIGURATION
|
| 8 |
# ==========================
|
| 9 |
|
| 10 |
-
INPUT_FILE = "hbl_dump.txt"
|
| 11 |
OUTPUT_FILE = "hbl_pipe_dataset.csv"
|
| 12 |
|
| 13 |
|
| 14 |
-
# Fields to
|
|
|
|
|
|
|
| 15 |
FIELDS = [
|
| 16 |
"name",
|
| 17 |
"fatherName",
|
|
@@ -26,46 +28,61 @@ FIELDS = [
|
|
| 26 |
# TEXT CLEANING
|
| 27 |
# ==========================
|
| 28 |
|
| 29 |
-
def clean_text(text):
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
"""
|
| 34 |
|
| 35 |
if not text:
|
| 36 |
return ""
|
| 37 |
|
| 38 |
-
# Remove
|
| 39 |
-
#
|
| 40 |
-
# U+202A
|
| 41 |
-
# U+202B
|
| 42 |
-
# U+202C
|
| 43 |
-
# U+202D
|
| 44 |
-
# U+202E
|
| 45 |
|
| 46 |
text = "".join(
|
| 47 |
ch for ch in text
|
| 48 |
if unicodedata.category(ch) != "Cf"
|
| 49 |
)
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
text = re.sub(
|
| 53 |
r"\s+",
|
| 54 |
" ",
|
| 55 |
text
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
# ==========================
|
| 63 |
-
#
|
| 64 |
# ==========================
|
| 65 |
|
| 66 |
def extract_block(text, key):
|
| 67 |
"""
|
| 68 |
-
Extract dictionary
|
|
|
|
| 69 |
Example:
|
| 70 |
"data" : {
|
| 71 |
...
|
|
@@ -82,61 +99,70 @@ def extract_block(text, key):
|
|
| 82 |
re.DOTALL
|
| 83 |
)
|
| 84 |
|
|
|
|
| 85 |
if match:
|
| 86 |
return match.group(1)
|
| 87 |
|
|
|
|
| 88 |
return None
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
-
def extract_fields(block):
|
| 93 |
"""
|
| 94 |
-
Extract required fields from block.
|
| 95 |
"""
|
| 96 |
|
| 97 |
result = {}
|
| 98 |
|
|
|
|
| 99 |
for field in FIELDS:
|
| 100 |
|
| 101 |
pattern = (
|
| 102 |
rf'"{field}"\s*:\s*"([^"]*)"'
|
| 103 |
)
|
| 104 |
|
|
|
|
| 105 |
match = re.search(
|
| 106 |
pattern,
|
| 107 |
block
|
| 108 |
)
|
| 109 |
|
|
|
|
| 110 |
if match:
|
|
|
|
| 111 |
result[field] = clean_text(
|
| 112 |
-
match.group(1)
|
|
|
|
| 113 |
)
|
|
|
|
| 114 |
else:
|
|
|
|
|
|
|
| 115 |
result[field] = ""
|
| 116 |
|
|
|
|
| 117 |
return result
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
def join_fields(data):
|
| 122 |
-
"""
|
| 123 |
-
Create pipe separated format.
|
| 124 |
-
"""
|
| 125 |
|
| 126 |
return "|".join(
|
| 127 |
-
|
| 128 |
for field in FIELDS
|
| 129 |
)
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
# ==========================
|
| 134 |
-
# MAIN
|
| 135 |
# ==========================
|
| 136 |
|
| 137 |
def process():
|
| 138 |
|
| 139 |
-
print("Reading
|
|
|
|
| 140 |
|
| 141 |
with open(
|
| 142 |
INPUT_FILE,
|
|
@@ -147,7 +173,8 @@ def process():
|
|
| 147 |
content = f.read()
|
| 148 |
|
| 149 |
|
| 150 |
-
|
|
|
|
| 151 |
|
| 152 |
documents = re.split(
|
| 153 |
r'\n}\s*\n{',
|
|
@@ -156,7 +183,7 @@ def process():
|
|
| 156 |
|
| 157 |
|
| 158 |
print(
|
| 159 |
-
"Documents
|
| 160 |
len(documents)
|
| 161 |
)
|
| 162 |
|
|
@@ -166,6 +193,7 @@ def process():
|
|
| 166 |
skipped = 0
|
| 167 |
|
| 168 |
|
|
|
|
| 169 |
for doc in documents:
|
| 170 |
|
| 171 |
|
|
@@ -174,6 +202,7 @@ def process():
|
|
| 174 |
"REQUEST_BODY"
|
| 175 |
)
|
| 176 |
|
|
|
|
| 177 |
roman_block = extract_block(
|
| 178 |
doc,
|
| 179 |
"data"
|
|
@@ -181,28 +210,40 @@ def process():
|
|
| 181 |
|
| 182 |
|
| 183 |
if not urdu_block or not roman_block:
|
|
|
|
| 184 |
skipped += 1
|
| 185 |
continue
|
| 186 |
|
| 187 |
|
|
|
|
|
|
|
|
|
|
| 188 |
urdu_fields = extract_fields(
|
| 189 |
-
urdu_block
|
|
|
|
| 190 |
)
|
| 191 |
|
|
|
|
|
|
|
|
|
|
| 192 |
roman_fields = extract_fields(
|
| 193 |
-
roman_block
|
|
|
|
| 194 |
)
|
| 195 |
|
| 196 |
|
|
|
|
| 197 |
urdu_text = join_fields(
|
| 198 |
urdu_fields
|
| 199 |
)
|
| 200 |
|
|
|
|
| 201 |
roman_text = join_fields(
|
| 202 |
roman_fields
|
| 203 |
)
|
| 204 |
|
| 205 |
|
|
|
|
| 206 |
rows.append(
|
| 207 |
{
|
| 208 |
"urdu": urdu_text,
|
|
@@ -211,17 +252,20 @@ def process():
|
|
| 211 |
)
|
| 212 |
|
| 213 |
|
|
|
|
| 214 |
print(
|
| 215 |
-
"
|
| 216 |
len(rows)
|
| 217 |
)
|
| 218 |
|
|
|
|
| 219 |
print(
|
| 220 |
"Skipped:",
|
| 221 |
skipped
|
| 222 |
)
|
| 223 |
|
| 224 |
|
|
|
|
| 225 |
# Save CSV
|
| 226 |
|
| 227 |
with open(
|
|
@@ -247,6 +291,7 @@ def process():
|
|
| 247 |
writer.writerows(rows)
|
| 248 |
|
| 249 |
|
|
|
|
| 250 |
print(
|
| 251 |
"Saved:",
|
| 252 |
OUTPUT_FILE
|
|
|
|
| 7 |
# CONFIGURATION
|
| 8 |
# ==========================
|
| 9 |
|
| 10 |
+
INPUT_FILE = "hbl_dump.txt" # MongoDB dump file
|
| 11 |
OUTPUT_FILE = "hbl_pipe_dataset.csv"
|
| 12 |
|
| 13 |
|
| 14 |
+
# Fields to use
|
| 15 |
+
# CNIC removed because it is numeric
|
| 16 |
+
|
| 17 |
FIELDS = [
|
| 18 |
"name",
|
| 19 |
"fatherName",
|
|
|
|
| 28 |
# TEXT CLEANING
|
| 29 |
# ==========================
|
| 30 |
|
| 31 |
+
def clean_text(text, uppercase=False):
|
| 32 |
"""
|
| 33 |
+
Cleans text:
|
| 34 |
+
- Removes invisible Unicode formatting characters
|
| 35 |
+
- Removes extra spaces
|
| 36 |
+
- Optional uppercase conversion
|
| 37 |
"""
|
| 38 |
|
| 39 |
if not text:
|
| 40 |
return ""
|
| 41 |
|
| 42 |
+
# Remove Unicode control formatting characters
|
| 43 |
+
# Example:
|
| 44 |
+
# U+202A LEFT-TO-RIGHT EMBEDDING
|
| 45 |
+
# U+202B RIGHT-TO-LEFT EMBEDDING
|
| 46 |
+
# U+202C POP DIRECTIONAL FORMATTING
|
| 47 |
+
# U+202D LEFT-TO-RIGHT OVERRIDE
|
| 48 |
+
# U+202E RIGHT-TO-LEFT OVERRIDE
|
| 49 |
|
| 50 |
text = "".join(
|
| 51 |
ch for ch in text
|
| 52 |
if unicodedata.category(ch) != "Cf"
|
| 53 |
)
|
| 54 |
|
| 55 |
+
|
| 56 |
+
# Normalize whitespace
|
| 57 |
+
|
| 58 |
text = re.sub(
|
| 59 |
r"\s+",
|
| 60 |
" ",
|
| 61 |
text
|
| 62 |
)
|
| 63 |
|
| 64 |
+
|
| 65 |
+
text = text.strip()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# Roman Urdu normalization
|
| 69 |
+
|
| 70 |
+
if uppercase:
|
| 71 |
+
text = text.upper()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
return text
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
# ==========================
|
| 79 |
+
# MONGODB PARSING
|
| 80 |
# ==========================
|
| 81 |
|
| 82 |
def extract_block(text, key):
|
| 83 |
"""
|
| 84 |
+
Extract dictionary content after a key.
|
| 85 |
+
|
| 86 |
Example:
|
| 87 |
"data" : {
|
| 88 |
...
|
|
|
|
| 99 |
re.DOTALL
|
| 100 |
)
|
| 101 |
|
| 102 |
+
|
| 103 |
if match:
|
| 104 |
return match.group(1)
|
| 105 |
|
| 106 |
+
|
| 107 |
return None
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
+
def extract_fields(block, uppercase=False):
|
| 112 |
"""
|
| 113 |
+
Extract required fields from MongoDB block.
|
| 114 |
"""
|
| 115 |
|
| 116 |
result = {}
|
| 117 |
|
| 118 |
+
|
| 119 |
for field in FIELDS:
|
| 120 |
|
| 121 |
pattern = (
|
| 122 |
rf'"{field}"\s*:\s*"([^"]*)"'
|
| 123 |
)
|
| 124 |
|
| 125 |
+
|
| 126 |
match = re.search(
|
| 127 |
pattern,
|
| 128 |
block
|
| 129 |
)
|
| 130 |
|
| 131 |
+
|
| 132 |
if match:
|
| 133 |
+
|
| 134 |
result[field] = clean_text(
|
| 135 |
+
match.group(1),
|
| 136 |
+
uppercase=uppercase
|
| 137 |
)
|
| 138 |
+
|
| 139 |
else:
|
| 140 |
+
|
| 141 |
+
# Keep empty field
|
| 142 |
result[field] = ""
|
| 143 |
|
| 144 |
+
|
| 145 |
return result
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
def join_fields(data):
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
return "|".join(
|
| 152 |
+
data[field]
|
| 153 |
for field in FIELDS
|
| 154 |
)
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
# ==========================
|
| 159 |
+
# MAIN
|
| 160 |
# ==========================
|
| 161 |
|
| 162 |
def process():
|
| 163 |
|
| 164 |
+
print("Reading MongoDB dump...")
|
| 165 |
+
|
| 166 |
|
| 167 |
with open(
|
| 168 |
INPUT_FILE,
|
|
|
|
| 173 |
content = f.read()
|
| 174 |
|
| 175 |
|
| 176 |
+
|
| 177 |
+
# Split individual MongoDB documents
|
| 178 |
|
| 179 |
documents = re.split(
|
| 180 |
r'\n}\s*\n{',
|
|
|
|
| 183 |
|
| 184 |
|
| 185 |
print(
|
| 186 |
+
"Documents found:",
|
| 187 |
len(documents)
|
| 188 |
)
|
| 189 |
|
|
|
|
| 193 |
skipped = 0
|
| 194 |
|
| 195 |
|
| 196 |
+
|
| 197 |
for doc in documents:
|
| 198 |
|
| 199 |
|
|
|
|
| 202 |
"REQUEST_BODY"
|
| 203 |
)
|
| 204 |
|
| 205 |
+
|
| 206 |
roman_block = extract_block(
|
| 207 |
doc,
|
| 208 |
"data"
|
|
|
|
| 210 |
|
| 211 |
|
| 212 |
if not urdu_block or not roman_block:
|
| 213 |
+
|
| 214 |
skipped += 1
|
| 215 |
continue
|
| 216 |
|
| 217 |
|
| 218 |
+
|
| 219 |
+
# Urdu stays original
|
| 220 |
+
|
| 221 |
urdu_fields = extract_fields(
|
| 222 |
+
urdu_block,
|
| 223 |
+
uppercase=False
|
| 224 |
)
|
| 225 |
|
| 226 |
+
|
| 227 |
+
# Roman Urdu converted to uppercase
|
| 228 |
+
|
| 229 |
roman_fields = extract_fields(
|
| 230 |
+
roman_block,
|
| 231 |
+
uppercase=True
|
| 232 |
)
|
| 233 |
|
| 234 |
|
| 235 |
+
|
| 236 |
urdu_text = join_fields(
|
| 237 |
urdu_fields
|
| 238 |
)
|
| 239 |
|
| 240 |
+
|
| 241 |
roman_text = join_fields(
|
| 242 |
roman_fields
|
| 243 |
)
|
| 244 |
|
| 245 |
|
| 246 |
+
|
| 247 |
rows.append(
|
| 248 |
{
|
| 249 |
"urdu": urdu_text,
|
|
|
|
| 252 |
)
|
| 253 |
|
| 254 |
|
| 255 |
+
|
| 256 |
print(
|
| 257 |
+
"Pairs generated:",
|
| 258 |
len(rows)
|
| 259 |
)
|
| 260 |
|
| 261 |
+
|
| 262 |
print(
|
| 263 |
"Skipped:",
|
| 264 |
skipped
|
| 265 |
)
|
| 266 |
|
| 267 |
|
| 268 |
+
|
| 269 |
# Save CSV
|
| 270 |
|
| 271 |
with open(
|
|
|
|
| 291 |
writer.writerows(rows)
|
| 292 |
|
| 293 |
|
| 294 |
+
|
| 295 |
print(
|
| 296 |
"Saved:",
|
| 297 |
OUTPUT_FILE
|