Skip to content

Commit da8c3b6

Browse files
authored
Merge pull request #1189 from PyThaiNLP/copilot/update-python-type-hints
Modernize to Python 3.9: type hints with future annotations, exception handling, and code quality
2 parents 6439d0d + 01a0a1f commit da8c3b6

191 files changed

Lines changed: 2100 additions & 1862 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[tool.ruff]
66
line-length = 79
77
indent-width = 4
8-
target-version = "py310"
8+
target-version = "py39"
99

1010
[tool.ruff.format]
1111
quote-style = "double"

pythainlp/__init__.py

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,63 @@
1-
# -*- coding: utf-8 -*-
2-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
3-
# SPDX-FileType: SOURCE
4-
# SPDX-License-Identifier: Apache-2.0
5-
__version__ = "5.2.0"
6-
7-
thai_consonants = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬอฮ" # 44 chars
8-
9-
thai_vowels = (
10-
"\u0e24\u0e26\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35\u0e36\u0e37"
11-
+ "\u0e38\u0e39\u0e40\u0e41\u0e42\u0e43\u0e44\u0e45\u0e4d\u0e47"
12-
) # 20
13-
thai_lead_vowels = "\u0e40\u0e41\u0e42\u0e43\u0e44" # 5
14-
thai_follow_vowels = "\u0e30\u0e32\u0e33\u0e45" # 4
15-
thai_above_vowels = "\u0e31\u0e34\u0e35\u0e36\u0e37\u0e4d\u0e47" # 7
16-
thai_below_vowels = "\u0e38\u0e39" # 2
17-
18-
thai_tonemarks = "\u0e48\u0e49\u0e4a\u0e4b" # 4
19-
20-
# Paiyannoi, Maiyamok, Phinthu, Thanthakhat, Nikhahit, Yamakkan:
21-
# These signs can be part of a word
22-
thai_signs = "\u0e2f\u0e3a\u0e46\u0e4c\u0e4d\u0e4e" # 6 chars
23-
24-
# Any Thai character that can be part of a word
25-
thai_letters = "".join(
26-
[thai_consonants, thai_vowels, thai_tonemarks, thai_signs]
27-
) # 74
28-
29-
# Fongman, Angkhankhu, Khomut:
30-
# These characters are section markers
31-
thai_punctuations = "\u0e4f\u0e5a\u0e5b" # 3 chars
32-
33-
thai_digits = "๐๑๒๓๔๕๖๗๘๙" # 10
34-
thai_symbols = "\u0e3f" # Thai Bath ฿
35-
36-
# All Thai characters that are presented in Unicode
37-
thai_characters = "".join(
38-
[thai_letters, thai_punctuations, thai_digits, thai_symbols]
39-
)
40-
# Thai pangram by Sungsit Sawaiwan
41-
# CC BY-SA License
42-
# Source: https://fontuni.com/articles/2015-07-12-thai-poetgram.html
43-
thai_pangram = """กีฬาบังลังก์ ฿๑,๒๓๔,๕๖๗,๘๙๐
44-
๏ จับฅอคนบั่นต้อง อาญา
45-
ขุดฆ่าโคตรฃัตติยา ซ่านม้วย
46-
ธรรมฤๅผ่อนรักษา ใจชั่ว โฉดแฮ
47-
สืบอยู่เต็มศึกด้วย ฝุ่นฟ้ากีฬา กามฦๅ ฯ
48-
๏ กตัญญูไป่พร้อม ปฐมฌาน
49-
เกมส๎วัฒน์ปฏิภาณ ห่อนล้ำ
50-
ทฤษฎีถ่อยๆ สังหาร เกณฑ์โทษ
51-
โกรธจี๊ดจ๋อยจ่มถ้ำ อยู่เฝ้า “อตฺตา” ๚ะ๛
52-
๑๒ กรกฎาคม ๒๕๕๘"""
53-
54-
from pythainlp.soundex import soundex
55-
from pythainlp.spell import correct, spell
56-
from pythainlp.tag import pos_tag
57-
from pythainlp.tokenize import (
58-
Tokenizer,
59-
sent_tokenize,
60-
subword_tokenize,
61-
word_tokenize,
62-
)
63-
from pythainlp.transliterate import romanize, transliterate
64-
from pythainlp.util import collate, thai_strftime
1+
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
2+
# SPDX-FileType: SOURCE
3+
# SPDX-License-Identifier: Apache-2.0
4+
__version__ = "5.2.0"
5+
6+
thai_consonants = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬอฮ" # 44 chars
7+
8+
thai_vowels = (
9+
"\u0e24\u0e26\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35\u0e36\u0e37"
10+
+ "\u0e38\u0e39\u0e40\u0e41\u0e42\u0e43\u0e44\u0e45\u0e4d\u0e47"
11+
) # 20
12+
thai_lead_vowels = "\u0e40\u0e41\u0e42\u0e43\u0e44" # 5
13+
thai_follow_vowels = "\u0e30\u0e32\u0e33\u0e45" # 4
14+
thai_above_vowels = "\u0e31\u0e34\u0e35\u0e36\u0e37\u0e4d\u0e47" # 7
15+
thai_below_vowels = "\u0e38\u0e39" # 2
16+
17+
thai_tonemarks = "\u0e48\u0e49\u0e4a\u0e4b" # 4
18+
19+
# Paiyannoi, Maiyamok, Phinthu, Thanthakhat, Nikhahit, Yamakkan:
20+
# These signs can be part of a word
21+
thai_signs = "\u0e2f\u0e3a\u0e46\u0e4c\u0e4d\u0e4e" # 6 chars
22+
23+
# Any Thai character that can be part of a word
24+
thai_letters = "".join(
25+
[thai_consonants, thai_vowels, thai_tonemarks, thai_signs]
26+
) # 74
27+
28+
# Fongman, Angkhankhu, Khomut:
29+
# These characters are section markers
30+
thai_punctuations = "\u0e4f\u0e5a\u0e5b" # 3 chars
31+
32+
thai_digits = "๐๑๒๓๔๕๖๗๘๙" # 10
33+
thai_symbols = "\u0e3f" # Thai Bath ฿
34+
35+
# All Thai characters that are presented in Unicode
36+
thai_characters = "".join(
37+
[thai_letters, thai_punctuations, thai_digits, thai_symbols]
38+
)
39+
# Thai pangram by Sungsit Sawaiwan
40+
# CC BY-SA License
41+
# Source: https://fontuni.com/articles/2015-07-12-thai-poetgram.html
42+
thai_pangram = """กีฬาบังลังก์ ฿๑,๒๓๔,๕๖๗,๘๙๐
43+
๏ จับฅอคนบั่นต้อง อาญา
44+
ขุดฆ่าโคตรฃัตติยา ซ่านม้วย
45+
ธรรมฤๅผ่อนรักษา ใจชั่ว โฉดแฮ
46+
สืบอยู่เต็มศึกด้วย ฝุ่นฟ้ากีฬา กามฦๅ ฯ
47+
๏ กตัญญูไป่พร้อม ปฐมฌาน
48+
เกมส๎วัฒน์ปฏิภาณ ห่อนล้ำ
49+
ทฤษฎีถ่อยๆ สังหาร เกณฑ์โทษ
50+
โกรธจี๊ดจ๋อยจ่มถ้ำ อยู่เฝ้า “อตฺตา” ๚ะ๛
51+
๑๒ กรกฎาคม ๒๕๕๘"""
52+
53+
from pythainlp.soundex import soundex
54+
from pythainlp.spell import correct, spell
55+
from pythainlp.tag import pos_tag
56+
from pythainlp.tokenize import (
57+
Tokenizer,
58+
sent_tokenize,
59+
subword_tokenize,
60+
word_tokenize,
61+
)
62+
from pythainlp.transliterate import romanize, transliterate
63+
from pythainlp.util import collate, thai_strftime

