File size: 32,663 Bytes
9906dbd a31933e 9906dbd a31933e 9906dbd a31933e 9906dbd a31933e 9906dbd a31933e 9906dbd a31933e 9906dbd a31933e 9906dbd a31933e 9906dbd | 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 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 | """
Beam search and greedy decoders for Singlish β Sinhala transliteration.
"""
import math
import re
import torch
import pickle
import logging
from typing import List, Tuple, Dict, Optional, Set
from transformers import AutoTokenizer, AutoModelForMaskedLM
from core.constants import (
DEFAULT_MODEL_NAME, DEFAULT_DICTIONARY_PATH,
DEFAULT_BEAM_WIDTH, MAX_CANDIDATES, MIN_ENGLISH_LEN,
PUNCT_PATTERN,
)
from core.mappings import COMMON_WORDS, CONTEXT_WORDS_STANDALONE
from core.english import ENGLISH_VOCAB
from core.scorer import CandidateScorer, ScoredCandidate, WordDiagnostic
from core.dictionary import DictionaryAdapter
logger = logging.getLogger(__name__)
# Sinhala Unicode block: U+0D80 β U+0DFF
_SINHALA_RE = re.compile(r"[\u0D80-\u0DFF]")
def _is_sinhala(text: str) -> bool:
"""Return True if the text already contains Sinhala script characters."""
return bool(_SINHALA_RE.search(text))
class BeamSearchDecoder:
"""
Contextual beam-search decoder for Singlish β Sinhala transliteration.
For each word position the decoder:
1. Generates candidates (dictionary + rule engine)
2. Scores them with XLM-R MLM in sentence context
3. Combines MLM score with fidelity & rank via CandidateScorer
4. Prunes to the top-k (beam width) hypotheses
"""
def __init__(
self,
model_name: str = DEFAULT_MODEL_NAME,
dictionary_path: str = DEFAULT_DICTIONARY_PATH,
device: Optional[str] = None,
):
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
logger.info("Loading tokenizer & model: %s", model_name)
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = AutoModelForMaskedLM.from_pretrained(model_name)
self.model.to(self.device)
self.model.eval()
logger.info("Loading dictionary: %s", dictionary_path)
with open(dictionary_path, "rb") as f:
d_data = pickle.load(f)
self.adapter = DictionaryAdapter(d_data)
self.scorer = CandidateScorer()
# ββ Normalization βββββββββββββββββββββββββββββββββββββββββββββ
@staticmethod
def _softmax_normalize(raw_scores: List[float]) -> List[float]:
"""
Normalize raw log-probability scores to [0, 1] via softmax.
Unlike min-max (which maps bestβ1.0, worstβ0.0 regardless of
the actual difference), softmax preserves the model's relative
confidence. When all candidates have similar log-probs the
output values cluster together; when the model is very
confident they spread apart.
The raw scores are already log-probs (negative), so we use
them directly as logits for softmax.
"""
if not raw_scores:
return []
if len(raw_scores) == 1:
return [1.0]
# Subtract max for numerical stability (standard log-sum-exp trick)
max_s = max(raw_scores)
exps = [math.exp(s - max_s) for s in raw_scores]
total = sum(exps)
return [e / total for e in exps]
# ββ MLM batch scoring ββββββββββββββββββββββββββββββββββββββββββββ
def _batch_mlm_score(
self,
left_contexts: List[str],
right_contexts: List[str],
candidates: List[str],
) -> List[float]:
"""
Score each candidate using masked LM log-probability with proper
multi-mask scoring for multi-subword candidates.
Instead of placing a single <mask> and summing subword log-probs
at that one position, this method creates one <mask> per subword
token and scores each subword at its own position:
score = (1/N) * Ξ£_i log P(t_i | mask_position_i, context)
"""
if not candidates:
return []
mask = self.tokenizer.mask_token
mask_token_id = self.tokenizer.mask_token_id
# Pre-tokenize every candidate to determine subword count
cand_token_ids: List[List[int]] = []
for c in candidates:
ids = self.tokenizer.encode(c, add_special_tokens=False)
cand_token_ids.append(ids if ids else [self.tokenizer.unk_token_id])
# Build context strings with the correct number of <mask> tokens
batch_texts: List[str] = []
for i in range(len(candidates)):
n_masks = len(cand_token_ids[i])
mask_str = " ".join([mask] * n_masks)
parts = [p for p in [left_contexts[i], mask_str, right_contexts[i]] if p]
batch_texts.append(" ".join(parts))
inputs = self.tokenizer(
batch_texts,
return_tensors="pt",
padding=True,
truncation=True,
).to(self.device)
with torch.no_grad():
logits = self.model(**inputs).logits
scores: List[float] = []
for i, target_ids in enumerate(cand_token_ids):
token_ids = inputs.input_ids[i]
mask_positions = (token_ids == mask_token_id).nonzero(as_tuple=True)[0]
if mask_positions.numel() == 0 or not target_ids:
scores.append(-100.0)
continue
# Score each subword at its corresponding mask position
n = min(len(target_ids), mask_positions.numel())
total = 0.0
for j in range(n):
pos = mask_positions[j].item()
log_probs = torch.log_softmax(logits[i, pos, :], dim=0)
total += log_probs[target_ids[j]].item()
scores.append(total / n)
return scores
# ββ Main decode entry-point ββββββββββββββββββββββββββββββββββββββ
def decode(
self,
sentence: str,
beam_width: int = DEFAULT_BEAM_WIDTH,
mode: str = "greedy",
) -> Tuple[str, List[str]]:
"""
Transliterate a full Singlish sentence into Sinhala script.
Args:
mode: "greedy" (accurate, uses dynamic context) or
"beam" (uses fixed rule-based context)
Returns:
result β the best transliteration string
trace_logs β per-step markdown logs for the debug UI
"""
if mode == "greedy":
result, trace_logs, _ = self.greedy_decode_with_diagnostics(sentence)
else:
result, trace_logs, _ = self.decode_with_diagnostics(
sentence=sentence,
beam_width=beam_width,
)
return result, trace_logs
# ββ Greedy decode (dynamic context β more accurate) ββββββββββββββ
def greedy_decode_with_diagnostics(
self,
sentence: str,
) -> Tuple[str, List[str], List[WordDiagnostic]]:
"""
Greedy word-by-word decode using actual selected outputs as
left context for subsequent MLM scoring.
More accurate than beam search with fixed context because XLM-R
sees the real transliteration built so far, not rule-based guesses.
"""
words = sentence.split()
if not words:
return "", [], []
# ββ Phase 1: candidate generation (same as beam) βββββββββββββ
word_infos: List[dict] = []
for raw in words:
match = PUNCT_PATTERN.match(raw)
prefix, core, suffix = match.groups() if match else ("", raw, "")
if not core:
word_infos.append({
"candidates": [raw],
"rule_output": raw,
"english_flags": [False],
"dict_flags": [False],
"prefix": prefix,
"suffix": suffix,
"sinhala_passthrough": False,
})
continue
# Already-Sinhala text: pass through unchanged
if _is_sinhala(core):
word_infos.append({
"candidates": [raw],
"rule_output": raw,
"english_flags": [False],
"dict_flags": [False],
"prefix": prefix,
"suffix": suffix,
"sinhala_passthrough": True,
})
continue
rule_output = self.adapter.get_rule_output(core)
cands = self.adapter.get_candidates(core, rule_output)
dict_entries: Set[str] = set()
if core in self.adapter.dictionary:
dict_entries.update(self.adapter.dictionary[core])
elif core.lower() in self.adapter.dictionary:
dict_entries.update(self.adapter.dictionary[core.lower()])
if rule_output and rule_output not in cands:
cands.append(rule_output)
if not cands:
cands = [rule_output]
english_flags = [c.lower() in ENGLISH_VOCAB for c in cands]
dict_flags = [c in dict_entries for c in cands]
full_cands = [prefix + c + suffix for c in cands]
word_infos.append({
"candidates": full_cands[:MAX_CANDIDATES],
"rule_output": prefix + rule_output + suffix,
"core_rule_output": rule_output,
"n_dict_entries": len(dict_entries),
"dict_entries": dict_entries,
"english_flags": english_flags[:MAX_CANDIDATES],
"dict_flags": dict_flags[:MAX_CANDIDATES],
"prefix": prefix,
"suffix": suffix,
"sinhala_passthrough": False,
})
# Build right-side stable context (rule outputs for future words)
stable_right: List[str] = []
for info in word_infos:
eng_cands = [
c for c, e in zip(info["candidates"], info["english_flags"]) if e
]
stable_right.append(
eng_cands[0] if eng_cands else info["rule_output"]
)
# ββ Phase 2: greedy word-by-word with dynamic left context βββ
selected_words: List[str] = []
trace_logs: List[str] = []
diagnostics: List[WordDiagnostic] = []
for t, info in enumerate(word_infos):
candidates = info["candidates"]
eng_flags = info["english_flags"]
d_flags = info.get("dict_flags", [False] * len(candidates))
rule_out = info["rule_output"]
prefix = info.get("prefix", "")
suffix = info.get("suffix", "")
total_cands = len(candidates)
# ββ Sinhala passthrough ββββββββββββββββββββββββββββββββββββ
if info.get("sinhala_passthrough"):
selected_words.append(words[t])
trace_logs.append(
f"**Step {t + 1}: `{words[t]}`** β "
f"`{words[t]}` (Sinhala passthrough)\n"
)
diagnostics.append(WordDiagnostic(
step_index=t,
input_word=words[t],
rule_output=rule_out,
selected_candidate=words[t],
beam_score=0.0,
candidate_breakdown=[],
))
continue
# ββ Common-word shortcut βββββββββββββββββββββββββββββββββ
core_lower = words[t].lower().strip()
if core_lower in COMMON_WORDS:
override = prefix + COMMON_WORDS[core_lower] + suffix
selected_words.append(override)
trace_logs.append(
f"**Step {t + 1}: `{words[t]}`** β "
f"`{override}` (common-word override)\n"
)
diagnostics.append(WordDiagnostic(
step_index=t,
input_word=words[t],
rule_output=rule_out,
selected_candidate=override,
beam_score=0.0,
candidate_breakdown=[],
))
continue
# ββ Context-dependent standalone overrides ββββββββββββββββ
if core_lower in CONTEXT_WORDS_STANDALONE:
prev_word_lower = words[t - 1].lower() if t > 0 else ""
prev_common_val = COMMON_WORDS.get(prev_word_lower, "")
prev_is_english = (
t > 0
and (
prev_word_lower in ENGLISH_VOCAB
or prev_common_val.isascii() and prev_common_val != ""
)
)
if not prev_is_english:
override = prefix + CONTEXT_WORDS_STANDALONE[core_lower] + suffix
selected_words.append(override)
trace_logs.append(
f"**Step {t + 1}: `{words[t]}`** β "
f"`{override}` (standalone override)\n"
)
diagnostics.append(WordDiagnostic(
step_index=t,
input_word=words[t],
rule_output=rule_out,
selected_candidate=override,
beam_score=0.0,
candidate_breakdown=[],
))
continue
# ββ English-word shortcut ββββββββββββββββββββββββββββββββ
# Preserve English immediately UNLESS the romanisation maps
# to a genuine Sinhala word (rule output found in the
# dictionary with 3+ entries β multiple meanings).
# e.g. "game" ruleβΰΆΰΆΈΰ· exists in dict with 7 entries β ambiguous.
# e.g. "meeting" ruleβΰΆΈΰ·ΰΆ§ΰ·ΰΆ±ΰ·ΰΆΰ· is in dict but only 1 entry β
# loanword transliteration, keep English.
core_rule = info.get("core_rule_output", "")
core_dict = info.get("dict_entries", set())
is_semantically_ambiguous = (
core_rule in core_dict and len(core_dict) >= 3
)
if (
len(core_lower) >= MIN_ENGLISH_LEN
and core_lower in ENGLISH_VOCAB
and not is_semantically_ambiguous
):
selected_words.append(words[t])
trace_logs.append(
f"**Step {t + 1}: `{words[t]}`** β "
f"`{words[t]}` (English preserved)\n"
)
diagnostics.append(WordDiagnostic(
step_index=t,
input_word=words[t],
rule_output=rule_out,
selected_candidate=words[t],
beam_score=0.0,
candidate_breakdown=[],
))
continue
# Dynamic left context = actual selected outputs so far
left_ctx = " ".join(selected_words) if selected_words else ""
# Right context = rule-based stable context for future words
right_ctx = " ".join(stable_right[t + 1:]) if t + 1 < len(words) else ""
# Score all candidates for this position in one batch
batch_left = [left_ctx] * total_cands
batch_right = [right_ctx] * total_cands
mlm_scores = self._batch_mlm_score(batch_left, batch_right, candidates)
# ββ Softmax normalise MLM scores βββββββββββββββββββββββββ
# Preserves the model's relative confidence β close raw
# log-probs yield close normalised values, unlike min-max
# which always maps bestβ1.0 / worstβ0.0.
mlm_scores = self._softmax_normalize(mlm_scores)
# MLM floor for English code-switching
# Skip floor for semantically ambiguous words (rule output
# found in dict with 3+ entries) so raw MLM context signal
# can distinguish e.g. "game" (English) vs ΰΆΰΆΈΰ· (village).
best_nonenglish_mlm = -1e9
if not is_semantically_ambiguous:
for i, mlm in enumerate(mlm_scores):
is_eng = eng_flags[i] if i < len(eng_flags) else False
if not is_eng and mlm > best_nonenglish_mlm:
best_nonenglish_mlm = mlm
# Score & select best candidate
step_log = f"**Step {t + 1}: `{words[t]}`** (rule β `{rule_out}`)\n\n"
best_scored: Optional[ScoredCandidate] = None
candidate_breakdown: List[ScoredCandidate] = []
for i, mlm in enumerate(mlm_scores):
cand = candidates[i]
is_eng = eng_flags[i] if i < len(eng_flags) else False
is_dict = d_flags[i] if i < len(d_flags) else False
effective_mlm = mlm
if is_eng and cand.lower() == words[t].lower() and not is_semantically_ambiguous:
effective_mlm = max(mlm, best_nonenglish_mlm)
scored = self.scorer.score(
mlm_score=effective_mlm,
candidate=cand,
rule_output=rule_out,
rank=i,
total_candidates=total_cands,
is_english=is_eng,
original_input=words[t],
is_from_dict=is_dict,
is_ambiguous=is_semantically_ambiguous,
)
candidate_breakdown.append(scored)
if best_scored is None or scored.combined_score > best_scored.combined_score:
best_scored = scored
if mlm > -25.0:
eng_tag = " π€" if is_eng else ""
step_log += (
f"- `{cand}`{eng_tag} "
f"MLM={scored.mlm_score:.2f} "
f"Fid={scored.fidelity_score:.2f} "
f"Rank={scored.rank_score:.2f} β "
f"**{scored.combined_score:.2f}**\n"
)
trace_logs.append(step_log)
selected = best_scored.text if best_scored else rule_out
selected_words.append(selected)
candidate_breakdown.sort(key=lambda s: s.combined_score, reverse=True)
diagnostics.append(WordDiagnostic(
step_index=t,
input_word=words[t],
rule_output=rule_out,
selected_candidate=selected,
beam_score=best_scored.combined_score if best_scored else 0.0,
candidate_breakdown=candidate_breakdown,
))
result = " ".join(selected_words)
return result, trace_logs, diagnostics
# ββ Beam decode (fixed context β legacy comparison) ββββββββββββββ
def decode_with_diagnostics(
self,
sentence: str,
beam_width: int = DEFAULT_BEAM_WIDTH,
) -> Tuple[str, List[str], List[WordDiagnostic]]:
"""
Decode sentence using beam search and return detailed diagnostics.
Uses fixed rule-based context for all beam paths. Kept for
comparison with greedy decode in evaluation.
"""
words = sentence.split()
if not words:
return "", [], []
# ββ Phase 1: candidate generation ββββββββββββββββββββββββββββ
word_infos: List[dict] = []
for raw in words:
match = PUNCT_PATTERN.match(raw)
prefix, core, suffix = match.groups() if match else ("", raw, "")
if not core:
word_infos.append({
"candidates": [raw],
"rule_output": raw,
"english_flags": [False],
"prefix": prefix,
"suffix": suffix,
"sinhala_passthrough": False,
})
continue
# Already-Sinhala text: pass through unchanged
if _is_sinhala(core):
word_infos.append({
"candidates": [raw],
"rule_output": raw,
"english_flags": [False],
"prefix": prefix,
"suffix": suffix,
"sinhala_passthrough": True,
})
continue
rule_output = self.adapter.get_rule_output(core)
cands = self.adapter.get_candidates(core, rule_output)
dict_entries: Set[str] = set()
if core in self.adapter.dictionary:
dict_entries.update(self.adapter.dictionary[core])
elif core.lower() in self.adapter.dictionary:
dict_entries.update(self.adapter.dictionary[core.lower()])
if rule_output and rule_output not in cands:
cands.append(rule_output)
if not cands:
cands = [rule_output]
english_flags = [c.lower() in ENGLISH_VOCAB for c in cands]
dict_flags = [c in dict_entries for c in cands]
full_cands = [prefix + c + suffix for c in cands]
word_infos.append({
"candidates": full_cands[:MAX_CANDIDATES],
"rule_output": prefix + rule_output + suffix,
"core_rule_output": rule_output,
"n_dict_entries": len(dict_entries),
"dict_entries": dict_entries,
"english_flags": english_flags[:MAX_CANDIDATES],
"dict_flags": dict_flags[:MAX_CANDIDATES],
"prefix": prefix,
"suffix": suffix,
"sinhala_passthrough": False,
})
# Build stable context (fixed for all beam paths)
stable_context: List[str] = []
for info in word_infos:
eng_cands = [
c for c, e in zip(info["candidates"], info["english_flags"]) if e
]
stable_context.append(
eng_cands[0] if eng_cands else info["rule_output"]
)
# ββ Phase 2: beam search with data-driven scoring ββββββββββββ
beam: List[Tuple[List[str], float]] = [([], 0.0)]
trace_logs: List[str] = []
diagnostics: List[WordDiagnostic] = []
for t, info in enumerate(word_infos):
candidates = info["candidates"]
eng_flags = info["english_flags"]
d_flags = info.get("dict_flags", [False] * len(candidates))
rule_out = info["rule_output"]
prefix = info.get("prefix", "")
suffix = info.get("suffix", "")
total_cands = len(candidates)
# ββ Sinhala passthrough ββββββββββββββββββββββββββββββββββββ
if info.get("sinhala_passthrough"):
next_beam_si = [(path + [words[t]], sc) for path, sc in beam]
beam = next_beam_si[:beam_width]
trace_logs.append(
f"**Step {t + 1}: `{words[t]}`** β "
f"`{words[t]}` (Sinhala passthrough)\n"
)
diagnostics.append(WordDiagnostic(
step_index=t,
input_word=words[t],
rule_output=rule_out,
selected_candidate=words[t],
beam_score=beam[0][1] if beam else 0.0,
candidate_breakdown=[],
))
continue
# ββ Common-word shortcut βββββββββββββββββββββββββββββββββ
core_lower = words[t].lower().strip()
if core_lower in COMMON_WORDS:
override = prefix + COMMON_WORDS[core_lower] + suffix
next_beam_cw = [(path + [override], sc) for path, sc in beam]
beam = next_beam_cw[:beam_width]
trace_logs.append(
f"**Step {t + 1}: `{words[t]}`** β "
f"`{override}` (common-word override)\n"
)
diagnostics.append(WordDiagnostic(
step_index=t,
input_word=words[t],
rule_output=rule_out,
selected_candidate=override,
beam_score=beam[0][1] if beam else 0.0,
candidate_breakdown=[],
))
continue
# ββ Context-dependent standalone overrides ββββββββββββββββ
if core_lower in CONTEXT_WORDS_STANDALONE:
prev_word_lower = words[t - 1].lower() if t > 0 else ""
prev_common_val = COMMON_WORDS.get(prev_word_lower, "")
prev_is_english = (
t > 0
and (
prev_word_lower in ENGLISH_VOCAB
or prev_common_val.isascii() and prev_common_val != ""
)
)
if not prev_is_english:
override = prefix + CONTEXT_WORDS_STANDALONE[core_lower] + suffix
next_beam_ctx = [(path + [override], sc) for path, sc in beam]
beam = next_beam_ctx[:beam_width]
trace_logs.append(
f"**Step {t + 1}: `{words[t]}`** β "
f"`{override}` (standalone override)\n"
)
diagnostics.append(WordDiagnostic(
step_index=t,
input_word=words[t],
rule_output=rule_out,
selected_candidate=override,
beam_score=beam[0][1] if beam else 0.0,
candidate_breakdown=[],
))
continue
# ββ English-word shortcut ββββββββββββββββββββββββββββββββ
# See greedy decode for detailed comment on criterion.
core_rule = info.get("core_rule_output", "")
core_dict = info.get("dict_entries", set())
is_semantically_ambiguous = (
core_rule in core_dict and len(core_dict) >= 3
)
if (
len(core_lower) >= MIN_ENGLISH_LEN
and core_lower in ENGLISH_VOCAB
and not is_semantically_ambiguous
):
eng_word = words[t]
next_beam_eng = [(path + [eng_word], sc) for path, sc in beam]
beam = next_beam_eng[:beam_width]
trace_logs.append(
f"**Step {t + 1}: `{words[t]}`** β "
f"`{eng_word}` (English preserved)\n"
)
diagnostics.append(WordDiagnostic(
step_index=t,
input_word=words[t],
rule_output=rule_out,
selected_candidate=eng_word,
beam_score=beam[0][1] if beam else 0.0,
candidate_breakdown=[],
))
continue
# Build left/right context pairs for multi-mask MLM scoring
batch_left: List[str] = []
batch_right: List[str] = []
batch_tgt: List[str] = []
batch_meta: List[Tuple[int, int]] = [] # (beam_idx, cand_idx)
for p_idx, (path, _) in enumerate(beam):
for c_idx, cand in enumerate(candidates):
future = stable_context[t + 1:] if t + 1 < len(words) else []
batch_left.append(" ".join(stable_context[:t]))
batch_right.append(" ".join(future))
batch_tgt.append(cand)
batch_meta.append((p_idx, c_idx))
if not batch_tgt:
continue
mlm_scores = self._batch_mlm_score(batch_left, batch_right, batch_tgt)
# ββ Softmax normalise MLM scores βββββββββββββββββββββββββ
mlm_scores = self._softmax_normalize(mlm_scores)
# ββ MLM floor for English code-switching βββββββββββββββββ
# See greedy decode for detailed comment on criterion.
best_nonenglish_mlm: Dict[int, float] = {}
if not is_semantically_ambiguous:
for i, mlm in enumerate(mlm_scores):
p_idx, c_idx = batch_meta[i]
is_eng = eng_flags[c_idx] if c_idx < len(eng_flags) else False
if not is_eng:
prev = best_nonenglish_mlm.get(p_idx, -1e9)
if mlm > prev:
best_nonenglish_mlm[p_idx] = mlm
# ββ Score & trace ββββββββββββββββββββββββββββββββββββββββ
next_beam: List[Tuple[List[str], float]] = []
all_step_scores: List[Tuple[int, ScoredCandidate, float]] = []
step_log = f"**Step {t + 1}: `{words[t]}`** (rule β `{rule_out}`)\n\n"
for i, mlm in enumerate(mlm_scores):
p_idx, c_idx = batch_meta[i]
orig_path, orig_score = beam[p_idx]
cand = batch_tgt[i]
is_eng = eng_flags[c_idx] if c_idx < len(eng_flags) else False
is_dict = d_flags[c_idx] if c_idx < len(d_flags) else False
effective_mlm = mlm
if is_eng and cand.lower() == words[t].lower() and not is_semantically_ambiguous:
floor = best_nonenglish_mlm.get(p_idx, mlm)
effective_mlm = max(mlm, floor)
scored = self.scorer.score(
mlm_score=effective_mlm,
candidate=cand,
rule_output=rule_out,
rank=c_idx,
total_candidates=total_cands,
is_english=is_eng,
original_input=words[t],
is_from_dict=is_dict,
is_ambiguous=is_semantically_ambiguous,
)
new_total = orig_score + scored.combined_score
next_beam.append((orig_path + [cand], new_total))
all_step_scores.append((p_idx, scored, new_total))
if mlm > -25.0:
eng_tag = " π€" if is_eng else ""
step_log += (
f"- `{cand}`{eng_tag} "
f"MLM={scored.mlm_score:.2f} "
f"Fid={scored.fidelity_score:.2f} "
f"Rank={scored.rank_score:.2f} β "
f"**{scored.combined_score:.2f}**\n"
)
trace_logs.append(step_log)
beam = sorted(next_beam, key=lambda x: x[1], reverse=True)[:beam_width]
root_scores = [item for item in all_step_scores if item[0] == 0]
root_scores_sorted = sorted(root_scores, key=lambda x: x[2], reverse=True)
selected = beam[0][0][t] if beam and beam[0][0] else ""
selected_total = beam[0][1] if beam else float("-inf")
candidate_breakdown = [item[1] for item in root_scores_sorted]
diagnostics.append(WordDiagnostic(
step_index=t,
input_word=words[t],
rule_output=rule_out,
selected_candidate=selected,
beam_score=selected_total,
candidate_breakdown=candidate_breakdown,
))
result = " ".join(beam[0][0]) if beam else ""
return result, trace_logs, diagnostics
|