Skip to content

Commit ba221c3

Browse files
committed
Fix few lint issues
1 parent 07f16b1 commit ba221c3

7 files changed

Lines changed: 36 additions & 46 deletions

File tree

pythainlp/augment/word2vec/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ def augment(
5555
:return: list of synonyms
5656
:rtype: list[tuple[str, ...]]
5757
"""
58-
self.sentence = self.tokenizer(sentence)
59-
self.list_synonym = self.modify_sent(self.sentence, p=p)
58+
_sentence = self.tokenizer(sentence)
59+
_list_synonym = self.modify_sent(_sentence, p=p)
6060
new_sentences = []
61-
for x in list(itertools.product(*self.list_synonym))[0:n_sent]:
61+
for x in list(itertools.product(*_list_synonym))[0:n_sent]:
6262
new_sentences.append(x)
6363
return new_sentences

pythainlp/corpus/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from __future__ import annotations
99

1010
import ast
11-
from typing import Any, Union
11+
from typing import Union
1212

1313
__all__ = [
1414
"countries",

pythainlp/corpus/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def json(self) -> dict:
4040
try:
4141
return json.loads(self._content.decode("utf-8"))
4242
except (json.JSONDecodeError, UnicodeDecodeError) as err:
43-
raise ValueError(f"Failed to parse JSON response: {err}")
43+
raise ValueError(f"Failed to parse JSON response: {err}") from err
4444

4545

4646
def get_corpus_db(url: str) -> Optional[_ResponseWrapper]:

pythainlp/tag/_tag_perceptron.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ def load(self, loc: str) -> None:
203203
try:
204204
with open(loc, encoding="utf-8-sig") as f:
205205
w_td_c = json.load(f)
206-
except OSError:
206+
except OSError as ex:
207207
msg = "Missing trontagger.json file."
208-
raise OSError(msg)
208+
raise OSError(msg) from ex
209209
self.model.weights = w_td_c["weights"]
210210
self.tagdict = w_td_c["tagdict"]
211211
self.classes = w_td_c["classes"]

pythainlp/tokenize/core.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
_RE_WORD_CHAR = re.compile(r"\w")
3131

3232

33-
3433
def word_detokenize(
3534
segments: Union[list[list[str]], list[str]], output: str = "str"
3635
) -> Union[list[str], str]:
@@ -483,16 +482,13 @@ def sent_tokenize(
483482
if not text or not isinstance(text, (str, list)):
484483
return []
485484

486-
is_list_input = isinstance(text, list)
487-
488-
if is_list_input:
485+
if isinstance(text, list):
489486
try:
490487
original_text = "".join(text)
491488
except ValueError:
492489
return []
493-
494490
else:
495-
original_text = text
491+
original_text = str(text)
496492

497493
segments = []
498494

@@ -501,13 +497,13 @@ def sent_tokenize(
501497

502498
segments = segment(original_text)
503499

504-
if is_list_input:
500+
if isinstance(text, list):
505501
word_indices = indices_words(text)
506502
result = map_indices_to_words(word_indices, [original_text])
507503
return result
508504
elif engine == "whitespace":
509505
segments = re.split(r" +", original_text, flags=re.U)
510-
if is_list_input:
506+
if isinstance(text, list):
511507
result = []
512508
_temp: list[str] = []
513509
for i, w in enumerate(text):
@@ -523,7 +519,7 @@ def sent_tokenize(
523519
return result
524520
elif engine == "whitespace+newline":
525521
segments = original_text.split()
526-
if is_list_input:
522+
if isinstance(text, list):
527523
result = []
528524
_temp = []
529525
for i, w in enumerate(text):
@@ -538,24 +534,22 @@ def sent_tokenize(
538534
result.append(_temp)
539535
return result
540536
elif engine == "tltk":
541-
from pythainlp.tokenize.tltk import sent_tokenize as segment
537+
from pythainlp.tokenize.tltk import sent_tokenize as tltk_sent_tokenize
542538

543-
segments = segment(original_text)
539+
segments = tltk_sent_tokenize(original_text)
544540
elif engine == "thaisum":
545-
from pythainlp.tokenize.thaisumcut import (
546-
ThaiSentenceSegmentor as segmentor,
547-
)
541+
from pythainlp.tokenize.thaisumcut import ThaiSentenceSegmentor
548542

549-
segment = segmentor()
550-
segments = segment.split_into_sentences(original_text)
543+
segmentor = ThaiSentenceSegmentor()
544+
segments = segmentor.split_into_sentences(original_text)
551545
elif engine.startswith("wtp"):
552546
if "-" not in engine:
553547
_size = "mini"
554548
else:
555549
_size = engine.split("-")[-1]
556-
from pythainlp.tokenize.wtsplit import tokenize as segment
550+
from pythainlp.tokenize.wtsplit import tokenize
557551

558-
segments = segment(original_text, size=_size, tokenize="sentence")
552+
segments = tokenize(text=original_text, size=_size, tokenize="sentence")
559553
else:
560554
raise ValueError(
561555
f"""Tokenizer \"{engine}\" not found.
@@ -565,7 +559,7 @@ def sent_tokenize(
565559
if not keep_whitespace:
566560
segments = strip_whitespace(segments)
567561

568-
if is_list_input and engine not in ["crfcut"]:
562+
if isinstance(text, list) and engine not in ["crfcut"]:
569563
word_indices = indices_words(text)
570564
result = map_indices_to_words(word_indices, segments)
571565
return result
@@ -914,7 +908,7 @@ class Tokenizer:
914908

915909
def __init__(
916910
self,
917-
custom_dict: Union[Trie, Iterable[str], str] = [],
911+
custom_dict: Union[Trie, Iterable[str], str, None] = None,
918912
engine: str = "newmm",
919913
keep_whitespace: bool = True,
920914
join_broken_num: bool = True,
@@ -937,10 +931,8 @@ def __init__(
937931
self.__engine = engine
938932
if self.__engine not in ["newmm", "mm", "longest", "deepcut"]:
939933
raise NotImplementedError(
940-
"""
941-
The Tokenizer class is not support %s for custom tokenizer
942-
"""
943-
% self.__engine
934+
"The Tokenizer class does not support "
935+
f"{self.__engine} for custom tokenizer."
944936
)
945937
self.__keep_whitespace = keep_whitespace
946938
self.__join_broken_num = join_broken_num

pythainlp/tokenize/longest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def __init__(self, trie: Trie):
5252
@staticmethod
5353
def __search_nonthai(text: str) -> Optional[str]:
5454
match = _RE_NONTHAI.search(text)
55+
if not match:
56+
return None
5557
if match.group(0):
5658
return match.group(0).lower()
5759
return None

pythainlp/util/date.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@
2626

2727
import re
2828
from datetime import datetime, timedelta
29-
30-
try:
31-
from zoneinfo import ZoneInfo
32-
except ImportError:
33-
from backports.zoneinfo import ZoneInfo # type: ignore[no-redef]
34-
29+
from zoneinfo import ZoneInfo
3530

3631
thai_abbr_weekdays = ["จ", "อ", "พ", "พฤ", "ศ", "ส", "อา"]
3732
thai_full_weekdays = [
@@ -290,19 +285,19 @@ def thai_strptime(
290285
y_matches = re.findall(fmt, text)
291286

292287
data = {i: "".join(list(j)) for i, j in zip(keys, y_matches[0])}
293-
H: Union[int, str] = 0
294-
M: Union[int, str] = 0
295-
S: Union[int, str] = 0
288+
hour: Union[int, str] = 0
289+
minute: Union[int, str] = 0
290+
second: Union[int, str] = 0
296291
f: Union[int, str] = 0
297292
d = data["d"]
298293
m = _find_month(data["B"])
299294
y = data["Y"]
300295
if "H" in keys:
301-
H = data["H"]
296+
hour = data["H"]
302297
if "M" in keys:
303-
M = data["M"]
298+
minute = data["M"]
304299
if "S" in keys:
305-
S = data["S"]
300+
second = data["S"]
306301
if "f" in keys:
307302
f = data["f"]
308303
if int(y) < 100 and year == "be":
@@ -321,9 +316,9 @@ def thai_strptime(
321316
year=int(y),
322317
month=int(m),
323318
day=int(d),
324-
hour=int(H),
325-
minute=int(M),
326-
second=int(S),
319+
hour=int(hour),
320+
minute=int(minute),
321+
second=int(second),
327322
microsecond=int(f),
328323
tzinfo=tzinfo,
329324
)
@@ -376,6 +371,7 @@ def reign_year_to_ad(reign_year: int, reign: int) -> int:
376371
reign_year_to_ad(1, 9))
377372
# output: The 4th reign year of the King Rama X is in 1946
378373
"""
374+
ad = 0
379375
if int(reign) == 10:
380376
ad = int(reign_year) + 2015
381377
elif int(reign) == 9:

0 commit comments

Comments
 (0)