|
|
|
|
|
import argparse |
|
|
import json |
|
|
import re |
|
|
from pathlib import Path |
|
|
|
|
|
import orjson |
|
|
|
|
|
|
|
|
def clean_text(t: str) -> str: |
|
|
t = t.strip() |
|
|
|
|
|
t = re.sub(r"\s+", " ", t) |
|
|
|
|
|
t = re.sub(r"^[\-\β\β\.\,\;\:\!\?\"\']+\s*", "", t) |
|
|
t = re.sub(r"\s*[\-\β\β\.\,\;\:\!\?\"\']+$", "", t) |
|
|
|
|
|
t = t.replace("β", "'").replace("β", '"').replace("β", '"') |
|
|
|
|
|
if not re.search(r"[A-Z]{2,}", t): |
|
|
t = t.lower() |
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|