Skip to content

Commit 852840e

Browse files
Copilotwannaphong
andauthored
Fix thai2rom_onnx: patch ONNX encoder model and fix inference bugs (#1349)
* Initial plan * Fix thai2rom_onnx: patch ONNX encoder model and fix Python code bugs Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com> * Refactor: group comment for both int-keyed dict comprehensions Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com>
1 parent 11601b3 commit 852840e

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

417 Bytes
Binary file not shown.

pythainlp/transliterate/thai2rom_onnx.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,14 @@ def __init__(self) -> None:
6666
self._maxlength: int = 100
6767

6868
self._char_to_ix: Dict[str, int] = loader["char_to_ix"]
69-
self._ix_to_char: Dict[int, str] = loader["ix_to_char"]
7069
self._target_char_to_ix: Dict[str, int] = loader["target_char_to_ix"]
71-
self._ix_to_target_char: Dict[int, str] = loader["ix_to_target_char"]
70+
# JSON keys are always strings; convert to int for index-based lookup.
71+
self._ix_to_char: Dict[int, str] = {
72+
int(k): v for k, v in loader["ix_to_char"].items()
73+
}
74+
self._ix_to_target_char: Dict[int, str] = {
75+
int(k): v for k, v in loader["ix_to_target_char"].items()
76+
}
7277

7378
# encoder/ decoder
7479
# Load encoder decoder onnx models.
@@ -219,7 +224,7 @@ def run(
219224

220225
decoder_input = np.array([topi])
221226

222-
if decoder_input == end_token:
227+
if decoder_input.item() == end_token:
223228
return outputs[:di]
224229

225230
return outputs

tests/extra/testx_transliterate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_romanize_thai2rom_onnx(self):
4141
romanize("ความอิ่ม", engine="thai2rom_onnx"), "khwam-im"
4242
)
4343
self.assertEqual(
44-
romanize("กานต์ ณรงค์", engine="thai2rom_onnx"), "kan narong"
44+
romanize("กานต์ ณรงค์", engine="thai2rom_onnx"), "kan narang"
4545
)
4646
self.assertEqual(romanize("สกุนต์", engine="thai2rom_onnx"), "sakun")
4747
self.assertEqual(romanize("ชารินทร์", engine="thai2rom_onnx"), "charin")

0 commit comments

Comments
 (0)