Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[tool.ruff]
line-length = 79
indent-width = 4
target-version = "py310"
target-version = "py39"

[tool.ruff.format]
quote-style = "double"
Expand Down
127 changes: 63 additions & 64 deletions pythainlp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,63 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
__version__ = "5.2.0"

thai_consonants = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬอฮ" # 44 chars

thai_vowels = (
"\u0e24\u0e26\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35\u0e36\u0e37"
+ "\u0e38\u0e39\u0e40\u0e41\u0e42\u0e43\u0e44\u0e45\u0e4d\u0e47"
) # 20
thai_lead_vowels = "\u0e40\u0e41\u0e42\u0e43\u0e44" # 5
thai_follow_vowels = "\u0e30\u0e32\u0e33\u0e45" # 4
thai_above_vowels = "\u0e31\u0e34\u0e35\u0e36\u0e37\u0e4d\u0e47" # 7
thai_below_vowels = "\u0e38\u0e39" # 2

thai_tonemarks = "\u0e48\u0e49\u0e4a\u0e4b" # 4

# Paiyannoi, Maiyamok, Phinthu, Thanthakhat, Nikhahit, Yamakkan:
# These signs can be part of a word
thai_signs = "\u0e2f\u0e3a\u0e46\u0e4c\u0e4d\u0e4e" # 6 chars

# Any Thai character that can be part of a word
thai_letters = "".join(
[thai_consonants, thai_vowels, thai_tonemarks, thai_signs]
) # 74

# Fongman, Angkhankhu, Khomut:
# These characters are section markers
thai_punctuations = "\u0e4f\u0e5a\u0e5b" # 3 chars

thai_digits = "๐๑๒๓๔๕๖๗๘๙" # 10
thai_symbols = "\u0e3f" # Thai Bath ฿

# All Thai characters that are presented in Unicode
thai_characters = "".join(
[thai_letters, thai_punctuations, thai_digits, thai_symbols]
)
# Thai pangram by Sungsit Sawaiwan
# CC BY-SA License
# Source: https://fontuni.com/articles/2015-07-12-thai-poetgram.html
thai_pangram = """กีฬาบังลังก์ ฿๑,๒๓๔,๕๖๗,๘๙๐
๏ จับฅอคนบั่นต้อง อาญา
ขุดฆ่าโคตรฃัตติยา ซ่านม้วย
ธรรมฤๅผ่อนรักษา ใจชั่ว โฉดแฮ
สืบอยู่เต็มศึกด้วย ฝุ่นฟ้ากีฬา กามฦๅ ฯ
๏ กตัญญูไป่พร้อม ปฐมฌาน
เกมส๎วัฒน์ปฏิภาณ ห่อนล้ำ
ทฤษฎีถ่อยๆ สังหาร เกณฑ์โทษ
โกรธจี๊ดจ๋อยจ่มถ้ำ อยู่เฝ้า “อตฺตา” ๚ะ๛
๑๒ กรกฎาคม ๒๕๕๘"""

from pythainlp.soundex import soundex
from pythainlp.spell import correct, spell
from pythainlp.tag import pos_tag
from pythainlp.tokenize import (
Tokenizer,
sent_tokenize,
subword_tokenize,
word_tokenize,
)
from pythainlp.transliterate import romanize, transliterate
from pythainlp.util import collate, thai_strftime
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
__version__ = "5.2.0"

thai_consonants = "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬอฮ" # 44 chars

thai_vowels = (
"\u0e24\u0e26\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35\u0e36\u0e37"
+ "\u0e38\u0e39\u0e40\u0e41\u0e42\u0e43\u0e44\u0e45\u0e4d\u0e47"
) # 20
thai_lead_vowels = "\u0e40\u0e41\u0e42\u0e43\u0e44" # 5
thai_follow_vowels = "\u0e30\u0e32\u0e33\u0e45" # 4
thai_above_vowels = "\u0e31\u0e34\u0e35\u0e36\u0e37\u0e4d\u0e47" # 7
thai_below_vowels = "\u0e38\u0e39" # 2

thai_tonemarks = "\u0e48\u0e49\u0e4a\u0e4b" # 4

# Paiyannoi, Maiyamok, Phinthu, Thanthakhat, Nikhahit, Yamakkan:
# These signs can be part of a word
thai_signs = "\u0e2f\u0e3a\u0e46\u0e4c\u0e4d\u0e4e" # 6 chars

# Any Thai character that can be part of a word
thai_letters = "".join(
[thai_consonants, thai_vowels, thai_tonemarks, thai_signs]
) # 74

# Fongman, Angkhankhu, Khomut:
# These characters are section markers
thai_punctuations = "\u0e4f\u0e5a\u0e5b" # 3 chars

