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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ module = [
"torch.*",
"tqdm.*",
"transformers.*",
"ufal.*",
"ufal.chu_liu_edmonds.*",
"word2word.*",
"wtpsplit.*",
Expand Down
4 changes: 2 additions & 2 deletions pythainlp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
thai_above_vowels = "\u0e31\u0e34\u0e35\u0e36\u0e37\u0e4d\u0e47" # 7
thai_below_vowels = "\u0e38\u0e39" # 2

thai_tonemarks = "\u0e48\u0e49\u0e4a\u0e4b" # 4
thai_tonemarks: str = "\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_letters: str = "".join(
[thai_consonants, thai_vowels, thai_tonemarks, thai_signs]
) # 74

Expand Down
3 changes: 1 addition & 2 deletions pythainlp/ancient/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Ancient versions of the Thai language
"""
"""Ancient versions of the Thai language"""

__all__ = ["aksonhan_to_current", "convert_currency"]

Expand Down
3 changes: 1 addition & 2 deletions pythainlp/augment/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Thai text augment
"""
"""Thai text augment"""

__all__ = ["WordNetAug"]

Expand Down
3 changes: 1 addition & 2 deletions pythainlp/augment/lm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Language Models
"""
"""Language Models"""

__all__ = [
"FastTextAug",
Expand Down
9 changes: 4 additions & 5 deletions pythainlp/augment/lm/fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class FastTextAug:
"""

def __init__(self, model_path: str):
""":param str model_path: path of model file
"""
""":param str model_path: path of model file"""
from gensim.models.fasttext import FastText as FastText_gensim
from gensim.models.keyedvectors import KeyedVectors

Expand All @@ -38,8 +37,8 @@ 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]]:
""":param str sent: text of sentence
def modify_sent(self, sent: list[str], p: float = 0.7) -> list[list[str]]:
""":param list[str] sent: text of sentence
:param float p: probability
:rtype: List[List[str]]
"""
Expand All @@ -57,7 +56,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

You may want to download the Thai model
Expand Down
12 changes: 9 additions & 3 deletions pythainlp/augment/lm/phayathaibert.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def __init__(self) -> None:
)

self.tokenizer = AutoTokenizer.from_pretrained(_MODEL_NAME)
self.model_for_masked_lm = AutoModelForMaskedLM.from_pretrained(_MODEL_NAME)
self.model_for_masked_lm = AutoModelForMaskedLM.from_pretrained(
_MODEL_NAME
)
self.model = pipeline(
"fill-mask",
tokenizer=self.tokenizer,
Expand Down Expand Up @@ -53,7 +55,9 @@ def generate(

return gen_txt

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

:param str text: Thai text
Expand Down Expand Up @@ -87,7 +91,9 @@ def augment(self, text: str, num_augs: int = 3, sample: bool = False) -> list[st
if num_augs <= MAX_NUM_AUGS:
for rank in range(num_augs):
gen_text = self.generate(text, rank, sample=sample)
processed_text = re.sub("<_>", " ", self.processor.preprocess(gen_text))
processed_text = re.sub(
"<_>", " ", self.processor.preprocess(gen_text)
)
augment_list.append(processed_text)
else:
raise ValueError(
Expand Down
3 changes: 1 addition & 2 deletions pythainlp/augment/word2vec/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Word2Vec
"""
"""Word2Vec"""

__all__ = ["Word2VecAug", "Thai2fitAug", "LTW2VAug"]

Expand Down
3 changes: 1 addition & 2 deletions pythainlp/augment/word2vec/bpemb_wv.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def tokenizer(self, text: str) -> list[str]:
return self.bpemb_temp.encode(text) # type: ignore[no-any-return]

def load_w2v(self):
"""Load BPEmb model
"""
"""Load BPEmb model"""
self.aug = Word2VecAug(
self.model, tokenize=self.tokenizer, type="model"
)
Expand Down
5 changes: 4 additions & 1 deletion pythainlp/augment/word2vec/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

class Word2VecAug:
def __init__(
self, model: str, tokenize: Callable[[str], list[str]], type: str = "file"
self,
model: str,
tokenize: Callable[[str], list[str]],
type: str = "file",
) -> None:
""":param str model: path of model
:param Callable[[str], list[str]] tokenize: tokenize function
Expand Down
10 changes: 7 additions & 3 deletions pythainlp/augment/word2vec/ltw2v.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ def tokenizer(self, text: str) -> list[str]:
return word_tokenize(text, engine="newmm")

def load_w2v(self): # insert substitute
"""Load LTW2V's word2vec model
"""
"""Load LTW2V's word2vec model"""
if self.ltw2v_wv is None:
raise ValueError(
"LTW2V word2vec model not found. "
"Please download it first using pythainlp.corpus.download('ltw2v_wv')"
)
self.aug = Word2VecAug(self.ltw2v_wv, self.tokenizer, type="binary")

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

:param str sentence: Thai sentence
Expand Down
12 changes: 8 additions & 4 deletions pythainlp/augment/word2vec/thai2fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ def tokenizer(self, text: str) -> list[str]:
:rtype: List[str]
"""
tok = thai2fit_tokenizer()
return tok.word_tokenize(text)
return tok.word_tokenize(text) # type: ignore[no-any-return]

def load_w2v(self):
"""Load Thai2Fit's word2vec model
"""
"""Load Thai2Fit's word2vec model"""
if self.thai2fit_wv is None:
raise ValueError(
"Thai2Fit word2vec model not found. "
"Please download it first using pythainlp.corpus.download('thai2fit_wv')"
)
self.aug = Word2VecAug(self.thai2fit_wv, self.tokenizer, type="binary")

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

