33# SPDX-License-Identifier: Apache-2.0
44"""Command line for PyThaiNLP's tokenizers."""
55
6+ from __future__ import annotations
7+
68import argparse
9+ from typing import TYPE_CHECKING
710
811from pythainlp import cli
912from pythainlp .tokenize import (
1619)
1720from pythainlp .tools import safe_print
1821
22+ if TYPE_CHECKING :
23+ from collections .abc import Callable , Sequence
24+
1925DEFAULT_SENT_TOKEN_SEPARATOR = "@@" # noqa: S105
2026DEFAULT_SUBWORD_TOKEN_SEPARATOR = "/" # noqa: S105
2127DEFAULT_SYLLABLE_TOKEN_SEPARATOR = "~" # noqa: S105
2228DEFAULT_WORD_TOKEN_SEPARATOR = "|" # noqa: S105
2329
2430
2531class SubAppBase :
26- def __init__ (self , name , argv ):
27- parser = argparse .ArgumentParser (** cli .make_usage ("tokenize " + name ))
32+ separator : str
33+ algorithm : str
34+ run : Callable [..., list [str ]]
35+
36+ def __init__ (self , name : str , argv : Sequence [str ]) -> None :
37+ parser = argparse .ArgumentParser (** cli .make_usage ("tokenize " + name )) # type: ignore[arg-type]
2838 parser .add_argument (
2939 "text" ,
3040 type = str ,
@@ -74,7 +84,7 @@ def __init__(self, name, argv):
7484
7585
7686class WordTokenizationApp (SubAppBase ):
77- def __init__ (self , * args , ** kwargs ) :
87+ def __init__ (self , * args : str , ** kwargs : str ) -> None :
7888 self .keep_whitespace = True
7989 self .algorithm = DEFAULT_WORD_TOKENIZE_ENGINE
8090 self .separator = DEFAULT_WORD_TOKEN_SEPARATOR
@@ -83,7 +93,7 @@ def __init__(self, *args, **kwargs):
8393
8494
8595class SentenceTokenizationApp (SubAppBase ):
86- def __init__ (self , * args , ** kwargs ) :
96+ def __init__ (self , * args : str , ** kwargs : str ) -> None :
8797 self .keep_whitespace = True
8898 self .algorithm = DEFAULT_SENT_TOKENIZE_ENGINE
8999 self .separator = DEFAULT_SENT_TOKEN_SEPARATOR
@@ -92,7 +102,7 @@ def __init__(self, *args, **kwargs):
92102
93103
94104class SubwordTokenizationApp (SubAppBase ):
95- def __init__ (self , * args , ** kwargs ) :
105+ def __init__ (self , * args : str , ** kwargs : str ) -> None :
96106 self .keep_whitespace = True
97107 self .algorithm = DEFAULT_SUBWORD_TOKENIZE_ENGINE
98108 self .separator = DEFAULT_SUBWORD_TOKEN_SEPARATOR
@@ -101,7 +111,7 @@ def __init__(self, *args, **kwargs):
101111
102112
103113class App :
104- def __init__ (self , argv ) :
114+ def __init__ (self , argv : Sequence [ str ]) -> None :
105115 parser = argparse .ArgumentParser (
106116 prog = "tokenize" ,
107117 description = "Break a text into small units (tokens)." ,
@@ -137,10 +147,10 @@ def __init__(self, argv):
137147
138148 argv = argv [3 :]
139149 if token_type .startswith ("w" ):
140- WordTokenizationApp ("word" , argv )
150+ WordTokenizationApp ("word" , argv ) # type: ignore[arg-type]
141151 elif token_type .startswith ("su" ):
142- SubwordTokenizationApp ("subword" , argv )
152+ SubwordTokenizationApp ("subword" , argv ) # type: ignore[arg-type]
143153 elif token_type .startswith ("se" ):
144- SentenceTokenizationApp ("sent" , argv )
154+ SentenceTokenizationApp ("sent" , argv ) # type: ignore[arg-type]
145155 else :
146156 safe_print (f"Token type not available: { token_type } " )
0 commit comments