feat: IPA-Lautschrift in Cell-Texte einfuegen (fuer Overlay-Modus)
Some checks failed
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Successful in 34s
CI / test-go-edu-search (push) Successful in 31s
CI / test-python-klausur (push) Failing after 2m5s
CI / test-python-agent-core (push) Successful in 23s
CI / test-nodejs-website (push) Successful in 22s
Some checks failed
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Successful in 34s
CI / test-go-edu-search (push) Successful in 31s
CI / test-python-klausur (push) Failing after 2m5s
CI / test-python-agent-core (push) Successful in 23s
CI / test-nodejs-website (push) Successful in 22s
fix_cell_phonetics() ersetzt fehlerhafte IPA-Klammern UND fuegt fehlende Lautschrift fuer englische Woerter ein (z.B. badge, film, challenge, profit). Wird auf alle Zellen mit col_type column_en/column_text angewandt. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -885,6 +885,105 @@ def _replace_phonetics_in_text(text: str, pronunciation: str = 'british') -> str
|
||||
return text
|
||||
|
||||
|
||||
def _insert_missing_ipa(text: str, pronunciation: str = 'british') -> str:
|
||||
"""Insert IPA pronunciation for English words that have no brackets at all.
|
||||
|
||||
OCR sometimes drops the phonetic transcription entirely (e.g. "challenge"
|
||||
instead of "challenge [ˈtʃælɪndʒ]"). This scans the text for lone English
|
||||
words that have a dictionary IPA entry and appends [ipa] after them.
|
||||
|
||||
Only inserts for words that:
|
||||
- are standalone (not already followed by a bracket)
|
||||
- have an IPA entry in the dictionary
|
||||
- appear to be English headwords (at the start of text or after common
|
||||
separators like ",", ";", "•")
|
||||
|
||||
This is intentionally conservative: it only inserts at the END of each
|
||||
whitespace-separated token group to avoid breaking phrases.
|
||||
"""
|
||||
if not IPA_AVAILABLE:
|
||||
return text
|
||||
if not text or not text.strip():
|
||||
return text
|
||||
|
||||
# Skip if already has brackets (IPA replacement handles those)
|
||||
if any(ch in text for ch in '[{('):
|
||||
return text
|
||||
|
||||
# Only process short text fragments (typical vocab cells).
|
||||
# Long sentences / paragraphs should not get IPA insertions.
|
||||
words = text.strip().split()
|
||||
if len(words) > 6:
|
||||
return text
|
||||
|
||||
# Try to insert IPA for the first alphanumeric word
|
||||
# Typical patterns: "challenge", "profit", "film", "badge"
|
||||
for i, w in enumerate(words):
|
||||
# Clean punctuation for lookup
|
||||
clean = re.sub(r'[^a-zA-ZäöüÄÖÜß\'-]', '', w)
|
||||
if not clean or len(clean) < 2:
|
||||
continue
|
||||
# Skip German/grammar words
|
||||
if clean.lower() in _GRAMMAR_BRACKET_WORDS:
|
||||
continue
|
||||
ipa = _lookup_ipa(clean, pronunciation)
|
||||
if ipa:
|
||||
words[i] = f"{w} [{ipa}]"
|
||||
# Only insert for the FIRST word that has IPA
|
||||
# (headword in English column)
|
||||
break
|
||||
|
||||
return ' '.join(words)
|
||||
|
||||
|
||||
def fix_cell_phonetics(
|
||||
cells: List[Dict[str, Any]],
|
||||
pronunciation: str = 'british',
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Apply IPA phonetic fixes to cell texts for overlay mode.
|
||||
|
||||
In the normal pipeline, _fix_phonetic_brackets operates on vocab entries
|
||||
(entry['english']). But the overlay reads cell['text'] directly, so
|
||||
phonetic fixes must be applied to cells too.
|
||||
|
||||
This function:
|
||||
1. Replaces garbled IPA brackets with correct dictionary IPA
|
||||
2. Inserts missing IPA for English headwords that have no brackets
|
||||
|
||||
Only processes cells in English-like columns (column_en, column_text).
|
||||
German columns are never processed (they contain meaningful parentheses).
|
||||
"""
|
||||
if not IPA_AVAILABLE:
|
||||
return cells
|
||||
|
||||
# Column types where IPA processing makes sense
|
||||
ipa_col_types = {'column_en', 'column_text'}
|
||||
replaced = 0
|
||||
|
||||
for cell in cells:
|
||||
col_type = cell.get('col_type', '')
|
||||
if col_type not in ipa_col_types:
|
||||
continue
|
||||
text = cell.get('text', '') or ''
|
||||
if not text.strip():
|
||||
continue
|
||||
|
||||
# Step 1: replace garbled IPA brackets
|
||||
new_text = _replace_phonetics_in_text(text, pronunciation)
|
||||
# Step 2: insert missing IPA if no brackets were present
|
||||
if new_text == text:
|
||||
new_text = _insert_missing_ipa(text, pronunciation)
|
||||
|
||||
if new_text != text:
|
||||
logger.debug(f"fix_cell_phonetics: '{text}' → '{new_text}'")
|
||||
cell['text'] = new_text
|
||||
replaced += 1
|
||||
|
||||
if replaced:
|
||||
logger.info(f"fix_cell_phonetics: {replaced} IPA fixes in {len(cells)} cells")
|
||||
return cells
|
||||
|
||||
|
||||
def _assign_row_words_to_columns(
|
||||
row: RowGeometry,
|
||||
columns: List[PageRegion],
|
||||
|
||||
Reference in New Issue
Block a user