pythainlp/__main__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
32
# SPDX-FileType: SOURCE
43
# SPDX-License-Identifier: Apache-2.0

pythainlp/ancient/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
32
# SPDX-FileType: SOURCE
43
# SPDX-License-Identifier: Apache-2.0

pythainlp/ancient/aksonhan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# -*- coding: utf-8 -*-
21
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
32
# SPDX-FileType: SOURCE
43
# SPDX-License-Identifier: Apache-2.0
4+
from __future__ import annotations
55

66
from pythainlp import thai_consonants, thai_tonemarks
77
from pythainlp.corpus import thai_orst_words

pythainlp/ancient/currency.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# -*- coding: utf-8 -*-
21
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
32
# SPDX-FileType: SOURCE
43
# SPDX-License-Identifier: Apache-2.0
4+
from __future__ import annotations
5+
56

67
def convert_currency(value: float, from_unit: str) -> dict:
78
"""
@@ -44,14 +45,14 @@ def convert_currency(value: float, from_unit: str) -> dict:
4445
# }
4546
"""
4647
conversion_factors_to_att = {
47-
'เบี้ย': 1,
48-
'อัฐ': 100, # 1 อัฐ = 100 เบี้ย
49-
'ไพ': 2 * 100, # 1 ไพ = 2 อัฐ
50-
'เฟื้อง': 4 * 2 * 100, # 1 เฟื้อง = 4 ไพ
51-
'สลึง': 2 * 4 * 2 * 100, # 1 สลึง = 2 เฟื้อง
52-
'บาท': 4 * 2 * 4 * 2 * 100, # 1 บาท = 4 สลึง
53-
'ตำลึง': 4 * 4 * 2 * 4 * 2 * 100, # 1 ตำลึง = 4 บาท
54-
'ชั่ง': 20 * 4 * 4 * 2 * 4 * 2 * 100, # 1 ชั่ง = 20 ตำลึง
48+
"เบี้ย": 1,
49+
"อัฐ": 100, # 1 อัฐ = 100 เบี้ย
50+
"ไพ": 2 * 100, # 1 ไพ = 2 อัฐ
51+
"เฟื้อง": 4 * 2 * 100, # 1 เฟื้อง = 4 ไพ
52+
"สลึง": 2 * 4 * 2 * 100, # 1 สลึง = 2 เฟื้อง
53+
"บาท": 4 * 2 * 4 * 2 * 100, # 1 บาท = 4 สลึง
54+
"ตำลึง": 4 * 4 * 2 * 4 * 2 * 100, # 1 ตำลึง = 4 บาท
55+
"ชั่ง": 20 * 4 * 4 * 2 * 4 * 2 * 100, # 1 ชั่ง = 20 ตำลึง
5556
}
5657

