File size: 5,344 Bytes
911e360 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 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()
|