Skip to content

Commit 9bc9bb5

Browse files
authored
Merge pull request #1382 from phoneee/fix/tokenize-defensive-guards
fix: guard han_solo list_cut[-1] and narrow longest.py exception catch
2 parents 67187a1 + 06e016f commit 9bc9bb5

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

pythainlp/tokenize/han_solo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ def segment(text: str) -> list[str]:
160160
tagger = _get_tagger()
161161
x = _to_feature.featurize(text)["X"]
162162
y_pred = tagger.tag(x)
163-
list_cut = []
163+
list_cut: list[str] = []
164164
for j, k in zip(list(text), y_pred):
165-
if k == "1":
165+
if k == "1" or not list_cut:
166166
list_cut.append(j)
167167
else:
168168
list_cut[-1] += j

pythainlp/tokenize/longest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def __longest_matching(self, text: str, begin_pos: int) -> str:
105105
return text[0 : len_word_valid + 1]
106106
else:
107107
return word_valid
108-
except BaseException:
108+
except IndexError:
109109
return word_valid
110110
else:
111111
return ""

0 commit comments

Comments
 (0)