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
4 changes: 2 additions & 2 deletions pythainlp/augment/wordnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ def __init__(self):
pass

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

:param str word: word
:param str pos: part-of-speech type
:param str | None pos: part-of-speech type. Default is None.
:param str postag_corpus: name of POS tag corpus
:return: list of synonyms
:rtype: List[str]
Expand Down
25 changes: 15 additions & 10 deletions pythainlp/classify/param_free.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
class GzipModel:
"""
This class is a re-implementation of
“Low-Resource” Text Classification: A Parameter-Free Classification Method with Compressors
(Jiang et al., Findings 2023)
“Low-Resource” Text Classification: A Parameter-Free Classification Method
with Compressors (Jiang et al., Findings 2023)

:param list training_data: list [(text_sample,label)]
:param str model_path: Path for loading model (if you saved the model)
:param list | None training_data: list [(text_sample,label)].
Default is None.
:param str model_path: Path for loading model (if you saved the model).
Default is empty string.
"""

def __init__(
self,
training_data: list[tuple[str, str]] = None,
model_path: str = None,
training_data: list[tuple[str, str]] | None = None,
model_path: str = "",
):
if model_path is not None:
if model_path:
self.load(model_path)
else:
self.training_data = np.array(training_data)
Expand Down Expand Up @@ -85,9 +87,9 @@ def predict(self, x1: str, k: int = 1) -> str:

