Spaces:
Runtime error
Runtime error
File size: 19,564 Bytes
b97b788 | 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | """
correction_engine.py β Pre- and Post-translation correction pipeline
Handles: misheard words, slang, short forms, spoken English,
confused words, and domain-specific corrections.
"""
import json
import os
import re
from typing import Dict, List, Optional, Any
from config import CORRECTIONS_FILE, DATASETS_DIR
class CorrectionEngine:
"""
Two-stage correction pipeline:
1. PRE-TRANSLATION: Fix speech recognition errors before translation
2. POST-TRANSLATION: Fix known translation errors after NLLB output
"""
def __init__(self):
self.pre_corrections: Dict[str, Dict] = {} # lang β {pattern: replacement}
self.post_corrections: Dict[str, Dict] = {} # lang_pair β {pattern: replacement}
self.slang_map: Dict[str, str] = {}
self.short_forms: Dict[str, str] = {}
self.confused_words: Dict[str, str] = {}
self.domain_terms: Dict[str, Dict[str, str]] = {}
self.loaded = False
def load(self):
"""Load all correction datasets."""
os.makedirs(DATASETS_DIR, exist_ok=True)
# Create default corrections file if it doesn't exist
if not os.path.exists(CORRECTIONS_FILE):
self._create_default_corrections()
try:
with open(CORRECTIONS_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
self.slang_map = data.get("slang", {})
self.short_forms = data.get("short_forms", {})
self.confused_words = data.get("confused_words", {})
self.domain_terms = data.get("domain_terms", {})
self.pre_corrections = data.get("pre_corrections", {})
self.post_corrections = data.get("post_corrections", {})
total = (len(self.slang_map) + len(self.short_forms) +
len(self.confused_words) + sum(len(v) for v in self.domain_terms.values()))
print(f"[Correction] Loaded {total} correction entries.")
self.loaded = True
except Exception as e:
print(f"[Correction] Error loading corrections: {e}")
self._create_default_corrections()
self.loaded = True
# Load any additional custom correction files
self._load_custom_corrections()
def pre_translate(self, text: str, source_lang: str = "en") -> Dict[str, Any]:
"""
Apply pre-translation corrections to recognized speech text.
Pipeline:
Raw Whisper text β Slang fix β Short forms β Confused words
β Domain terms β Custom pre-corrections β Cleaned text
Returns:
dict with 'text' (corrected), 'corrections' (list of changes made)
"""
corrections = []
original = text
# 1. Normalize whitespace
text = re.sub(r'\s+', ' ', text).strip()
# 2. Apply slang corrections (case-insensitive)
for slang, replacement in self.slang_map.items():
pattern = re.compile(r'\b' + re.escape(slang) + r'\b', re.IGNORECASE)
if pattern.search(text):
text = pattern.sub(replacement, text)
corrections.append({"type": "slang", "from": slang, "to": replacement})
# 3. Apply short form expansions
for short, full in self.short_forms.items():
pattern = re.compile(r'\b' + re.escape(short) + r'\b', re.IGNORECASE)
if pattern.search(text):
text = pattern.sub(full, text)
corrections.append({"type": "short_form", "from": short, "to": full})
# 4. Fix commonly confused words
for wrong, correct in self.confused_words.items():
pattern = re.compile(r'\b' + re.escape(wrong) + r'\b', re.IGNORECASE)
if pattern.search(text):
text = pattern.sub(correct, text)
corrections.append({"type": "confused", "from": wrong, "to": correct})
# 5. Apply domain-specific corrections
for domain, terms in self.domain_terms.items():
for wrong, correct in terms.items():
pattern = re.compile(r'\b' + re.escape(wrong) + r'\b', re.IGNORECASE)
if pattern.search(text):
text = pattern.sub(correct, text)
corrections.append({
"type": "domain",
"domain": domain,
"from": wrong,
"to": correct
})
# 6. Apply language-specific pre-corrections
lang_corrections = self.pre_corrections.get(source_lang, {})
for wrong, correct in lang_corrections.items():
pattern = re.compile(re.escape(wrong), re.IGNORECASE)
if pattern.search(text):
text = pattern.sub(correct, text)
corrections.append({"type": "pre_lang", "from": wrong, "to": correct})
return {
"text": text,
"original": original,
"corrected": text != original,
"corrections": corrections,
"correction_count": len(corrections)
}
def post_translate(self, text: str, source_lang: str, target_lang: str) -> Dict[str, Any]:
"""
Apply post-translation corrections to NLLB output.
Returns:
dict with 'text' (corrected), 'corrections' (list of changes)
"""
corrections = []
original = text
lang_pair = f"{source_lang}_{target_lang}"
# Apply language-pair-specific post-corrections
pair_corrections = self.post_corrections.get(lang_pair, {})
for wrong, correct in pair_corrections.items():
if wrong in text:
text = text.replace(wrong, correct)
corrections.append({"type": "post_lang", "from": wrong, "to": correct})
# Target-language-specific corrections
target_corrections = self.post_corrections.get(target_lang, {})
for wrong, correct in target_corrections.items():
if wrong in text:
text = text.replace(wrong, correct)
corrections.append({"type": "post_target", "from": wrong, "to": correct})
return {
"text": text,
"original": original,
"corrected": text != original,
"corrections": corrections
}
def add_correction(self, category: str, wrong: str, correct: str,
domain: Optional[str] = None) -> bool:
"""
Add a new correction entry and persist to file.
Args:
category: 'slang', 'short_forms', 'confused_words', 'domain_terms'
wrong: The incorrect/misheard form
correct: The correct replacement
domain: Domain name (only for 'domain_terms' category)
"""
try:
if category == "slang":
self.slang_map[wrong] = correct
elif category == "short_forms":
self.short_forms[wrong] = correct
elif category == "confused_words":
self.confused_words[wrong] = correct
elif category == "domain_terms" and domain:
if domain not in self.domain_terms:
self.domain_terms[domain] = {}
self.domain_terms[domain][wrong] = correct
else:
return False
self._save_corrections()
return True
except Exception as e:
print(f"[Correction] Error adding correction: {e}")
return False
def get_stats(self) -> Dict[str, int]:
"""Return statistics about loaded corrections."""
return {
"slang_entries": len(self.slang_map),
"short_form_entries": len(self.short_forms),
"confused_word_entries": len(self.confused_words),
"domain_entries": sum(len(v) for v in self.domain_terms.values()),
"pre_correction_langs": len(self.pre_corrections),
"post_correction_pairs": len(self.post_corrections),
"total": (len(self.slang_map) + len(self.short_forms) +
len(self.confused_words) +
sum(len(v) for v in self.domain_terms.values()))
}
def _load_custom_corrections(self):
"""Load additional correction files from datasets/ folder."""
custom_dir = os.path.join(DATASETS_DIR, "custom_corrections")
if not os.path.exists(custom_dir):
os.makedirs(custom_dir, exist_ok=True)
return
for filename in os.listdir(custom_dir):
if filename.endswith(".json"):
try:
filepath = os.path.join(custom_dir, filename)
with open(filepath, "r", encoding="utf-8") as f:
data = json.load(f)
# Merge custom corrections
if "slang" in data:
self.slang_map.update(data["slang"])
if "short_forms" in data:
self.short_forms.update(data["short_forms"])
if "confused_words" in data:
self.confused_words.update(data["confused_words"])
if "domain_terms" in data:
for domain, terms in data["domain_terms"].items():
if domain not in self.domain_terms:
self.domain_terms[domain] = {}
self.domain_terms[domain].update(terms)
print(f"[Correction] Loaded custom: {filename}")
except Exception as e:
print(f"[Correction] Error loading {filename}: {e}")
def _save_corrections(self):
"""Persist corrections to disk."""
data = {
"slang": self.slang_map,
"short_forms": self.short_forms,
"confused_words": self.confused_words,
"domain_terms": self.domain_terms,
"pre_corrections": self.pre_corrections,
"post_corrections": self.post_corrections,
}
os.makedirs(os.path.dirname(CORRECTIONS_FILE), exist_ok=True)
with open(CORRECTIONS_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
def _create_default_corrections(self):
"""Create the default corrections dataset."""
data = {
"slang": {
"gonna": "going to",
"wanna": "want to",
"gotta": "got to",
"lemme": "let me",
"gimme": "give me",
"kinda": "kind of",
"sorta": "sort of",
"dunno": "don't know",
"ain't": "is not",
"y'all": "you all",
"bro": "brother",
"sis": "sister",
"dude": "friend",
"nah": "no",
"yep": "yes",
"yup": "yes",
"nope": "no",
"ok": "okay",
"coz": "because",
"cuz": "because",
"cos": "because",
"sup": "what is up",
"wassup": "what is up",
"whatcha": "what are you",
"imma": "I am going to",
"fam": "family",
"lit": "exciting",
"lowkey": "slightly",
"highkey": "very much",
"salty": "upset",
"shook": "shocked",
"vibe": "feeling",
"flex": "show off",
"ghost": "disappear",
"slay": "do excellently",
"bet": "agreed",
"cap": "lie",
"no cap": "no lie",
"fire": "excellent",
"sus": "suspicious",
"bruh": "brother",
"hella": "very",
"bougie": "fancy",
"clap back": "respond sharply",
"shade": "disrespect",
"tea": "gossip",
"mood": "relatable",
"stan": "admire greatly",
"simp": "overly devoted person",
"yeet": "throw",
"oof": "expressing discomfort",
"periodt": "period",
"deadass": "seriously",
"idk": "I don't know",
"tbh": "to be honest",
"imo": "in my opinion",
"smh": "shaking my head",
"lol": "laughing out loud",
"omg": "oh my god",
"brb": "be right back",
"btw": "by the way",
"fyi": "for your information",
"asap": "as soon as possible"
},
"short_forms": {
"govt": "government",
"dept": "department",
"mgr": "manager",
"prof": "professor",
"doc": "doctor",
"info": "information",
"tech": "technology",
"app": "application",
"bio": "biography",
"exam": "examination",
"lab": "laboratory",
"math": "mathematics",
"stats": "statistics",
"ref": "reference",
"temp": "temperature",
"approx": "approximately",
"etc": "etcetera",
"vs": "versus",
"min": "minute",
"max": "maximum",
"avg": "average",
"qty": "quantity",
"yr": "year",
"mo": "month",
"wk": "week",
"hr": "hour",
"sec": "second",
"Mon": "Monday",
"Tue": "Tuesday",
"Wed": "Wednesday",
"Thu": "Thursday",
"Fri": "Friday",
"Sat": "Saturday",
"Sun": "Sunday",
"Jan": "January",
"Feb": "February",
"Mar": "March",
"Apr": "April",
"Jun": "June",
"Jul": "July",
"Aug": "August",
"Sep": "September",
"Oct": "October",
"Nov": "November",
"Dec": "December"
},
"confused_words": {
"their there": "there",
"your you're": "you are",
"its it's": "it is",
"affect effect": "effect",
"then than": "than",
"loose lose": "lose",
"weather whether": "whether",
"to too": "too",
"accept except": "except",
"advise advice": "advice",
"principal principle": "principle",
"stationary stationery": "stationery",
"complement compliment": "compliment",
"desert dessert": "dessert",
"alot": "a lot",
"definately": "definitely",
"seperate": "separate",
"occured": "occurred",
"recieve": "receive",
"untill": "until",
"writting": "writing",
"calender": "calendar",
"goverment": "government",
"enviroment": "environment",
"tommorrow": "tomorrow",
"accomodate": "accommodate",
"concious": "conscious",
"necesary": "necessary",
"occassion": "occasion",
"priviledge": "privilege",
"pronounciation": "pronunciation",
"recomend": "recommend",
"refered": "referred",
"succesful": "successful",
"wierd": "weird"
},
"domain_terms": {
"medical": {
"BP": "blood pressure",
"HR": "heart rate",
"temp": "temperature",
"RX": "prescription",
"OPD": "outpatient department",
"ICU": "intensive care unit",
"ER": "emergency room",
"MRI": "magnetic resonance imaging",
"CT scan": "computed tomography scan",
"ECG": "electrocardiogram"
},
"education": {
"GPA": "grade point average",
"CGPA": "cumulative grade point average",
"HOD": "head of department",
"PhD": "Doctor of Philosophy",
"MSc": "Master of Science",
"BSc": "Bachelor of Science",
"sem": "semester",
"assign": "assignment",
"ppt": "presentation"
},
"technology": {
"API": "application programming interface",
"UI": "user interface",
"UX": "user experience",
"DB": "database",
"OS": "operating system",
"CPU": "central processing unit",
"GPU": "graphics processing unit",
"RAM": "random access memory",
"SSD": "solid state drive",
"URL": "uniform resource locator"
},
"business": {
"CEO": "Chief Executive Officer",
"CFO": "Chief Financial Officer",
"CTO": "Chief Technology Officer",
"HR": "human resources",
"KPI": "key performance indicator",
"ROI": "return on investment",
"B2B": "business to business",
"B2C": "business to consumer",
"ETA": "estimated time of arrival",
"RSVP": "please respond"
}
},
"pre_corrections": {
"en": {
"hii": "hi",
"hiii": "hi",
"hellooo": "hello",
"byeee": "bye",
"yesss": "yes",
"nooo": "no",
"pleaseee": "please",
"thanksss": "thanks",
"sorryyy": "sorry",
"okayyyy": "okay",
"wowww": "wow",
"ummm": "",
"uhhh": "",
"ahhh": "",
"hmmmm": "",
"errr": "",
"like like": "like",
"you know you know": "you know",
"I mean I mean": "I mean"
}
},
"post_corrections": {}
}
os.makedirs(os.path.dirname(CORRECTIONS_FILE), exist_ok=True)
with open(CORRECTIONS_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
# Load into memory
self.slang_map = data["slang"]
self.short_forms = data["short_forms"]
self.confused_words = data["confused_words"]
self.domain_terms = data["domain_terms"]
self.pre_corrections = data["pre_corrections"]
self.post_corrections = data["post_corrections"]
print(f"[Correction] Created default corrections dataset.")
|