french-education-speech / clean_texts.py
mathisescriva
Initial commit: French Education Speech Corpus (Phase 1)
5d2c0a9
#!/usr/bin/env python3
import argparse
import json
import re
from pathlib import Path
import orjson
def clean_text(t: str) -> str:
t = t.strip()
# normalize whitespace
t = re.sub(r"\s+", " ", t)
# remove leading/trailing punctuation artifacts
t = re.sub(r"^[\-\–\β€”\.\,\;\:\!\?\"\']+\s*", "", t)
t = re.sub(r"\s*[\-\–\β€”\.\,\;\:\!\?\"\']+$", "", t)
# normalize quotes
t = t.replace("’", "'").replace("β€œ", '"').replace("”", '"')
# lowercase except acronyms (simple heuristic)
if not re.search(r"[A-Z]{2,}", t):
t = t.lower()
# remove filler tokens common in ASR
t = re.sub(r"\b(euh+|heu+|hum+)\b", "", t)
t = re.sub(r"\s+", " ", t).strip()
return t
def main() -> None:
parser = argparse.ArgumentParser(description="Clean transcript texts in-place or to a new file.")
parser.add_argument("--jsonl", required=True, help="Input JSONL")
parser.add_argument("--out", required=True, help="Output JSONL (can overwrite input)")
args = parser.parse_args()
with open(args.out, "w", encoding="utf-8") as out_f, open(args.jsonl, "r", encoding="utf-8") as in_f:
for line in in_f:
if not line.strip():
continue
rec = json.loads(line)
if "text" in rec and rec["text"]:
rec["text"] = clean_text(rec["text"])
out_f.write(orjson.dumps(rec).decode("utf-8") + "\n")
print(f"Cleaned texts -> {args.out}")
if __name__ == "__main__":
main()