:param str sentence: Thai sentence
Expand Down
17 changes: 9 additions & 8 deletions pythainlp/augment/wordnet.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Thank https://dev.to/ton_ami/text-data-augmentation-synonym-replacement-4h8l
"""
"""Thank https://dev.to/ton_ami/text-data-augmentation-synonym-replacement-4h8l"""

from __future__ import annotations

Expand All @@ -13,7 +12,7 @@

import itertools
from collections import OrderedDict
from typing import Optional
from typing import Callable, Optional

from nltk.corpus import wordnet as wn

Expand Down Expand Up @@ -117,14 +116,16 @@ def postype2wordnet(pos: str, corpus: str):


class WordNetAug:
"""Text Augment using wordnet
"""
"""Text Augment using wordnet"""

def __init__(self):
pass

def find_synonyms(
self, word: str, pos: Optional[str] = None, postag_corpus: str = "orchid"
self,
word: str,
pos: Optional[str] = None,
postag_corpus: str = "orchid",
) -> list[str]:
"""Find synonyms using wordnet

Expand Down Expand Up @@ -156,7 +157,7 @@ def find_synonyms(
def augment(
self,
sentence: str,
tokenize: object = word_tokenize,
tokenize: Callable[[str], list[str]] = word_tokenize,
max_syn_sent: int = 6,
postag: bool = True,
postag_corpus: str = "orchid",
Expand Down Expand Up @@ -210,5 +211,5 @@ def augment(
if max_syn_sent > self.p_all:
max_syn_sent = self.p_all
for x in list(itertools.product(*self.list_synonym))[0:max_syn_sent]:
new_sentences.append(x)
new_sentences.append(list(x))
return new_sentences
3 changes: 1 addition & 2 deletions pythainlp/benchmarks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Performance benchmarking.
"""
"""Performance benchmarking."""

__all__ = ["benchmark"]

Expand Down
5 changes: 4 additions & 1 deletion pythainlp/benchmarks/word_tokenization.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ def compute_stats(ref_sample: str, raw_sample: str) -> dict:
sample_arr = _binary_representation(raw_sample)

# Compute character-level statistics
c_pos_pred, c_neg_pred = np.argwhere(sample_arr == 1), np.argwhere(sample_arr == 0)
c_pos_pred, c_neg_pred = (
np.argwhere(sample_arr == 1),
np.argwhere(sample_arr == 0),
)

c_pos_pred = c_pos_pred[c_pos_pred < ref_sample_arr.shape[0]]
c_neg_pred = c_neg_pred[c_neg_pred < ref_sample_arr.shape[0]]
Expand Down
3 changes: 1 addition & 2 deletions pythainlp/chat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""pythainlp.chat
"""
"""pythainlp.chat"""

__all__ = ["ChatBotModel"]

Expand Down
8 changes: 3 additions & 5 deletions pythainlp/chat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

class ChatBotModel:
def __init__(self):
"""Chat using AI generation
"""
"""Chat using AI generation"""
self.history = []

def reset_chat(self):
"""Reset chat by cleaning history
"""
"""Reset chat by cleaning history"""
self.history = []

def load_model(
Expand Down Expand Up @@ -88,4 +86,4 @@ def chat(self, text: str) -> str:
)
_bot = self.model.gen_instruct(_temp)
self.history.append((text, _bot))
return _bot
return _bot # type: ignore[no-any-return]
3 changes: 1 addition & 2 deletions pythainlp/classify/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""pythainlp.classify
"""
"""pythainlp.classify"""

__all__ = ["GzipModel"]

Expand Down
3 changes: 1 addition & 2 deletions pythainlp/cli/data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Command line for PyThaiNLP's dataset/corpus management.
"""
"""Command line for PyThaiNLP's dataset/corpus management."""

from __future__ import annotations

Expand Down
3 changes: 1 addition & 2 deletions pythainlp/cli/tag.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Command line for PyThaiNLP's taggers.
"""
"""Command line for PyThaiNLP's taggers."""

from __future__ import annotations

Expand Down
4 changes: 2 additions & 2 deletions pythainlp/cli/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import annotations

import argparse
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from pythainlp import cli
from pythainlp.tokenize import (
Expand All @@ -31,7 +31,7 @@
class SubAppBase:
separator: str
algorithm: str
run: Callable[..., list[str]]
run: Callable[..., Any]

def __init__(self, name: str, argv: Sequence[str]) -> None:
parser = argparse.ArgumentParser(**cli.make_usage("tokenize " + name)) # type: ignore[arg-type]
Expand Down
3 changes: 1 addition & 2 deletions pythainlp/coref/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""PyThaiNLP Coreference Resolution
"""
"""PyThaiNLP Coreference Resolution"""

__all__ = ["coreference_resolution"]

Expand Down
6 changes: 5 additions & 1 deletion pythainlp/coref/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

from typing import Union

_MODEL = None


def coreference_resolution(
texts: list[str], model_name: str = "han-coref-v1.0", device: str = "cpu"
texts: Union[str, list[str]],
model_name: str = "han-coref-v1.0",
device: str = "cpu",
) -> list[dict]:
"""Coreference Resolution

Expand Down
Loading
Loading