Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pythainlp/tokenize/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def word_detokenize(
"""
list_all: list[list[str]] = []

if not segments:
return "" if output == "str" else []

if isinstance(segments[0], str):
segments = [segments] # type: ignore[assignment]

Expand All @@ -63,6 +66,8 @@ def word_detokenize(
space_index: list[int] = []
mark_index: list[int] = []
for j, w in enumerate(s):
if not w:
continue
if j > 0:
# previous word
p_w = s[j - 1]
Expand All @@ -75,7 +80,7 @@ def word_detokenize(
list_sents.append(" ")
add_index.append(j)
# if previous word is number or other language and is not space
elif p_w[0] not in thai_characters and not p_w.isspace():
elif p_w and p_w[0] not in thai_characters and not p_w.isspace():
list_sents.append(" ")
add_index.append(j)
# if word is Thai iteration mark
Expand Down
6 changes: 6 additions & 0 deletions tests/core/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ def test_word_detokenize(self):
word_detokenize(["ม่ายย", " ", "ผม", "เลี้ยง", "5", "ตัว"]),
"ม่ายย ผมเลี้ยง 5 ตัว",
)
# Reproduce: empty strings in token list should not cause IndexError
self.assertIsInstance(word_detokenize(["สวัสดี", "", "ครับ"]), str)
self.assertIsInstance(word_detokenize(["hello", "", "world"]), str)
self.assertIsInstance(word_detokenize([""]), str)
# Empty list should not crash
self.assertEqual(word_detokenize([]), "")

def test_numeric_data_format(self):
engines = ["newmm", "longest"]
Expand Down
Loading