Yağmur Tuncer
Upload scripts/validate_dataset.py with huggingface_hub
8ca7769 verified
Raw
History Blame Contribute Delete
2.95 kB
from __future__ import annotations
import argparse
import csv
from collections import Counter
from pathlib import Path
EXPECTED_COLUMNS = [
"id",
"input_text",
"normalized_text",
"task_type",
"style",
"domain",
"source",
"license",
"split",
]
EXPECTED_SPLITS = {
"data/train.csv": "train",
"data/test.csv": "test",
}
def read_rows(path: Path) -> tuple[list[str], list[dict[str, str]]]:
with path.open("r", encoding="utf-8", newline="") as handle:
reader = csv.DictReader(handle)
return list(reader.fieldnames or []), list(reader)
def validate_file(path: Path, expected_split: str | None) -> list[str]:
errors: list[str] = []
fieldnames, rows = read_rows(path)
if fieldnames != EXPECTED_COLUMNS:
errors.append(f"{path}: unexpected columns: {fieldnames}")
if not rows:
errors.append(f"{path}: file has no rows")
seen_ids: set[str] = set()
for index, row in enumerate(rows, start=2):
row_id = row.get("id", "")
if not row_id:
errors.append(f"{path}:{index}: missing id")
elif row_id in seen_ids:
errors.append(f"{path}:{index}: duplicate id {row_id}")
seen_ids.add(row_id)
for column in EXPECTED_COLUMNS:
if not row.get(column):
errors.append(f"{path}:{index}: missing value in {column}")
if expected_split and row.get("split") != expected_split:
errors.append(f"{path}:{index}: expected split {expected_split}, got {row.get('split')}")
if row.get("input_text", "").strip() == row.get("normalized_text", "").strip():
errors.append(f"{path}:{index}: input_text equals normalized_text")
return errors
def print_summary(path: Path) -> None:
_, rows = read_rows(path)
print(f"{path}: {len(rows):,} rows")
for column in ("task_type", "style", "domain", "source"):
counts = Counter(row[column] for row in rows)
formatted = ", ".join(f"{key}={value}" for key, value in sorted(counts.items()))
print(f" {column}: {formatted}")
def main() -> int:
parser = argparse.ArgumentParser(description="Validate Turkish Chat Normalization Mini CSV files.")
parser.add_argument("paths", nargs="*", type=Path, default=[Path("data/train.csv"), Path("data/test.csv")])
args = parser.parse_args()
all_errors: list[str] = []
for path in args.paths:
expected_split = EXPECTED_SPLITS.get(path.as_posix().replace("\\", "/"))
all_errors.extend(validate_file(path, expected_split))
print_summary(path)
if all_errors:
print("\nValidation failed:")
for error in all_errors[:50]:
print(f" {error}")
if len(all_errors) > 50:
print(f" ... {len(all_errors) - 50} more")
return 1
print("\nValidation passed.")
return 0
if __name__ == "__main__":
raise SystemExit(main())