From 54ab172c6af0e97c2771dfb1c577962955220820 Mon Sep 17 00:00:00 2001 From: phoneee Date: Sun, 29 Mar 2026 16:43:22 +0700 Subject: [PATCH 1/2] fix: guard han_solo list_cut[-1] and narrow longest.py exception catch han_solo: if CRF predicts "0" for first char, list_cut is empty and list_cut[-1] raises IndexError. Add fallback to append when empty. longest: except BaseException catches KeyboardInterrupt and SystemExit. Narrow to except IndexError which is the only expected exception. --- pythainlp/tokenize/han_solo.py | 2 +- pythainlp/tokenize/longest.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pythainlp/tokenize/han_solo.py b/pythainlp/tokenize/han_solo.py index dc1b18ecf..efcbf43b0 100644 --- a/pythainlp/tokenize/han_solo.py +++ b/pythainlp/tokenize/han_solo.py @@ -162,7 +162,7 @@ def segment(text: str) -> list[str]: y_pred = tagger.tag(x) list_cut = [] for j, k in zip(list(text), y_pred): - if k == "1": + if k == "1" or not list_cut: list_cut.append(j) else: list_cut[-1] += j diff --git a/pythainlp/tokenize/longest.py b/pythainlp/tokenize/longest.py index e2453c7de..3ea12d599 100644 --- a/pythainlp/tokenize/longest.py +++ b/pythainlp/tokenize/longest.py @@ -105,7 +105,7 @@ def __longest_matching(self, text: str, begin_pos: int) -> str: return text[0 : len_word_valid + 1] else: return word_valid - except BaseException: + except IndexError: return word_valid else: return "" From 06e016ff02da758411da351d511f11a681d81cbf Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 30 Mar 2026 19:07:56 +0100 Subject: [PATCH 2/2] Update pythainlp/tokenize/han_solo.py --- pythainlp/tokenize/han_solo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythainlp/tokenize/han_solo.py b/pythainlp/tokenize/han_solo.py index efcbf43b0..4e455f394 100644 --- a/pythainlp/tokenize/han_solo.py +++ b/pythainlp/tokenize/han_solo.py @@ -160,7 +160,7 @@ def segment(text: str) -> list[str]: tagger = _get_tagger() x = _to_feature.featurize(text)["X"] y_pred = tagger.tag(x) - list_cut = [] + list_cut: list[str] = [] for j, k in zip(list(text), y_pred): if k == "1" or not list_cut: list_cut.append(j)