thai_digits = "๐๑๒๓๔๕๖๗๘๙" # 10
thai_symbols = "\u0e3f" # Thai Bath ฿

# All Thai characters that are presented in Unicode
thai_characters = "".join(
[thai_letters, thai_punctuations, thai_digits, thai_symbols]
)
# Thai pangram by Sungsit Sawaiwan
# CC BY-SA License
# Source: https://fontuni.com/articles/2015-07-12-thai-poetgram.html
thai_pangram = """กีฬาบังลังก์ ฿๑,๒๓๔,๕๖๗,๘๙๐
๏ จับฅอคนบั่นต้อง อาญา
ขุดฆ่าโคตรฃัตติยา ซ่านม้วย
ธรรมฤๅผ่อนรักษา ใจชั่ว โฉดแฮ
สืบอยู่เต็มศึกด้วย ฝุ่นฟ้ากีฬา กามฦๅ ฯ
๏ กตัญญูไป่พร้อม ปฐมฌาน
เกมส๎วัฒน์ปฏิภาณ ห่อนล้ำ
ทฤษฎีถ่อยๆ สังหาร เกณฑ์โทษ
โกรธจี๊ดจ๋อยจ่มถ้ำ อยู่เฝ้า “อตฺตา” ๚ะ๛
๑๒ กรกฎาคม ๒๕๕๘"""

from pythainlp.soundex import soundex
from pythainlp.spell import correct, spell
from pythainlp.tag import pos_tag
from pythainlp.tokenize import (
Tokenizer,
sent_tokenize,
subword_tokenize,
word_tokenize,
)
from pythainlp.transliterate import romanize, transliterate
from pythainlp.util import collate, thai_strftime
1 change: 0 additions & 1 deletion pythainlp/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
Expand Down
1 change: 0 additions & 1 deletion pythainlp/ancient/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
Expand Down
2 changes: 1 addition & 1 deletion pythainlp/ancient/aksonhan.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

from pythainlp import thai_consonants, thai_tonemarks
from pythainlp.corpus import thai_orst_words
Expand Down
19 changes: 10 additions & 9 deletions pythainlp/ancient/currency.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations


def convert_currency(value: float, from_unit: str) -> dict:
"""
Expand Down Expand Up @@ -44,14 +45,14 @@ def convert_currency(value: float, from_unit: str) -> dict:
# }
"""
conversion_factors_to_att = {
'เบี้ย': 1,
'อัฐ': 100, # 1 อัฐ = 100 เบี้ย
'ไพ': 2 * 100, # 1 ไพ = 2 อัฐ
'เฟื้อง': 4 * 2 * 100, # 1 เฟื้อง = 4 ไพ
'สลึง': 2 * 4 * 2 * 100, # 1 สลึง = 2 เฟื้อง
'บาท': 4 * 2 * 4 * 2 * 100, # 1 บาท = 4 สลึง
'ตำลึง': 4 * 4 * 2 * 4 * 2 * 100, # 1 ตำลึง = 4 บาท
'ชั่ง': 20 * 4 * 4 * 2 * 4 * 2 * 100, # 1 ชั่ง = 20 ตำลึง
"เบี้ย": 1,
"อัฐ": 100, # 1 อัฐ = 100 เบี้ย
"ไพ": 2 * 100, # 1 ไพ = 2 อัฐ
"เฟื้อง": 4 * 2 * 100, # 1 เฟื้อง = 4 ไพ
"สลึง": 2 * 4 * 2 * 100, # 1 สลึง = 2 เฟื้อง
"บาท": 4 * 2 * 4 * 2 * 100, # 1 บาท = 4 สลึง
"ตำลึง": 4 * 4 * 2 * 4 * 2 * 100, # 1 ตำลึง = 4 บาท
"ชั่ง": 20 * 4 * 4 * 2 * 4 * 2 * 100, # 1 ชั่ง = 20 ตำลึง
}

if from_unit not in conversion_factors_to_att:
Expand Down
1 change: 0 additions & 1 deletion pythainlp/augment/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
Expand Down
1 change: 0 additions & 1 deletion pythainlp/augment/lm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
Expand Down
10 changes: 5 additions & 5 deletions pythainlp/augment/lm/fasttext.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import itertools
from typing import List, Tuple

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

def tokenize(self, text: str) -> List[str]:
def tokenize(self, text: str) -> list[str]:
"""
Thai text tokenization for fastText

Expand All @@ -41,7 +41,7 @@ def tokenize(self, text: str) -> List[str]:
"""
return word_tokenize(text, engine="icu")

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

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

Expand Down
5 changes: 2 additions & 3 deletions pythainlp/augment/lm/phayathaibert.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import random
import re
from typing import List