def save(self, path: str):
"""
:param str path: path for save model
:param str path: path to save model
"""
with open(path, "w") as f:
with open(path, "w", encoding="utf-8") as f:
json.dump(
{
"training_data": self.training_data.tolist(),
Expand All @@ -98,7 +100,10 @@ def save(self, path: str):
)

def load(self, path: str):
with open(path) as f:
"""
:param str path: path to load model
"""
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
self.Cx2_list = data["Cx2_list"]
self.training_data = np.array(data["training_data"])
7 changes: 4 additions & 3 deletions pythainlp/corpus/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,12 +656,13 @@ def make_safe_directory_name(name: str) -> str:
return safe_name


def get_hf_hub(repo_id: str, filename: str = None) -> str:
def get_hf_hub(repo_id: str, filename: str = "") -> str:
"""
HuggingFace Hub in :mod:`pythainlp` data directory.

:param str repo_id: repo_id
:param str filename: filename
:param str filename: filename (optional, default is empty string).
If empty, downloads entire snapshot.
:return: path
:rtype: str
"""
Expand All @@ -677,7 +678,7 @@ def get_hf_hub(repo_id: str, filename: str = None) -> str:
hf_root = get_full_data_path("hf_models")
name_dir = make_safe_directory_name(repo_id)
root_project = os.path.join(hf_root, name_dir)
if filename is not None:
if filename:
output_path = hf_hub_download(
repo_id=repo_id, filename=filename, local_dir=root_project
)
Expand Down
25 changes: 13 additions & 12 deletions pythainlp/corpus/wordnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
from nltk.corpus import wordnet


def synsets(word: str, pos: str = None, lang: str = "tha"):
def synsets(word: str, pos: str | None = None, lang: str = "tha"):
"""
This function returns the synonym set for all lemmas of the given word
with an optional argument to constrain the part of speech of the word.

:param str word: word to find synsets of
:param str pos: constraint of the part of speech (i.e. *n* for Noun, *v*
:param str | None pos: constraint of the part of speech (i.e. *n* for Noun, *v*
for Verb, *a* for Adjective, *s* for Adjective
satellites, and *r* for Adverb)
satellites, and *r* for Adverb). Default is None.
:param str lang: abbreviation of language (i.e. *eng*, *tha*).
By default, it is *tha*

Expand Down Expand Up @@ -101,13 +101,13 @@ def synset(name_synsets):
return wordnet.synset(name_synsets)


def all_lemma_names(pos: str = None, lang: str = "tha"):
def all_lemma_names(pos: str | None = None, lang: str = "tha"):
"""
This function returns all lemma names for all synsets of the given
part of speech tag and language. If part of speech tag is not
specified, all synsets of all parts of speech will be used.

:param str pos: constraint of the part of speech (i.e. *n* for Noun,
:param str | None pos: constraint of the part of speech (i.e. *n* for Noun,
*v* for Verb, *a* for Adjective, *s* for
Adjective satellites, and *r* for Adverb).
By default, *pos* is **None**.
Expand Down Expand Up @@ -144,12 +144,12 @@ def all_lemma_names(pos: str = None, lang: str = "tha"):
return wordnet.all_lemma_names(pos=pos, lang=lang)


def all_synsets(pos: str = None):
def all_synsets(pos: str | None = None):
"""
This function iterates over all synsets constrained by the given
part of speech tag.

:param str pos: part of speech tag
:param str | None pos: part of speech tag. Default is None.

:return: list of synsets constrained by the given part of speech tag.
:rtype: Iterable[:class:`Synset`]
Expand Down Expand Up @@ -194,15 +194,15 @@ def langs():
return wordnet.langs()


def lemmas(word: str, pos: str = None, lang: str = "tha"):
def lemmas(word: str, pos: str | None = None, lang: str = "tha"):
"""
This function returns all lemmas given the word with an optional
argument to constrain the part of speech of the word.

:param str word: word to find lemmas of
:param str pos: constraint of the part of speech (i.e. *n* for Noun,
:param str | None pos: constraint of the part of speech (i.e. *n* for Noun,
*v* for Verb, *a* for Adjective, *s* for
Adjective satellites, and *r* for Adverb)
Adjective satellites, and *r* for Adverb). Default is None.
:param str lang: abbreviation of language (i.e. *eng*, *tha*).
By default, it is *tha*.

Expand Down Expand Up @@ -403,13 +403,14 @@ def wup_similarity(synsets1, synsets2):
return wordnet.wup_similarity(synsets1, synsets2)


def morphy(form, pos: str = None):
def morphy(form, pos: str | None = None):
"""
This function finds a possible base form for the given form,
with the given part of speech.

:param str form: the form to finds the base form of
:param str pos: part of speech tag of words to be searched
:param str | None pos: part of speech tag of words to be searched.
Default is None.

:return: base form of the given form
:rtype: str
Expand Down
6 changes: 3 additions & 3 deletions pythainlp/generate/wangchanglm.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def gen_instruct(
def instruct_generate(
self,
instruct: str,
context: str = None,
context: str = "",
max_new_tokens=512,
temperature: float = 0.9,
top_p: float = 0.95,
Expand All @@ -140,7 +140,7 @@ def instruct_generate(
Generate Instruct

:param str instruct: Instruct
:param str context: context
:param str context: context (optional, default is empty string)
:param int max_new_tokens: maximum number of new tokens
:param float top_p: top p
:param float temperature: temperature
Expand Down Expand Up @@ -172,7 +172,7 @@ def instruct_generate(
# และเครื่องดื่มแอลกอฮอล์

"""
if context in (None, ""):
if not context:
prompt = self.PROMPT_DICT["prompt_no_input"].format_map(
{"instruction": instruct, "input": ""}
)
Expand Down
21 changes: 19 additions & 2 deletions pythainlp/summarize/mt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,27 @@ def __init__(
min_length: int = 30,
max_length: int = 100,
skip_special_tokens: bool = True,
pretrained_mt5_model_name: str = None,
pretrained_mt5_model_name: str = "",
):
"""
Initialize mT5 Summarizer.

:param str model_size: Size of the model ("small", "base", "large",
"xl", "xxl"). Default is "small".
:param int num_beams: Number of beams for beam search. Default is 4.
:param int no_repeat_ngram_size: Size of n-grams to avoid repeating.
Default is 2.
:param int min_length: Minimum length of generated summary.
Default is 30.
:param int max_length: Maximum length of generated summary.
Default is 100.
:param bool skip_special_tokens: Whether to skip special tokens in
output. Default is True.
:param str pretrained_mt5_model_name: Name of pretrained model.
If empty (default), uses google/mt5-{model_size}.
"""
model_name = ""
if pretrained_mt5_model_name is None:
if not pretrained_mt5_model_name:
if model_size not in ["small", "base", "large", "xl", "xxl"]:
raise ValueError(
f"""model_size \"{model_size}\" not found.
Expand Down
16 changes: 8 additions & 8 deletions pythainlp/transliterate/wunsen.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ def transliterate(
self,
text: str,
lang: str,
jp_input: str = None,
zh_sandhi: bool = None,
system: str = None,
jp_input: str | None = None,
zh_sandhi: bool | None = None,
system: str | None = None,
):
"""
Use Wunsen for transliteration

:param str text: text to be transliterated to Thai text.
:param str lang: source language
:param str jp_input: Japanese input method (for Japanese only)
:param bool zh_sandhi: Mandarin third tone sandhi option
(for Mandarin only)
:param str system: transliteration system (for Japanese and
Mandarin only)
:param str | None jp_input: Japanese input method (for Japanese only). Default is None.
:param bool | None zh_sandhi: Mandarin third tone sandhi option
(for Mandarin only). Default is None.
:param str | None system: transliteration system (for Japanese and
Mandarin only). Default is None.

:return: Thai text
:rtype: str
Expand Down
4 changes: 2 additions & 2 deletions pythainlp/util/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def thai_strptime(
text: str,
fmt: str,
year: str = "be",
add_year: int = None,
add_year: int | None = None,
tzinfo=ZoneInfo("Asia/Bangkok"),
):
"""
Expand All @@ -211,7 +211,7 @@ def thai_strptime(
:param str fmt: string containing date and time directives
:param str year: year of the text \
(ad is Anno Domini and be is Buddhist Era)
:param int add_year: add to year when converting to ad
:param int | None add_year: add to year when converting to ad. Default is None.
:param object tzinfo: tzinfo (default is Asia/Bangkok)
:return: The year that is converted to datetime.datetime
:rtype: datetime.datetime
Expand Down
Loading