@@ -29,7 +29,7 @@ def __init__(self):
2929 self ._TK_URL ,
3030 self ._TK_END ,
3131 ) = "<unk> <rep> <wrep> <url> </s>" .split ()
32- self .SPACE_SPECIAL_TOKEN = "<_>"
32+ self .SPACE_SPECIAL_TOKEN = "<_>" # noqa: S105
3333
3434 def replace_url (self , text : str ) -> str :
3535 """Replace url in `text` with TK_URL (https://stackoverflow.com/a/6041965)
@@ -60,25 +60,13 @@ def rm_brackets(self, text: str) -> str:
6060 new_line = re .sub (r"\{[^a-zA-Z0-9ก-๙]+\}" , "" , new_line )
6161 new_line = re .sub (r"\[[^a-zA-Z0-9ก-๙]+\]" , "" , new_line )
6262 # artifiacts after (
63- new_line = re .sub (
64- r"(?<=\()[^a-zA-Z0-9ก-๙]+(?=[a-zA-Z0-9ก-๙])" , "" , new_line
65- )
66- new_line = re .sub (
67- r"(?<=\{)[^a-zA-Z0-9ก-๙]+(?=[a-zA-Z0-9ก-๙])" , "" , new_line
68- )
69- new_line = re .sub (
70- r"(?<=\[)[^a-zA-Z0-9ก-๙]+(?=[a-zA-Z0-9ก-๙])" , "" , new_line
71- )
63+ new_line = re .sub (r"(?<=\()[^a-zA-Z0-9ก-๙]+(?=[a-zA-Z0-9ก-๙])" , "" , new_line )
64+ new_line = re .sub (r"(?<=\{)[^a-zA-Z0-9ก-๙]+(?=[a-zA-Z0-9ก-๙])" , "" , new_line )
65+ new_line = re .sub (r"(?<=\[)[^a-zA-Z0-9ก-๙]+(?=[a-zA-Z0-9ก-๙])" , "" , new_line )
7266 # artifacts before )
73- new_line = re .sub (
74- r"(?<=[a-zA-Z0-9ก-๙])[^a-zA-Z0-9ก-๙]+(?=\))" , "" , new_line
75- )
76- new_line = re .sub (
77- r"(?<=[a-zA-Z0-9ก-๙])[^a-zA-Z0-9ก-๙]+(?=\})" , "" , new_line
78- )
79- new_line = re .sub (
80- r"(?<=[a-zA-Z0-9ก-๙])[^a-zA-Z0-9ก-๙]+(?=\])" , "" , new_line
81- )
67+ new_line = re .sub (r"(?<=[a-zA-Z0-9ก-๙])[^a-zA-Z0-9ก-๙]+(?=\))" , "" , new_line )
68+ new_line = re .sub (r"(?<=[a-zA-Z0-9ก-๙])[^a-zA-Z0-9ก-๙]+(?=\})" , "" , new_line )
69+ new_line = re .sub (r"(?<=[a-zA-Z0-9ก-๙])[^a-zA-Z0-9ก-๙]+(?=\])" , "" , new_line )
8270 return new_line
8371
8472 def replace_newlines (self , text : str ) -> str :
@@ -103,7 +91,7 @@ def rm_useless_spaces(self, text: str) -> str:
10391 """
10492 return re .sub (" {2,}" , " " , text )
10593
106- def replace_spaces (self , text : str , space_token : str = "<_>" ) -> str :
94+ def replace_spaces (self , text : str , space_token : str = "<_>" ) -> str : # noqa: S107
10795 """Replace spaces with _
10896 :param str text: text to replace spaces
10997 :return: text where all spaces replaced with _
@@ -206,9 +194,7 @@ def __init__(self) -> None:
206194 )
207195
208196 self .tokenizer = AutoTokenizer .from_pretrained (_model_name )
209- self .model_for_masked_lm = AutoModelForMaskedLM .from_pretrained (
210- _model_name
211- )
197+ self .model_for_masked_lm = AutoModelForMaskedLM .from_pretrained (_model_name )
212198 self .model = pipeline (
213199 "fill-mask" ,
214200 tokenizer = self .tokenizer ,
@@ -223,15 +209,17 @@ def generate(
223209 max_length : int = 3 ,
224210 sample : bool = False ,
225211 ) -> str :
212+ """Generate text from PhayaThaiBERT"""
226213 sample_txt = sample_text
227214 final_text = ""
228- for j in range (max_length ):
229- input = self .processor .preprocess (sample_txt )
215+ for _ in range (max_length ):
216+ input_text = self .processor .preprocess (sample_txt )
230217 if sample :
231- random_word_idx = random .randint (0 , 4 )
232- output = self .model (input )[random_word_idx ]["sequence" ]
218+ # Non-cryptographic use, pseudo-random generator is acceptable here
219+ random_word_idx = random .randint (0 , 4 ) # noqa: S311
220+ output = self .model (input_text )[random_word_idx ]["sequence" ]
233221 else :
234- output = self .model (input )[word_rank ]["sequence" ]
222+ output = self .model (input_text )[word_rank ]["sequence" ]
235223 sample_txt = output + "<mask>"
236224 final_text = sample_txt
237225
@@ -279,9 +267,7 @@ def augment(
279267 rank ,
280268 sample = sample ,
281269 )
282- processed_text = re .sub (
283- "<_>" , " " , self .processor .preprocess (gen_text )
284- )
270+ processed_text = re .sub ("<_>" , " " , self .processor .preprocess (gen_text ))
285271 augment_list .append (processed_text )
286272 else :
287273 raise ValueError (
@@ -383,7 +369,8 @@ def get_ner(
383369 if pos :
384370 warnings .warn (
385371 "This model doesn't support output \
386- postag and It doesn't output the postag."
372+ postag and It doesn't output the postag." ,
373+ stacklevel = 2 ,
387374 )
388375
389376 sample_output = []
0 commit comments