Download clean-transcript.py from baniguru/speech_data_recordings: direct link, hf CLI and curl.
- Browser
- Download file 5.34 kB
-
https://huggingface.co/datasets/baniguru/speech_data_recordings/resolve/main/clean-transcript.py
- Command line
-
hf download hf://datasets/baniguru/speech_data_recordings/clean-transcript.py
-
curl -L -o clean-transcript.py https://huggingface.co/datasets/baniguru/speech_data_recordings/resolve/main/clean-transcript.py
5.34 kB
| import argparse | |
| import json | |
| import re | |
| from pathlib import Path | |
| # search | |
| # "transcript"\s*:\s*"[^"]*[^\u0A00-\u0A7F\s][^"]*" | |
| # "transcript"\s*:\s*"[^"]*(?:ਅਾ|ੲਿ|ੲੀ|ੳੁ|ੳੂ|ੲੇ|ਅੈ|ੳੋ|ਅੌ)[^"]*" | |
| # ੍[ਾਿੀੁੂੇੈੋੌ] | |
| # [੍ੑਂੰਁੱ][ਾਿੀੁੂੇੈੋੌ] | |
| # (ਪਹਿਲਾ|ਦੂਜਾ|ਤੀਜਾ|ਚੌਥਾ|ਪੰਜਵਾ|ਛੇਵਾ|ਸਤਵਾ|ਅਠਵਾ|ਨੌਵਾ|ਦਸਵਾ)(?=[\u0A00-\u0A7F]) | |
| # ॥ਪਹਿਲਾਰਹਾਉ ਦੂਜਾ | |
| # todo: check matra split issues including ਦ੍ਰਿੜ੍ੁ | |
| # Folder where this Python script is located | |
| BASE_DIR = Path(__file__).resolve().parent | |
| # Exact word/text replacements | |
| REPLACEMENTS = { | |
| # "ਦ੍ਰਿੜ੍ੁ": "ਦ੍ਰਿੜੑੁ", | |
| "ਦ੍ਰਿਸਿ੍ਟ": "ਦ੍ਰਿਸ੍ਟਿ", | |
| "ਦ੍ਰਿਸਿ†": "ਦ੍ਰਿਸ੍ਟਿ", | |
| "ਬਿਸ†ਾ": "ਬਿਸ੍ਟਾ", | |
| "ਸ੍ਰਿਸਿ†": "ਸ੍ਰਿਸ੍ਟਿ", | |
| "ਸ੍ਰਿਸਿ੍ਟ": "ਸ੍ਰਿਸ੍ਟਿ", | |
| "ਗਰਿਸ†": "ਗਰਿਸ੍ਟ", | |
| "ਮਿਸ†": "ਮਿਸ੍ਟ", | |
| "ਇਸ਼†": "ਇਸ਼ਟ", | |
| "ਇਸ˜ਾਨੁ": "ਇਸ੍ਨਾਨੁ", | |
| "ਕ੍ਰਿਸ˜ਾ": "ਕ੍ਰਿਸ੍ਨਾ", | |
| "ਕ੍ਰਿਸ˜ੰ": "ਕ੍ਰਿਸ੍ਨੰ", | |
| "ਕ੍ਰਿਸ˜": "ਕ੍ਰਿਸ੍ਨ", | |
| "ਸ˜ੇਹੰ": "ਸ੍ਨੇਹੰ", | |
| "ਸ˜ੇਹ": "ਸ੍ਨੇਹ", | |
| "ਬੈਸ˜ਵਹ": "ਬੈਸ੍ਨਵਹ" | |
| } | |
| def apply_text_fixes(text): | |
| text = text.replace('ਅਾਂ', 'ਆਂ') | |
| text = text.replace('ਅਾ', 'ਆ') | |
| text = text.replace('ੲੀਂ', 'ਈਂ') | |
| text = text.replace("ˆ", "ਂ") | |
| # halant fix | |
| text = text.replace('ਕਾਨ੍ੁ', 'ਕਾਨੑੁ') | |
| text = text.replace('ਚੀਨ੍ਾਂ', 'ਚੀਨੑਾਂ') | |
| text = text.replace('ਦ੍ਰਿੜ੍ੁ', 'ਦ੍ਰਿੜੑੁ') | |
| text = text.replace('ਕੀਨ੍ੋ', 'ਕੀਨੑੋ') | |
| text = text.replace('ਥੰਮ੍ੁ', 'ਥੰਮੑੁ') | |
| text = text.replace('ਪਾਨ੍ੋ', 'ਪਾਨੑੋ') | |
| # Apply known corrections | |
| for wrong, correct in REPLACEMENTS.items(): | |
| text = text.replace(wrong, correct) | |
| # Collapse multiple whitespace into one space | |
| text = re.sub(r"\s+", " ", text) | |
| # Remove spaces from beginning and end | |
| text = text.strip() | |
| return text | |
| def clean_transcript(text): | |
| # Transcript-only cleanup | |
| text = text.replace(",", "").replace("।", "") | |
| text = text.replace(";", "").replace(".", "") | |
| text = text.replace("Ø", "") | |
| # Shared fixes | |
| text = apply_text_fixes(text) | |
| return text | |
| def clean_gurmukhi(text): | |
| # Do NOT remove punctuation from gurmukhi | |
| text = apply_text_fixes(text) | |
| return text | |
| def process_file(path, clean=False): | |
| try: | |
| with path.open("r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| except Exception as e: | |
| print(f"ERROR: {path.relative_to(BASE_DIR)}") | |
| print(f" {e}") | |
| return 0 | |
| changed = 0 | |
| for section in data.get("sections", []): | |
| section_id = section.get("section_id", "?") | |
| fields = { | |
| "transcript": clean_transcript, | |
| "gurbani_text": clean_gurmukhi, | |
| } | |
| for field, cleaner in fields.items(): | |
| old_text = section.get(field) | |
| if not isinstance(old_text, str): | |
| continue | |
| new_text = cleaner(old_text) | |
| if old_text == new_text: | |
| continue | |
| changed += 1 | |
| print(f"\n{path.relative_to(BASE_DIR)}") | |
| print(f" {section_id}") | |
| print(f" FIELD: {field}") | |
| print(f" OLD: {old_text}") | |
| print(f" NEW: {new_text}") | |
| if clean: | |
| section[field] = new_text | |
| if clean and changed: | |
| with path.open("w", encoding="utf-8") as f: | |
| json.dump(data, f, ensure_ascii=False, indent=2) | |
| return changed | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="Clean transcript fields in all nested JSON files." | |
| ) | |
| mode = parser.add_mutually_exclusive_group(required=True) | |
| mode.add_argument( | |
| "--dry-run", | |
| action="store_true", | |
| help="Show changes without modifying files", | |
| ) | |
| mode.add_argument( | |
| "--clean", | |
| action="store_true", | |
| help="Apply changes and overwrite files", | |
| ) | |
| args = parser.parse_args() | |
| # Recursively find JSON files in all folders below this script | |
| files = sorted(BASE_DIR.rglob("*.json")) | |
| if not files: | |
| print(f"No JSON files found under: {BASE_DIR}") | |
| return | |
| print(f"Base folder: {BASE_DIR}") | |
| print(f"JSON files found: {len(files)}") | |
| total_changed = 0 | |
| files_changed = 0 | |
| for file in files: | |
| count = process_file(file, clean=args.clean) | |
| if count: | |
| files_changed += 1 | |
| total_changed += count | |
| print("\n" + "=" * 60) | |
| if args.dry_run: | |
| print("DRY RUN — no files were modified") | |
| else: | |
| print("CLEAN COMPLETE") | |
| print(f"JSON files checked: {len(files)}") | |
| print(f"Files with changes: {files_changed}") | |
| print(f"Transcripts changed: {total_changed}") | |
| if __name__ == "__main__": | |
| main() | |