bengali_romanizer

1from .romanizer import romanize
2
3__version__ = "0.2.1"
4__all__ = ["romanize"]
def romanize(text: str) -> str:
 88def romanize(text: str) -> str:
 89    """Return verified learner-facing readings while preserving uncertain words.
 90
 91    Words with one pronunciation in the bundled expert-built lexicon are
 92    romanized. Unknown words and context-dependent homographs stay in Bengali,
 93    making missing coverage visible instead of presenting a guess as fact.
 94
 95    >>> romanize("বাংলা নববর্ষ")
 96    'bangla nôbobôrsho'
 97    >>> romanize("বাংলায়")
 98    'banglay'
 99    """
100    lexicon = _pronunciations()
101    result: list[str] = []
102    position = 0
103
104    while position < len(text):
105        if _is_bengali(text[position]) and not text[position].isdigit():
106            end = position + 1
107            while end < len(text) and _is_bengali(text[end]):
108                end += 1
109            word = text[position:end]
110            readings = lexicon.get(word, ())
111            result.append(readings[0] if len(readings) == 1 else word)
112            position = end
113        else:
114            result.append(text[position].translate(_DIGITS))
115            position += 1
116
117    return "".join(result)

Return verified learner-facing readings while preserving uncertain words.

Words with one pronunciation in the bundled expert-built lexicon are romanized. Unknown words and context-dependent homographs stay in Bengali, making missing coverage visible instead of presenting a guess as fact.

>>> romanize("বাংলা নববর্ষ")
'bangla nôbobôrsho'
>>> romanize("বাংলায়")
'banglay'