Skip to content

ฅ sometimes breaks transliterator #9

@tabidots

Description

@tabidots

pythainlp.transliterate.transliterate() sometimes throws a KeyError when the input contains the character . The stacktrace points to ReplaceSnd in nlp.py.

In the following output, the word ภาษาไวคาลีฅ is taken from the list of processed Wikipedia article titles in this repo.

>>> transliterate("ภาษาไวคาลีฅ", engine="tltk_g2p")
Traceback (most recent call last):
  File "<python-input-7>", line 1, in <module>
    transliterate("ภาษาไวคาลีฅ", engine="tltk_g2p")
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<SNIP>/venv/lib/python3.13/site-packages/pythainlp/transliterate/core.py", line 185, in transliterate
    return transliterate(text)
  File "/<SNIP>/venv/lib/python3.13/site-packages/pythainlp/transliterate/tltk.py", line 24, in tltk_g2p
    _temp = g2p(text).split("<tr/>")[1].replace("|<s/>", "").replace("|", " ")
            ~~~^^^^^^
  File "/<SNIP>/venv/lib/python3.13/site-packages/tltk/nlp.py", line 1690, in g2p
    y = sylparse(inp)
  File "/<SNIP>/venv/lib/python3.13/site-packages/tltk/nlp.py", line 1767, in sylparse
    phone = ReplaceSnd(PronF,codematch,charmatch)
  File "/<SNIP>/venv/lib/python3.13/site-packages/tltk/nlp.py", line 1929, in ReplaceSnd
    s = stable[x][tmp1Lst[i]]
        ~~~~~~~~~^^^^^^^^^^^^
KeyError: 'ฅ'

For now, this can be solved easily with this basic replacement: text.replace("ฅ", "ค"), which will not affect the transliteration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions