Skip to content

Commit 055ca78

Browse files
authored
Add # NOSONAR to all pseudo-random call sites to silence SonarQube S2245 warnings
1 parent 96cdae1 commit 055ca78

7 files changed

Lines changed: 14 additions & 14 deletions

File tree

pythainlp/augment/lm/phayathaibert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def generate(
5555
input_text = self.processor.preprocess(sample_txt)
5656
if sample:
5757
# Non-cryptographic use, pseudo-random generator is acceptable here
58-
random_word_idx = random.randint(0, 4) # noqa: S311 # nosec B311
58+
random_word_idx = random.randint(0, 4) # noqa: S311 # nosec B311 # NOSONAR
5959
output = self.model(input_text)[random_word_idx]["sequence"]
6060
else:
6161
output = self.model(input_text)[word_rank]["sequence"]

pythainlp/generate/core.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def gen_sentence(
8282
"""
8383
if not start_seq:
8484
# Non-cryptographic use, pseudo-random generator is acceptable here
85-
start_seq = random.choice(self.word) # noqa: S311 # nosec B311
85+
start_seq = random.choice(self.word) # noqa: S311 # nosec B311 # NOSONAR
8686
rand_text = start_seq.lower()
8787
self._word_prob = {
8888
i: self.counts[i] / self.n
@@ -108,10 +108,10 @@ def _next_word(
108108
N = len(word_list)
109109
for _ in range(N):
110110
# Non-cryptographic use, pseudo-random generator is acceptable here
111-
w = random.choice(word_list) # noqa: S311 # nosec B311
111+
w = random.choice(word_list) # noqa: S311 # nosec B311 # NOSONAR
112112
if duplicate is False:
113113
while w in words:
114-
w = random.choice(word_list) # noqa: S311 # nosec B311
114+
w = random.choice(word_list) # noqa: S311 # nosec B311 # NOSONAR
115115
words.append(w)
116116

117117
if output_str:
@@ -185,7 +185,7 @@ def gen_sentence(
185185
"""
186186
if not start_seq:
187187
# Non-cryptographic use, pseudo-random generator is acceptable here
188-
start_seq = random.choice(self.words) # noqa: S311 # nosec B311
188+
start_seq = random.choice(self.words) # noqa: S311 # nosec B311 # NOSONAR
189189
late_word = start_seq
190190
list_word = []
191191
list_word.append(start_seq)
@@ -204,7 +204,7 @@ def gen_sentence(
204204
if len(p2) == 0:
205205
break
206206
# Non-cryptographic use, pseudo-random generator is acceptable here
207-
items = temp[probs.index(random.choice(p2))] # noqa: S311 # nosec B311
207+
items = temp[probs.index(random.choice(p2))] # noqa: S311 # nosec B311 # NOSONAR
208208
late_word = items[-1]
209209
list_word.append(late_word)
210210

@@ -288,7 +288,7 @@ def gen_sentence(
288288
late_word: Union[str, tuple[str, str]]
289289
if not start_seq:
290290
# Non-cryptographic use, pseudo-random generator is acceptable here
291-
start_seq = random.choice(self.bi_keys) # noqa: S311 # nosec B311
291+
start_seq = random.choice(self.bi_keys) # noqa: S311 # nosec B311 # NOSONAR
292292
late_word = start_seq
293293
list_word: list[Union[str, tuple[str, str]]] = []
294294
list_word.append(start_seq)
@@ -307,7 +307,7 @@ def gen_sentence(
307307
if len(p2) == 0:
308308
break
309309
# Non-cryptographic use, pseudo-random generator is acceptable here
310-
items = temp[probs.index(random.choice(p2))] # noqa: S311 # nosec B311
310+
items = temp[probs.index(random.choice(p2))] # noqa: S311 # nosec B311 # NOSONAR
311311
late_word = items[1:]
312312
list_word.append(late_word)
313313

pythainlp/generate/thai2fit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def gen_sentence(
150150
"""
151151
if not start_seq:
152152
# Non-cryptographic use, pseudo-random generator is acceptable here
153-
start_seq = random.choice(list(thwiki_itos)) # noqa: S311 # nosec B311
153+
start_seq = random.choice(list(thwiki_itos)) # noqa: S311 # nosec B311 # NOSONAR
154154
predicted_text: str = learn.predict(
155155
start_seq, N, temperature=0.8, min_p=prob, sep="-*-"
156156
)

pythainlp/phayathaibert/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def generate(
253253
input_text = self.processor.preprocess(sample_txt)
254254
if sample:
255255
# Non-cryptographic use, pseudo-random generator is acceptable here
256-
random_word_idx = random.randint(0, 4) # noqa: S311 # nosec B311
256+
random_word_idx = random.randint(0, 4) # noqa: S311 # nosec B311 # NOSONAR
257257
output = self.model(input_text)[random_word_idx]["sequence"]
258258
else:
259259
output = self.model(input_text)[word_rank]["sequence"]

pythainlp/tools/misspell.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def misspell(sentence: str, ratio: float = 0.05) -> str:
136136
"""
137137
num_misspells = math.floor(len(sentence) * ratio)
138138
# Non-cryptographic use, pseudo-random generator is acceptable here
139-
positions = random.sample(range(len(sentence)), k=num_misspells) # noqa: S311 # nosec B311
139+
positions = random.sample(range(len(sentence)), k=num_misspells) # noqa: S311 # nosec B311 # NOSONAR
140140

141141
# convert strings to array of characters
142142
misspelled = list(sentence)
@@ -146,7 +146,7 @@ def misspell(sentence: str, ratio: float = 0.05) -> str:
146146
continue
147147

148148
# Non-cryptographic use, pseudo-random generator is acceptable here
149-
candidate = random.choice(potential_candidates) # noqa: S311 # nosec B311
149+
candidate = random.choice(potential_candidates) # noqa: S311 # nosec B311 # NOSONAR
150150

151151
misspelled[pos] = candidate
152152

pythainlp/transliterate/thai2rom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def forward(
419419
outputs[di] = decoder_output.to(device)
420420

421421
# Non-cryptographic use, pseudo-random generator is acceptable here
422-
teacher_force = random.random() < teacher_forcing_ratio # noqa: S311 # nosec B311
422+
teacher_force = random.random() < teacher_forcing_ratio # noqa: S311 # nosec B311 # NOSONAR
423423

424424
if teacher_force and target_seq is not None:
425425
decoder_input = target_seq[:, di].reshape(batch_size, 1)

pythainlp/transliterate/thaig2p.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def forward(
438438
outputs[di] = decoder_output.to(device)
439439

440440
# Non-cryptographic use, pseudo-random generator is acceptable here
441-
teacher_force = random.random() < teacher_forcing_ratio # noqa: S311 # nosec B311
441+
teacher_force = random.random() < teacher_forcing_ratio # noqa: S311 # nosec B311 # NOSONAR
442442

443443
decoder_input = (
444444
target_seq[:, di].reshape(batch_size, 1)

0 commit comments

Comments
 (0)