Skip to content

Commit e4e46cd

Browse files
Copilotbact
andcommitted
Use 'import as' to avoid type: ignore[assignment] for import shadowing
Instead of using `# type: ignore[assignment]` when importing different segment functions with the same name, use unique aliases with 'import as': - from pythainlp.tokenize.attacut import segment as attacut_segment - from pythainlp.tokenize.sefr_cut import segment as sefrcut_segment This eliminates the need for type: ignore comments while making the code more explicit about which segment function is being used in each case. Benefits: - No type: ignore[assignment] needed for import shadowing - More explicit and readable code - Mypy can properly track all imports - Easier to debug which tokenizer is being used Co-authored-by: bact <128572+bact@users.noreply.github.com>
1 parent 3fbbeb3 commit e4e46cd

1 file changed

Lines changed: 41 additions & 36 deletions

File tree

pythainlp/tokenize/core.py

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -259,56 +259,56 @@ def word_tokenize(
259259

260260
segments = segment(text, custom_dict, safe_mode=True)
261261
elif engine == "attacut":
262-
from pythainlp.tokenize.attacut import segment # type: ignore[assignment] # noqa: I001
262+
from pythainlp.tokenize.attacut import segment as attacut_segment # noqa: I001
263263

264-
segments = segment(text)
264+
segments = attacut_segment(text)
265265
elif engine == "longest":
266-
from pythainlp.tokenize.longest import segment # type: ignore[assignment] # noqa: I001
266+
from pythainlp.tokenize.longest import segment as longest_segment # noqa: I001
267267

268-
segments = segment(text, custom_dict)
268+
segments = longest_segment(text, custom_dict)
269269
elif engine in ("mm", "multi_cut"):
270-
from pythainlp.tokenize.multi_cut import segment # type: ignore[assignment] # noqa: I001
270+
from pythainlp.tokenize.multi_cut import segment as multi_cut_segment # noqa: I001
271271

272-
segments = segment(text, custom_dict)
272+
segments = multi_cut_segment(text, custom_dict)
273273
elif engine == "deepcut": # deepcut can optionally use dictionary
274-
from pythainlp.tokenize.deepcut import segment # type: ignore[assignment] # noqa: I001
274+
from pythainlp.tokenize.deepcut import segment as deepcut_segment # noqa: I001
275275

276276
if custom_dict:
277277
custom_dict = list(custom_dict) # type: ignore[assignment]
278-
segments = segment(text, custom_dict)
278+
segments = deepcut_segment(text, custom_dict)
279279
else:
280-
segments = segment(text)
280+
segments = deepcut_segment(text)
281281
elif engine == "icu":
282-
from pythainlp.tokenize.pyicu import segment # type: ignore[assignment] # noqa: I001
282+
from pythainlp.tokenize.pyicu import segment as pyicu_segment # noqa: I001
283283

284-
segments = segment(text)
284+
segments = pyicu_segment(text)
285285
elif engine == "budoux":
286-
from pythainlp.tokenize.budoux import segment # type: ignore[assignment] # noqa: I001
286+
from pythainlp.tokenize.budoux import segment as budoux_segment # noqa: I001
287287

288-
segments = segment(text)
288+
segments = budoux_segment(text)
289289
elif engine == "nercut":
290-
from pythainlp.tokenize.nercut import segment # type: ignore[assignment] # noqa: I001
290+
from pythainlp.tokenize.nercut import segment as nercut_segment # noqa: I001
291291

292-
segments = segment(text)
292+
segments = nercut_segment(text)
293293
elif engine == "sefr_cut":
294-
from pythainlp.tokenize.sefr_cut import segment # type: ignore[assignment] # noqa: I001
294+
from pythainlp.tokenize.sefr_cut import segment as sefrcut_segment # noqa: I001
295295

296-
segments = segment(text)
296+
segments = sefrcut_segment(text)
297297
elif engine == "tltk":
298-
from pythainlp.tokenize.tltk import segment # type: ignore[assignment] # noqa: I001
298+
from pythainlp.tokenize.tltk import segment as tltk_segment # noqa: I001
299299

300-
segments = segment(text)
300+
segments = tltk_segment(text)
301301
elif engine == "oskut":
302-
from pythainlp.tokenize.oskut import segment # type: ignore[assignment] # noqa: I001
302+
from pythainlp.tokenize.oskut import segment as oskut_segment # noqa: I001
303303

304-
segments = segment(text)
304+
segments = oskut_segment(text)
305305
elif engine == "nlpo3":
306-
from pythainlp.tokenize.nlpo3 import segment # type: ignore[assignment] # noqa: I001
306+
from pythainlp.tokenize.nlpo3 import segment as nlpo3_segment # noqa: I001
307307

308308
# Currently cannot handle custom_dict from inside word_tokenize(),
309309
# due to difference in type.
310310
# if isinstance(custom_dict, str):
311-
# segments = segment(text, custom_dict=custom_dict)
311+
# segments = nlpo3_segment(text, custom_dict=custom_dict)
312312
# elif not isinstance(custom_dict, str) and not custom_dict:
313313
# raise ValueError(
314314
# f"""Tokenizer \"{engine}\":
@@ -317,8 +317,8 @@ def word_tokenize(
317317
# See pythainlp.tokenize.nlpo3.load_dict()"""
318318
# )
319319
# else:
320-
# segments = segment(text)
321-
segments = segment(text)
320+
# segments = nlpo3_segment(text)
321+
segments = nlpo3_segment(text)
322322
else:
323323
raise ValueError(
324324
f"""Tokenizer \"{engine}\" not found.
@@ -719,36 +719,41 @@ def subword_tokenize(
719719
segments = []
720720

721721
if engine == "tcc":
722-
from pythainlp.tokenize.tcc import segment
722+
from pythainlp.tokenize.tcc import segment as tcc_segment
723+
segments = tcc_segment(text)
723724
elif engine == "tcc_p":
724-
from pythainlp.tokenize.tcc_p import segment
725+
from pythainlp.tokenize.tcc_p import segment as tcc_p_segment
726+
segments = tcc_p_segment(text)
725727
elif engine == "etcc":
726-
from pythainlp.tokenize.etcc import segment
728+
from pythainlp.tokenize.etcc import segment as etcc_segment
729+
segments = etcc_segment(text)
727730
elif engine == "wangchanberta":
728-
from pythainlp.wangchanberta import segment
731+
from pythainlp.wangchanberta import segment as wangchanberta_segment
732+
segments = wangchanberta_segment(text)
729733
elif engine == "dict": # use syllable dictionary
730734
words = word_tokenize(text)
731735
for word in words:
732736
segments.extend(
733737
word_tokenize(text=word, custom_dict=syllable_dict_trie())
734738
)
735739
elif engine == "ssg":
736-
from pythainlp.tokenize.ssg import segment
740+
from pythainlp.tokenize.ssg import segment as ssg_segment
741+
segments = ssg_segment(text)
737742
elif engine == "tltk":
738-
from pythainlp.tokenize.tltk import syllable_tokenize as segment
743+
from pythainlp.tokenize.tltk import syllable_tokenize as tltk_segment
744+
segments = tltk_segment(text)
739745
elif engine == "han_solo":
740-
from pythainlp.tokenize.han_solo import segment
746+
from pythainlp.tokenize.han_solo import segment as han_solo_segment
747+
segments = han_solo_segment(text)
741748
elif engine == "phayathai":
742-
from pythainlp.phayathaibert import segment # type: ignore[assignment]
749+
from pythainlp.phayathaibert import segment as phayathai_segment
750+
segments = phayathai_segment(text)
743751
else:
744752
raise ValueError(
745753
f"""Tokenizer \"{engine}\" not found.
746754
It might be a typo; if not, please consult our document."""
747755
)
748756

749-
if not segments:
750-
segments = segment(text)
751-
752757
if not keep_whitespace:
753758
segments = strip_whitespace(segments)
754759

0 commit comments

Comments
 (0)