Skip to content

Fix KeyError when transliterating text containing obsolete Thai letter ฅ - #1187

Merged
bact merged 2 commits into
devfrom
copilot/fix-transliterator-key-error
Jan 11, 2026
Merged

Fix KeyError when transliterating text containing obsolete Thai letter ฅ#1187
bact merged 2 commits into
devfrom
copilot/fix-transliterator-key-error

Conversation

Copilot AI commented Jan 10, 2026

Copy link
Copy Markdown
Contributor

What does this changes

Adds preprocessing to replace obsolete Thai letter (Kho Khon) with (Kho Khwai) before calling tltk library functions.

What was wrong

The tltk library throws KeyError: 'ฅ' when encountering the obsolete Thai character ฅ because it's not in tltk's vocabulary. This occurs in g2p(), th2ipa(), and th2roman() functions.

transliterate("ภาษาไวคาลีฅ", engine="tltk_g2p")
# KeyError: 'ฅ' at tltk/nlp.py line 1929

How this fixes it

  • Modified romanize(), tltk_g2p(), and tltk_ipa() in pythainlp/transliterate/tltk.py to replace with before processing
  • Both characters are phonetically identical, so transliteration output is unaffected
  • Added test coverage for the ฅ character in tests/extra/testx_transliterate.py

Your checklist for this pull request

  • Passed code styles and structures
  • Passed code linting checks and unit test
Original prompt

This section details on the original issue you should resolve

<issue_title>Transliterator cannot handle ฅ</issue_title>
<issue_description>transliterate sometimes throws a KeyError when the input contains the character .

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.</issue_description>

<agent_instructions>Fix pythainlp/transliterate/tltk.py</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@wannaphong Thank you for your report! It is tltk's issue because it is out-of-vocabulary(OOV) problem.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
@sonarqubecloud

Copy link
Copy Markdown

Copilot AI changed the title [WIP] Fix transliterator to handle character ฅ Fix KeyError when transliterating text containing obsolete Thai letter ฅ Jan 10, 2026
Copilot AI requested a review from wannaphong January 10, 2026 16:36
@bact
bact marked this pull request as ready for review January 10, 2026 16:44
@coveralls

coveralls commented Jan 10, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 52.587% (-0.02%) from 52.606%
when pulling 621e15d on copilot/fix-transliterator-key-error
into 2d1466a on dev.

@bact bact added the bug bugs in the library label Jan 10, 2026
@bact
bact merged commit f59b908 into dev Jan 11, 2026
44 checks passed
@bact
bact deleted the copilot/fix-transliterator-key-error branch January 11, 2026 17:02
@bact bact mentioned this pull request Jan 11, 2026
@bact bact added this to the 5.3 milestone Jan 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug bugs in the library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Transliterator cannot handle ฅ

4 participants