5758
if from_unit not in conversion_factors_to_att:

pythainlp/augment/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
32
# SPDX-FileType: SOURCE
43
# SPDX-License-Identifier: Apache-2.0

pythainlp/augment/lm/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
32
# SPDX-FileType: SOURCE
43
# SPDX-License-Identifier: Apache-2.0

pythainlp/augment/lm/fasttext.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# -*- coding: utf-8 -*-
21
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
32
# SPDX-FileType: SOURCE
43
# SPDX-License-Identifier: Apache-2.0
4+
from __future__ import annotations
5+
56
import itertools
6-
from typing import List, Tuple
77

88
from gensim.models.fasttext import FastText as FastText_gensim
99
from gensim.models.keyedvectors import KeyedVectors
@@ -30,7 +30,7 @@ def __init__(self, model_path: str):
3030
self.model = FastText_gensim.load(model_path)
3131
self.dict_wv = list(self.model.key_to_index.keys())
3232

33-
def tokenize(self, text: str) -> List[str]:
33+
def tokenize(self, text: str) -> list[str]:
3434
"""
3535
Thai text tokenization for fastText
3636
@@ -41,7 +41,7 @@ def tokenize(self, text: str) -> List[str]:
4141
"""
4242
return word_tokenize(text, engine="icu")
4343

44-
def modify_sent(self, sent: str, p: float = 0.7) -> List[List[str]]:
44+
def modify_sent(self, sent: str, p: float = 0.7) -> list[list[str]]:
4545
"""
4646
:param str sent: text of sentence
4747
:param float p: probability
@@ -61,7 +61,7 @@ def modify_sent(self, sent: str, p: float = 0.7) -> List[List[str]]:
6161

6262
def augment(
6363
self, sentence: str, n_sent: int = 1, p: float = 0.7
64-
) -> List[Tuple[str]]:
64+
) -> list[tuple[str]]:
6565
"""
6666
Text Augment from fastText
6767

pythainlp/augment/lm/phayathaibert.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
# -*- coding: utf-8 -*-
21
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
32
# SPDX-FileType: SOURCE
43
# SPDX-License-Identifier: Apache-2.0
4+
from __future__ import annotations
55

66
import random
77
import re
8-
from typing import List
98

109
from pythainlp.phayathaibert.core import ThaiTextProcessor
1110

@@ -57,7 +56,7 @@ def generate(
5756

5857
def augment(
5958
self, text: str, num_augs: int = 3, sample: bool = False
60-
) -> List[str]:
59+
) -> list[str]:
6160
"""
6261
Text augmentation from PhayaThaiBERT
6362

0 commit comments

Comments
 (0)