Update src/02_prepare_dataset.py
Browse files- src/02_prepare_dataset.py +244 -496
src/02_prepare_dataset.py
CHANGED
|
@@ -1,12 +1,8 @@
|
|
| 1 |
|
| 2 |
-
import json
|
| 3 |
import os
|
| 4 |
import tempfile
|
| 5 |
-
from collections import Counter
|
| 6 |
-
from datetime import datetime, timezone
|
| 7 |
-
|
| 8 |
-
import pyarrow.parquet as pq
|
| 9 |
import requests
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
# ============================================================
|
|
@@ -15,33 +11,26 @@ import requests
|
|
| 15 |
|
| 16 |
DATASET_NAME = "DavidCBaines/ebible_corpus"
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
"book",
|
| 23 |
"chapter",
|
| 24 |
"verse",
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
]
|
| 28 |
|
| 29 |
-
OUTPUT_DIR = "data"
|
| 30 |
-
|
| 31 |
-
TRAIN_FILE = os.path.join(OUTPUT_DIR, "train.jsonl")
|
| 32 |
-
VALIDATION_FILE = os.path.join(OUTPUT_DIR, "validation.jsonl")
|
| 33 |
-
TEST_FILE = os.path.join(OUTPUT_DIR, "test.jsonl")
|
| 34 |
-
REPORT_FILE = os.path.join(OUTPUT_DIR, "dataset_report.json")
|
| 35 |
-
|
| 36 |
-
BOOKS_FOR_TEST = ["REV", "HEB", "ROM", "1CO", "JHN"]
|
| 37 |
-
|
| 38 |
-
BOOKS_FOR_VALIDATION = ["ACT", "MRK", "GAL", "EPH"]
|
| 39 |
-
|
| 40 |
-
PARQUET_API = (
|
| 41 |
-
"https://datasets-server.huggingface.co/parquet"
|
| 42 |
-
f"?dataset={DATASET_NAME}"
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
|
| 46 |
# ============================================================
|
| 47 |
# DESCOBERTA DOS PARQUETS
|
|
@@ -51,33 +40,40 @@ def get_parquet_files():
|
|
| 51 |
|
| 52 |
print("Consultando arquivos Parquet no Hugging Face...")
|
| 53 |
|
| 54 |
-
response = requests.get(
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
|
| 59 |
-
files =
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
if not files:
|
| 65 |
raise RuntimeError(
|
| 66 |
-
"Nenhum arquivo Parquet
|
| 67 |
)
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if file.get("config") == config
|
| 75 |
-
]
|
| 76 |
|
| 77 |
-
|
| 78 |
-
print(f"Arquivos Parquet: {len(files)}")
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
# ============================================================
|
|
@@ -89,7 +85,7 @@ def download_parquet(url, destination):
|
|
| 89 |
with requests.get(
|
| 90 |
url,
|
| 91 |
stream=True,
|
| 92 |
-
timeout=
|
| 93 |
) as response:
|
| 94 |
|
| 95 |
response.raise_for_status()
|
|
@@ -105,261 +101,236 @@ def download_parquet(url, destination):
|
|
| 105 |
|
| 106 |
|
| 107 |
# ============================================================
|
| 108 |
-
#
|
| 109 |
# ============================================================
|
| 110 |
|
| 111 |
-
def
|
| 112 |
-
|
| 113 |
-
if value is None:
|
| 114 |
-
return None
|
| 115 |
-
|
| 116 |
-
value = " ".join(str(value).strip().split())
|
| 117 |
|
| 118 |
-
|
| 119 |
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
return None, "source_and_target_empty"
|
| 128 |
|
| 129 |
-
|
| 130 |
-
return None, "source_empty"
|
| 131 |
|
| 132 |
-
if target is None:
|
| 133 |
-
return None, "target_empty"
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
if target.startswith("<"):
|
| 139 |
-
return None, "target_markup"
|
| 140 |
-
|
| 141 |
-
book = row.get("book")
|
| 142 |
|
| 143 |
-
|
| 144 |
-
return None, "missing_book"
|
| 145 |
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
verse = int(row.get("verse"))
|
| 150 |
|
| 151 |
-
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
"reference": f"{book} {chapter}:{verse}",
|
| 162 |
-
"source": source,
|
| 163 |
-
"target": target
|
| 164 |
-
}, None
|
| 165 |
|
|
|
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
def extract_pairs(parquet_files):
|
| 172 |
|
| 173 |
-
|
| 174 |
|
| 175 |
-
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
|
| 179 |
-
|
| 180 |
|
| 181 |
-
|
| 182 |
|
| 183 |
-
|
| 184 |
|
| 185 |
-
|
| 186 |
-
"filename",
|
| 187 |
-
f"shard_{index}"
|
| 188 |
-
)
|
| 189 |
|
| 190 |
print(
|
| 191 |
-
f"
|
| 192 |
-
f"
|
| 193 |
)
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
temp_dir,
|
| 199 |
-
"shard.parquet"
|
| 200 |
-
)
|
| 201 |
-
|
| 202 |
-
download_parquet(
|
| 203 |
-
info["url"],
|
| 204 |
-
local_file
|
| 205 |
-
)
|
| 206 |
|
| 207 |
-
|
| 208 |
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
|
| 212 |
-
column
|
| 213 |
-
|
| 214 |
-
if column not in available
|
| 215 |
-
]
|
| 216 |
|
| 217 |
-
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
|
| 220 |
-
"filename": filename,
|
| 221 |
-
"status": "missing_required_columns",
|
| 222 |
-
"missing_columns": missing,
|
| 223 |
-
"rows_examined": 0,
|
| 224 |
-
"valid_pairs": 0
|
| 225 |
-
})
|
| 226 |
|
| 227 |
-
|
| 228 |
-
f"ERRO: colunas ausentes: {missing}"
|
| 229 |
-
)
|
| 230 |
|
| 231 |
-
|
| 232 |
|
| 233 |
-
|
|
|
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
# Lê somente as colunas necessárias.
|
| 238 |
-
for batch in parquet.iter_batches(
|
| 239 |
-
batch_size=1000,
|
| 240 |
-
columns=REQUIRED_COLUMNS
|
| 241 |
-
):
|
| 242 |
-
|
| 243 |
-
for row in batch.to_pylist():
|
| 244 |
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
-
|
|
|
|
|
|
|
| 248 |
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
-
|
| 252 |
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
-
|
| 256 |
|
| 257 |
-
|
| 258 |
|
| 259 |
-
|
| 260 |
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
)
|
| 267 |
|
| 268 |
-
|
| 269 |
-
"filename": filename,
|
| 270 |
-
"status": "processed",
|
| 271 |
-
"rows_examined": file_rows,
|
| 272 |
-
"valid_pairs": file_valid
|
| 273 |
-
})
|
| 274 |
|
| 275 |
print(
|
| 276 |
-
f"
|
| 277 |
-
f"{file_valid} pares válidos."
|
| 278 |
)
|
| 279 |
|
| 280 |
-
|
| 281 |
-
pairs,
|
| 282 |
-
books,
|
| 283 |
-
discarded,
|
| 284 |
-
total_rows,
|
| 285 |
-
file_reports
|
| 286 |
-
)
|
| 287 |
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
-
#
|
| 290 |
-
#
|
| 291 |
-
#
|
| 292 |
|
| 293 |
-
|
| 294 |
|
| 295 |
-
|
|
|
|
| 296 |
|
| 297 |
-
|
| 298 |
|
| 299 |
-
|
|
|
|
| 300 |
|
| 301 |
-
|
| 302 |
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
| 304 |
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
-
|
|
|
|
|
|
|
| 308 |
|
| 309 |
-
|
| 310 |
|
| 311 |
-
|
|
|
|
|
|
|
| 312 |
|
| 313 |
else:
|
| 314 |
|
| 315 |
-
|
| 316 |
|
| 317 |
-
|
|
|
|
|
|
|
| 318 |
|
|
|
|
| 319 |
|
| 320 |
-
|
| 321 |
|
| 322 |
-
|
| 323 |
-
items,
|
| 324 |
-
key=lambda x: (
|
| 325 |
-
x["book"],
|
| 326 |
-
x["chapter"],
|
| 327 |
-
x["verse"]
|
| 328 |
-
)
|
| 329 |
-
)
|
| 330 |
|
|
|
|
| 331 |
|
| 332 |
-
|
| 333 |
|
| 334 |
-
|
| 335 |
-
sorted(
|
| 336 |
-
Counter(
|
| 337 |
-
x["book"] for x in items
|
| 338 |
-
).items()
|
| 339 |
-
)
|
| 340 |
-
)
|
| 341 |
|
|
|
|
| 342 |
|
| 343 |
-
|
| 344 |
-
# SALVAMENTO JSONL
|
| 345 |
-
# ============================================================
|
| 346 |
|
| 347 |
-
|
| 348 |
|
| 349 |
-
|
| 350 |
-
path,
|
| 351 |
-
"w",
|
| 352 |
-
encoding="utf-8"
|
| 353 |
-
) as output:
|
| 354 |
|
| 355 |
-
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
ensure_ascii=False
|
| 361 |
-
) + "\n"
|
| 362 |
-
)
|
| 363 |
|
| 364 |
|
| 365 |
# ============================================================
|
|
@@ -368,323 +339,100 @@ def save_jsonl(items, path):
|
|
| 368 |
|
| 369 |
def main():
|
| 370 |
|
| 371 |
-
print("=" *
|
| 372 |
-
|
| 373 |
-
print(
|
| 374 |
-
"SEEDS — PREPARAÇÃO REPRODUZÍVEL DO CORPUS"
|
| 375 |
-
)
|
| 376 |
-
|
| 377 |
-
print("=" * 65)
|
| 378 |
|
| 379 |
print(
|
| 380 |
-
"
|
| 381 |
-
)
|
| 382 |
-
|
| 383 |
-
os.makedirs(
|
| 384 |
-
OUTPUT_DIR,
|
| 385 |
-
exist_ok=True
|
| 386 |
-
)
|
| 387 |
-
|
| 388 |
-
# 1. Localizar os arquivos Parquet.
|
| 389 |
-
|
| 390 |
-
parquet_files, config = get_parquet_files()
|
| 391 |
-
|
| 392 |
-
# 2. Extrair os pares alinhados.
|
| 393 |
-
|
| 394 |
-
(
|
| 395 |
-
pairs,
|
| 396 |
-
books,
|
| 397 |
-
discarded,
|
| 398 |
-
total_rows,
|
| 399 |
-
file_reports
|
| 400 |
-
) = extract_pairs(parquet_files)
|
| 401 |
-
|
| 402 |
-
# 3. Verificar se todos os arquivos foram processados.
|
| 403 |
-
|
| 404 |
-
skipped = [
|
| 405 |
-
file
|
| 406 |
-
for file in file_reports
|
| 407 |
-
if file["status"] != "processed"
|
| 408 |
-
]
|
| 409 |
-
|
| 410 |
-
if skipped:
|
| 411 |
-
|
| 412 |
-
raise RuntimeError(
|
| 413 |
-
"Preparação cancelada: pelo menos um Parquet não contém "
|
| 414 |
-
"todas as colunas obrigatórias. Nenhum dataset final foi "
|
| 415 |
-
f"salvo. Arquivos problemáticos: {skipped}"
|
| 416 |
-
)
|
| 417 |
-
|
| 418 |
-
if not pairs:
|
| 419 |
-
|
| 420 |
-
raise RuntimeError(
|
| 421 |
-
"Nenhum par alinhado válido foi encontrado."
|
| 422 |
-
)
|
| 423 |
-
|
| 424 |
-
# 4. Dividir por livro.
|
| 425 |
-
|
| 426 |
-
train, validation, test = split_by_book(pairs)
|
| 427 |
-
|
| 428 |
-
train, validation, test = map(
|
| 429 |
-
sort_pairs,
|
| 430 |
-
(train, validation, test)
|
| 431 |
)
|
| 432 |
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
raise RuntimeError(
|
| 436 |
-
"Um dos conjuntos ficou vazio. Verifique os livros "
|
| 437 |
-
"definidos para validação e teste."
|
| 438 |
-
)
|
| 439 |
-
|
| 440 |
-
# 5. Verificar sobreposição entre os conjuntos.
|
| 441 |
-
|
| 442 |
-
train_books = {
|
| 443 |
-
x["book"] for x in train
|
| 444 |
-
}
|
| 445 |
-
|
| 446 |
-
val_books = {
|
| 447 |
-
x["book"] for x in validation
|
| 448 |
-
}
|
| 449 |
|
| 450 |
-
|
| 451 |
-
x["book"] for x in test
|
| 452 |
-
}
|
| 453 |
|
| 454 |
-
|
| 455 |
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
),
|
| 459 |
|
| 460 |
-
|
| 461 |
-
train_books & test_books
|
| 462 |
-
),
|
| 463 |
|
| 464 |
-
|
| 465 |
-
|
|
|
|
| 466 |
)
|
| 467 |
|
| 468 |
-
|
| 469 |
|
| 470 |
-
|
|
|
|
|
|
|
| 471 |
|
| 472 |
-
|
| 473 |
-
f"Há livros compartilhados entre conjuntos: {overlaps}"
|
| 474 |
-
)
|
| 475 |
|
| 476 |
-
|
| 477 |
|
| 478 |
-
|
| 479 |
|
| 480 |
-
|
| 481 |
-
set(BOOKS_FOR_VALIDATION) - available_books
|
| 482 |
-
)
|
| 483 |
|
| 484 |
-
|
| 485 |
-
|
|
|
|
|
|
|
| 486 |
)
|
| 487 |
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
save_jsonl(train, TRAIN_FILE)
|
| 491 |
-
|
| 492 |
-
save_jsonl(validation, VALIDATION_FILE)
|
| 493 |
-
|
| 494 |
-
save_jsonl(test, TEST_FILE)
|
| 495 |
-
|
| 496 |
-
# 8. Calcular estatísticas.
|
| 497 |
-
|
| 498 |
-
total_pairs = len(pairs)
|
| 499 |
-
|
| 500 |
-
def percentage(n):
|
| 501 |
-
|
| 502 |
-
return round(
|
| 503 |
-
100 * n / total_pairs,
|
| 504 |
-
2
|
| 505 |
-
)
|
| 506 |
-
|
| 507 |
-
refs = Counter(
|
| 508 |
-
x["reference"] for x in pairs
|
| 509 |
-
)
|
| 510 |
-
|
| 511 |
-
duplicate_refs = {
|
| 512 |
-
k: v
|
| 513 |
-
for k, v in refs.items()
|
| 514 |
-
if v > 1
|
| 515 |
-
}
|
| 516 |
-
|
| 517 |
-
# 9. Construir relatório.
|
| 518 |
-
|
| 519 |
-
report = {
|
| 520 |
-
|
| 521 |
-
"generated_at_utc":
|
| 522 |
-
datetime.now(timezone.utc).isoformat(),
|
| 523 |
-
|
| 524 |
-
"dataset": DATASET_NAME,
|
| 525 |
-
|
| 526 |
-
"dataset_config": config,
|
| 527 |
-
|
| 528 |
-
"source_column": SOURCE_COLUMN,
|
| 529 |
-
|
| 530 |
-
"target_column": TARGET_COLUMN,
|
| 531 |
-
|
| 532 |
-
"language_pair": "English -> Macushi",
|
| 533 |
-
|
| 534 |
-
"parquet_files": file_reports,
|
| 535 |
-
|
| 536 |
-
"raw_rows_examined": total_rows,
|
| 537 |
-
|
| 538 |
-
"discarded_rows": sum(discarded.values()),
|
| 539 |
-
|
| 540 |
-
"discard_reasons": dict(
|
| 541 |
-
sorted(discarded.items())
|
| 542 |
-
),
|
| 543 |
-
|
| 544 |
-
"total_aligned_pairs": total_pairs,
|
| 545 |
-
|
| 546 |
-
"train_pairs": len(train),
|
| 547 |
-
|
| 548 |
-
"validation_pairs": len(validation),
|
| 549 |
-
|
| 550 |
-
"test_pairs": len(test),
|
| 551 |
-
|
| 552 |
-
"train_percentage": percentage(len(train)),
|
| 553 |
-
|
| 554 |
-
"validation_percentage": percentage(len(validation)),
|
| 555 |
-
|
| 556 |
-
"test_percentage": percentage(len(test)),
|
| 557 |
-
|
| 558 |
-
"all_books": dict(
|
| 559 |
-
sorted(books.items())
|
| 560 |
-
),
|
| 561 |
-
|
| 562 |
-
"train_books": count_books(train),
|
| 563 |
-
|
| 564 |
-
"validation_books": count_books(validation),
|
| 565 |
-
|
| 566 |
-
"test_books": count_books(test),
|
| 567 |
-
|
| 568 |
-
"validation_book_selection":
|
| 569 |
-
BOOKS_FOR_VALIDATION,
|
| 570 |
-
|
| 571 |
-
"test_book_selection":
|
| 572 |
-
BOOKS_FOR_TEST,
|
| 573 |
-
|
| 574 |
-
"split_validation": {
|
| 575 |
-
|
| 576 |
-
"book_overlap": False,
|
| 577 |
-
|
| 578 |
-
"overlaps": overlaps,
|
| 579 |
-
|
| 580 |
-
"missing_validation_books": missing_val,
|
| 581 |
-
|
| 582 |
-
"missing_test_books": missing_test
|
| 583 |
-
|
| 584 |
-
},
|
| 585 |
-
|
| 586 |
-
"duplicated_reference_count":
|
| 587 |
-
len(duplicate_refs),
|
| 588 |
-
|
| 589 |
-
"duplicated_references_sample": dict(
|
| 590 |
-
list(
|
| 591 |
-
sorted(duplicate_refs.items())
|
| 592 |
-
)[:20]
|
| 593 |
-
),
|
| 594 |
-
|
| 595 |
-
"preprocessing": {
|
| 596 |
-
|
| 597 |
-
"trim_whitespace": True,
|
| 598 |
-
|
| 599 |
-
"collapse_repeated_whitespace": True,
|
| 600 |
-
|
| 601 |
-
"remove_empty_source_or_target": True,
|
| 602 |
-
|
| 603 |
-
"remove_markup_starting_with_angle_bracket": True,
|
| 604 |
-
|
| 605 |
-
"split_strategy": "book-level",
|
| 606 |
-
|
| 607 |
-
"automatic_deduplication": False,
|
| 608 |
-
|
| 609 |
-
"parquet_reading":
|
| 610 |
-
"individual files; required columns only"
|
| 611 |
-
|
| 612 |
-
}
|
| 613 |
-
|
| 614 |
-
}
|
| 615 |
-
|
| 616 |
-
with open(
|
| 617 |
-
REPORT_FILE,
|
| 618 |
-
"w",
|
| 619 |
-
encoding="utf-8"
|
| 620 |
-
) as output:
|
| 621 |
-
|
| 622 |
-
json.dump(
|
| 623 |
-
report,
|
| 624 |
-
output,
|
| 625 |
-
ensure_ascii=False,
|
| 626 |
-
indent=2
|
| 627 |
-
)
|
| 628 |
-
|
| 629 |
-
# 10. Exibir resumo.
|
| 630 |
-
|
| 631 |
-
print("\n" + "=" * 65)
|
| 632 |
-
|
| 633 |
-
print("RESUMO DA PREPARAÇÃO")
|
| 634 |
-
|
| 635 |
-
print("=" * 65)
|
| 636 |
-
|
| 637 |
-
print(f"Linhas examinadas: {total_rows}")
|
| 638 |
-
|
| 639 |
-
print(f"Pares válidos: {total_pairs}")
|
| 640 |
|
| 641 |
print(
|
| 642 |
-
f"
|
| 643 |
)
|
| 644 |
|
| 645 |
print(
|
| 646 |
-
f"
|
| 647 |
-
f"({percentage(len(train))}%)"
|
| 648 |
)
|
| 649 |
|
| 650 |
print(
|
| 651 |
-
f"
|
| 652 |
-
f"({percentage(len(validation))}%)"
|
| 653 |
)
|
| 654 |
|
| 655 |
-
|
| 656 |
-
f"Teste: {len(test)} "
|
| 657 |
-
f"({percentage(len(test))}%)"
|
| 658 |
-
)
|
| 659 |
|
| 660 |
-
|
|
|
|
|
|
|
| 661 |
|
| 662 |
-
|
|
|
|
|
|
|
| 663 |
|
| 664 |
-
print(
|
|
|
|
|
|
|
| 665 |
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
)
|
| 670 |
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
|
|
|
|
|
|
| 675 |
|
| 676 |
-
|
|
|
|
|
|
|
|
|
|
| 677 |
|
| 678 |
-
|
| 679 |
-
TRAIN_FILE,
|
| 680 |
-
VALIDATION_FILE,
|
| 681 |
-
TEST_FILE,
|
| 682 |
-
REPORT_FILE
|
| 683 |
-
):
|
| 684 |
|
| 685 |
-
|
|
|
|
|
|
|
|
|
|
| 686 |
|
| 687 |
-
print("
|
| 688 |
|
| 689 |
|
| 690 |
if __name__ == "__main__":
|
|
|
|
| 1 |
|
|
|
|
| 2 |
import os
|
| 3 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
+
import pyarrow.parquet as pq
|
| 6 |
|
| 7 |
|
| 8 |
# ============================================================
|
|
|
|
| 11 |
|
| 12 |
DATASET_NAME = "DavidCBaines/ebible_corpus"
|
| 13 |
|
| 14 |
+
PARQUET_API = (
|
| 15 |
+
"https://datasets-server.huggingface.co/parquet"
|
| 16 |
+
f"?dataset={DATASET_NAME}"
|
| 17 |
+
)
|
| 18 |
|
| 19 |
+
TIMEOUT = (30, 180)
|
| 20 |
+
|
| 21 |
+
# Termos utilizados para localizar colunas relevantes.
|
| 22 |
+
SEARCH_TERMS = [
|
| 23 |
"book",
|
| 24 |
"chapter",
|
| 25 |
"verse",
|
| 26 |
+
"eng",
|
| 27 |
+
"english",
|
| 28 |
+
"mbc",
|
| 29 |
+
"macuxi",
|
| 30 |
+
"macushi",
|
| 31 |
+
"language"
|
| 32 |
]
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# ============================================================
|
| 36 |
# DESCOBERTA DOS PARQUETS
|
|
|
|
| 40 |
|
| 41 |
print("Consultando arquivos Parquet no Hugging Face...")
|
| 42 |
|
| 43 |
+
response = requests.get(
|
| 44 |
+
PARQUET_API,
|
| 45 |
+
timeout=90
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
response.raise_for_status()
|
| 49 |
|
| 50 |
+
files = response.json().get(
|
| 51 |
+
"parquet_files",
|
| 52 |
+
[]
|
| 53 |
+
)
|
| 54 |
|
| 55 |
if not files:
|
| 56 |
raise RuntimeError(
|
| 57 |
+
"Nenhum arquivo Parquet foi encontrado."
|
| 58 |
)
|
| 59 |
|
| 60 |
+
print(
|
| 61 |
+
f"\nTotal de arquivos encontrados: {len(files)}"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
for index, file in enumerate(files, 1):
|
| 65 |
|
| 66 |
+
print(f"\nArquivo {index}")
|
| 67 |
+
|
| 68 |
+
print(f" Nome: {file.get('filename')}")
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
print(f" Configuração: {file.get('config')}")
|
|
|
|
| 71 |
|
| 72 |
+
print(f" Split: {file.get('split')}")
|
| 73 |
+
|
| 74 |
+
print(f" Tamanho: {file.get('size')}")
|
| 75 |
+
|
| 76 |
+
return files
|
| 77 |
|
| 78 |
|
| 79 |
# ============================================================
|
|
|
|
| 85 |
with requests.get(
|
| 86 |
url,
|
| 87 |
stream=True,
|
| 88 |
+
timeout=TIMEOUT
|
| 89 |
) as response:
|
| 90 |
|
| 91 |
response.raise_for_status()
|
|
|
|
| 101 |
|
| 102 |
|
| 103 |
# ============================================================
|
| 104 |
+
# IDENTIFICAÇÃO DE COLUNAS RELEVANTES
|
| 105 |
# ============================================================
|
| 106 |
|
| 107 |
+
def find_relevant_columns(columns):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
relevant = []
|
| 110 |
|
| 111 |
+
for column in columns:
|
| 112 |
|
| 113 |
+
normalized = column.lower()
|
| 114 |
|
| 115 |
+
if any(
|
| 116 |
+
term in normalized
|
| 117 |
+
for term in SEARCH_TERMS
|
| 118 |
+
):
|
| 119 |
|
| 120 |
+
relevant.append(column)
|
|
|
|
| 121 |
|
| 122 |
+
return relevant
|
|
|
|
| 123 |
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# ============================================================
|
| 126 |
+
# INSPEÇÃO INDIVIDUAL DOS PARQUETS
|
| 127 |
+
# ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
def inspect_parquet(index, info):
|
|
|
|
| 130 |
|
| 131 |
+
filename = info.get(
|
| 132 |
+
"filename",
|
| 133 |
+
f"shard_{index}"
|
| 134 |
+
)
|
| 135 |
|
| 136 |
+
print("\n" + "=" * 75)
|
|
|
|
| 137 |
|
| 138 |
+
print(
|
| 139 |
+
f"INSPEÇÃO DO ARQUIVO {index}: {filename}"
|
| 140 |
+
)
|
| 141 |
|
| 142 |
+
print("=" * 75)
|
| 143 |
|
| 144 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 145 |
|
| 146 |
+
local_file = os.path.join(
|
| 147 |
+
temp_dir,
|
| 148 |
+
"inspection.parquet"
|
| 149 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
print("\nBaixando arquivo...")
|
| 152 |
|
| 153 |
+
download_parquet(
|
| 154 |
+
info["url"],
|
| 155 |
+
local_file
|
| 156 |
+
)
|
|
|
|
| 157 |
|
| 158 |
+
parquet = pq.ParquetFile(local_file)
|
| 159 |
|
| 160 |
+
# ----------------------------------------------------
|
| 161 |
+
# INFORMAÇÕES GERAIS
|
| 162 |
+
# ----------------------------------------------------
|
| 163 |
|
| 164 |
+
total_rows = parquet.metadata.num_rows
|
| 165 |
|
| 166 |
+
columns = parquet.schema.names
|
| 167 |
|
| 168 |
+
print("\nINFORMAÇÕES GERAIS")
|
| 169 |
|
| 170 |
+
print(f"Linhas: {total_rows}")
|
| 171 |
|
| 172 |
+
print(f"Quantidade de colunas: {len(columns)}")
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
print(
|
| 175 |
+
f"Quantidade de grupos: "
|
| 176 |
+
f"{parquet.metadata.num_row_groups}"
|
| 177 |
)
|
| 178 |
|
| 179 |
+
# ----------------------------------------------------
|
| 180 |
+
# TODAS AS COLUNAS
|
| 181 |
+
# ----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
+
print("\nCOLUNAS DISPONÍVEIS")
|
| 184 |
|
| 185 |
+
for column_index, column in enumerate(
|
| 186 |
+
columns,
|
| 187 |
+
1
|
| 188 |
+
):
|
| 189 |
|
| 190 |
+
print(
|
| 191 |
+
f"{column_index:03d}. {column}"
|
| 192 |
+
)
|
|
|
|
|
|
|
| 193 |
|
| 194 |
+
# ----------------------------------------------------
|
| 195 |
+
# COLUNAS POTENCIALMENTE RELEVANTES
|
| 196 |
+
# ----------------------------------------------------
|
| 197 |
|
| 198 |
+
relevant = find_relevant_columns(columns)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
print("\nCOLUNAS POTENCIALMENTE RELEVANTES")
|
|
|
|
|
|
|
| 201 |
|
| 202 |
+
if relevant:
|
| 203 |
|
| 204 |
+
for column in relevant:
|
| 205 |
+
print(f" - {column}")
|
| 206 |
|
| 207 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
+
print(
|
| 210 |
+
"Nenhuma coluna correspondente aos termos "
|
| 211 |
+
"de busca foi encontrada."
|
| 212 |
+
)
|
| 213 |
|
| 214 |
+
# ----------------------------------------------------
|
| 215 |
+
# VERIFICAÇÃO DAS COLUNAS ESPERADAS
|
| 216 |
+
# ----------------------------------------------------
|
| 217 |
|
| 218 |
+
expected = [
|
| 219 |
+
"book",
|
| 220 |
+
"chapter",
|
| 221 |
+
"verse",
|
| 222 |
+
"eng-web",
|
| 223 |
+
"mbcNT"
|
| 224 |
+
]
|
| 225 |
|
| 226 |
+
available = set(columns)
|
| 227 |
|
| 228 |
+
missing = [
|
| 229 |
+
column
|
| 230 |
+
for column in expected
|
| 231 |
+
if column not in available
|
| 232 |
+
]
|
| 233 |
|
| 234 |
+
print("\nVERIFICAÇÃO DO ESQUEMA")
|
| 235 |
|
| 236 |
+
print("Colunas esperadas:")
|
| 237 |
|
| 238 |
+
for column in expected:
|
| 239 |
|
| 240 |
+
status = (
|
| 241 |
+
"ENCONTRADA"
|
| 242 |
+
if column in available
|
| 243 |
+
else "AUSENTE"
|
| 244 |
+
)
|
| 245 |
|
| 246 |
+
print(
|
| 247 |
+
f" {column}: {status}"
|
| 248 |
+
)
|
|
|
|
| 249 |
|
| 250 |
+
if missing:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
print(
|
| 253 |
+
f"\nColunas ausentes: {missing}"
|
|
|
|
| 254 |
)
|
| 255 |
|
| 256 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
+
print(
|
| 259 |
+
"\nTodas as colunas esperadas estão presentes."
|
| 260 |
+
)
|
| 261 |
|
| 262 |
+
# ----------------------------------------------------
|
| 263 |
+
# AMOSTRA DOS REGISTROS
|
| 264 |
+
# ----------------------------------------------------
|
| 265 |
|
| 266 |
+
print("\nAMOSTRA DOS REGISTROS")
|
| 267 |
|
| 268 |
+
# Prioriza colunas relevantes.
|
| 269 |
+
# Limita a amostra para evitar logs excessivamente grandes.
|
| 270 |
|
| 271 |
+
sample_columns = relevant[:15]
|
| 272 |
|
| 273 |
+
if not sample_columns:
|
| 274 |
+
sample_columns = columns[:10]
|
| 275 |
|
| 276 |
+
if total_rows > 0:
|
| 277 |
|
| 278 |
+
sample = parquet.read_row_group(
|
| 279 |
+
0,
|
| 280 |
+
columns=sample_columns
|
| 281 |
+
).slice(0, 3)
|
| 282 |
|
| 283 |
+
for row_index, row in enumerate(
|
| 284 |
+
sample.to_pylist(),
|
| 285 |
+
1
|
| 286 |
+
):
|
| 287 |
|
| 288 |
+
print(
|
| 289 |
+
f"\nRegistro {row_index}:"
|
| 290 |
+
)
|
| 291 |
|
| 292 |
+
for key, value in row.items():
|
| 293 |
|
| 294 |
+
print(
|
| 295 |
+
f" {key}: {str(value)[:300]}"
|
| 296 |
+
)
|
| 297 |
|
| 298 |
else:
|
| 299 |
|
| 300 |
+
print("Arquivo sem registros.")
|
| 301 |
|
| 302 |
+
# ----------------------------------------------------
|
| 303 |
+
# RESUMO
|
| 304 |
+
# ----------------------------------------------------
|
| 305 |
|
| 306 |
+
result = {
|
| 307 |
|
| 308 |
+
"filename": filename,
|
| 309 |
|
| 310 |
+
"rows": total_rows,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
+
"column_count": len(columns),
|
| 313 |
|
| 314 |
+
"columns": columns,
|
| 315 |
|
| 316 |
+
"relevant_columns": relevant,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
+
"missing_expected_columns": missing,
|
| 319 |
|
| 320 |
+
"schema_compatible": len(missing) == 0
|
|
|
|
|
|
|
| 321 |
|
| 322 |
+
}
|
| 323 |
|
| 324 |
+
print("\nRESUMO DO ARQUIVO")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
+
print(
|
| 327 |
+
f"Compatível com o esquema esperado: "
|
| 328 |
+
f"{result['schema_compatible']}"
|
| 329 |
+
)
|
| 330 |
|
| 331 |
+
print("=" * 75)
|
| 332 |
+
|
| 333 |
+
return result
|
|
|
|
|
|
|
|
|
|
| 334 |
|
| 335 |
|
| 336 |
# ============================================================
|
|
|
|
| 339 |
|
| 340 |
def main():
|
| 341 |
|
| 342 |
+
print("\n" + "=" * 75)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
print(
|
| 345 |
+
"SEEDS — DIAGNÓSTICO DA ESTRUTURA DO CORPUS"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
)
|
| 347 |
|
| 348 |
+
print("=" * 75)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
|
| 350 |
+
files = get_parquet_files()
|
|
|
|
|
|
|
| 351 |
|
| 352 |
+
results = []
|
| 353 |
|
| 354 |
+
# Não combinamos os Parquets.
|
| 355 |
+
# Cada arquivo é inspecionado individualmente.
|
|
|
|
| 356 |
|
| 357 |
+
for index, info in enumerate(files, 1):
|
|
|
|
|
|
|
| 358 |
|
| 359 |
+
result = inspect_parquet(
|
| 360 |
+
index,
|
| 361 |
+
info
|
| 362 |
)
|
| 363 |
|
| 364 |
+
results.append(result)
|
| 365 |
|
| 366 |
+
# --------------------------------------------------------
|
| 367 |
+
# RESUMO CONSOLIDADO
|
| 368 |
+
# --------------------------------------------------------
|
| 369 |
|
| 370 |
+
print("\n" + "=" * 75)
|
|
|
|
|
|
|
| 371 |
|
| 372 |
+
print("RESUMO CONSOLIDADO DA INSPEÇÃO")
|
| 373 |
|
| 374 |
+
print("=" * 75)
|
| 375 |
|
| 376 |
+
total_files = len(results)
|
|
|
|
|
|
|
| 377 |
|
| 378 |
+
compatible = sum(
|
| 379 |
+
1
|
| 380 |
+
for result in results
|
| 381 |
+
if result["schema_compatible"]
|
| 382 |
)
|
| 383 |
|
| 384 |
+
incompatible = total_files - compatible
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
print(
|
| 387 |
+
f"\nArquivos encontrados: {total_files}"
|
| 388 |
)
|
| 389 |
|
| 390 |
print(
|
| 391 |
+
f"Arquivos compatíveis: {compatible}"
|
|
|
|
| 392 |
)
|
| 393 |
|
| 394 |
print(
|
| 395 |
+
f"Arquivos incompatíveis: {incompatible}"
|
|
|
|
| 396 |
)
|
| 397 |
|
| 398 |
+
for result in results:
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
+
print(
|
| 401 |
+
f"\n{result['filename']}"
|
| 402 |
+
)
|
| 403 |
|
| 404 |
+
print(
|
| 405 |
+
f" Linhas: {result['rows']}"
|
| 406 |
+
)
|
| 407 |
|
| 408 |
+
print(
|
| 409 |
+
f" Colunas: {result['column_count']}"
|
| 410 |
+
)
|
| 411 |
|
| 412 |
+
print(
|
| 413 |
+
f" Compatível: {result['schema_compatible']}"
|
| 414 |
+
)
|
|
|
|
| 415 |
|
| 416 |
+
print(
|
| 417 |
+
f" Colunas relevantes: "
|
| 418 |
+
f"{result['relevant_columns']}"
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
if result["missing_expected_columns"]:
|
| 422 |
|
| 423 |
+
print(
|
| 424 |
+
f" Colunas ausentes: "
|
| 425 |
+
f"{result['missing_expected_columns']}"
|
| 426 |
+
)
|
| 427 |
|
| 428 |
+
print("\n" + "=" * 75)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
|
| 430 |
+
print(
|
| 431 |
+
"INSPEÇÃO CONCLUÍDA. "
|
| 432 |
+
"Nenhum arquivo de treinamento foi modificado."
|
| 433 |
+
)
|
| 434 |
|
| 435 |
+
print("=" * 75)
|
| 436 |
|
| 437 |
|
| 438 |
if __name__ == "__main__":
|