from pythainlp.phayathaibert.core import ThaiTextProcessor

Expand Down Expand Up @@ -57,7 +56,7 @@ def generate(

def augment(
self, text: str, num_augs: int = 3, sample: bool = False
) -> List[str]:
) -> list[str]:
"""
Text augmentation from PhayaThaiBERT

Expand Down
6 changes: 2 additions & 4 deletions pythainlp/augment/lm/wangchanberta.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0

from typing import List
from __future__ import annotations

from transformers import (
CamembertTokenizer,
Expand Down Expand Up @@ -52,7 +50,7 @@ def generate(self, sentence: str, num_replace_tokens: int = 3):
masked_text = self.input_text
return self.sent2

def augment(self, sentence: str, num_replace_tokens: int = 3) -> List[str]:
def augment(self, sentence: str, num_replace_tokens: int = 3) -> list[str]:
"""
Text augmentation from WangchanBERTa

Expand Down
1 change: 0 additions & 1 deletion pythainlp/augment/word2vec/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
Expand Down
7 changes: 3 additions & 4 deletions pythainlp/augment/word2vec/bpemb_wv.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
from typing import List, Tuple
from __future__ import annotations

from pythainlp.augment.word2vec.core import Word2VecAug

Expand All @@ -22,7 +21,7 @@ def __init__(self, lang: str = "th", vs: int = 100000, dim: int = 300):
self.model = self.bpemb_temp.emb
self.load_w2v()

def tokenizer(self, text: str) -> List[str]:
def tokenizer(self, text: str) -> list[str]:
"""
:param str text: Thai text
:rtype: List[str]
Expand All @@ -39,7 +38,7 @@ def load_w2v(self):

def augment(
self, sentence: str, n_sent: int = 1, p: float = 0.7
) -> List[Tuple[str]]:
) -> list[tuple[str]]:
"""
Text Augment using word2vec from BPEmb

Expand Down
8 changes: 4 additions & 4 deletions pythainlp/augment/word2vec/core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import itertools
from typing import List, Tuple


class Word2VecAug:
Expand All @@ -28,7 +28,7 @@ def __init__(
self.model = model
self.dict_wv = list(self.model.key_to_index.keys())

def modify_sent(self, sent: str, p: float = 0.7) -> List[List[str]]:
def modify_sent(self, sent: str, p: float = 0.7) -> list[list[str]]:
"""
:param str sent: text of sentence
:param float p: probability
Expand All @@ -48,7 +48,7 @@ def modify_sent(self, sent: str, p: float = 0.7) -> List[List[str]]:

def augment(
self, sentence: str, n_sent: int = 1, p: float = 0.7
) -> List[Tuple[str]]:
) -> list[tuple[str]]:
"""
:param str sentence: text of sentence
:param int n_sent: maximum number of synonymous sentences
Expand Down
7 changes: 3 additions & 4 deletions pythainlp/augment/word2vec/ltw2v.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
from typing import List, Tuple
from __future__ import annotations

from pythainlp.augment.word2vec.core import Word2VecAug
from pythainlp.corpus import get_corpus_path
Expand All @@ -21,7 +20,7 @@ def __init__(self):
self.ltw2v_wv = get_corpus_path("ltw2v")
self.load_w2v()

def tokenizer(self, text: str) -> List[str]:
def tokenizer(self, text: str) -> list[str]:
"""
:param str text: Thai text
:rtype: List[str]
Expand All @@ -36,7 +35,7 @@ def load_w2v(self): # insert substitute

def augment(
self, sentence: str, n_sent: int = 1, p: float = 0.7
) -> List[Tuple[str]]:
) -> list[tuple[str]]:
"""
Text Augment using word2vec from Thai2Fit

Expand Down
7 changes: 3 additions & 4 deletions pythainlp/augment/word2vec/thai2fit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
from typing import List, Tuple
from __future__ import annotations

from pythainlp.augment.word2vec.core import Word2VecAug
from pythainlp.corpus import get_corpus_path
Expand All @@ -21,7 +20,7 @@ def __init__(self):
self.thai2fit_wv = get_corpus_path("thai2fit_wv")
self.load_w2v()

def tokenizer(self, text: str) -> List[str]:
def tokenizer(self, text: str) -> list[str]:
"""
:param str text: Thai text
:rtype: List[str]
Expand All @@ -37,7 +36,7 @@ def load_w2v(self):

def augment(
self, sentence: str, n_sent: int = 1, p: float = 0.7
) -> List[Tuple[str]]:
) -> list[tuple[str]]:
"""
Text Augment using word2vec from Thai2Fit

Expand Down
Loading
Loading