Skip to content
Merged
4 changes: 2 additions & 2 deletions pythainlp/augment/wordnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def find_synonyms(
:param Optional[str] pos: part-of-speech type. Default is None.
:param str postag_corpus: name of POS tag corpus
:return: list of synonyms
:rtype: List[str]
:rtype: list[str]
"""
self.synonyms = []
if pos is None:
Expand Down Expand Up @@ -171,7 +171,7 @@ def augment(
:param str postag_corpus: name of POS tag corpus

:return: list of synonyms
:rtype: List[Tuple[str]]
:rtype: list[list[str]]

:Example:
::
Expand Down
23 changes: 14 additions & 9 deletions pythainlp/benchmarks/word_tokenization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import re
import sys
from typing import Any

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -40,7 +41,7 @@ def _f1(precision: float, recall: float) -> float:
return 2 * precision * recall / (precision + recall)


def _flatten_result(my_dict: dict, sep: str = ":") -> dict:
def _flatten_result(my_dict: dict, sep: str = ":") -> dict[str, Any]:
"""Flatten two-dimension dictionary.

Use keys in the first dimension as a prefix for keys in the second dimension.
Expand All @@ -54,7 +55,7 @@ def _flatten_result(my_dict: dict, sep: str = ":") -> dict:
:param str sep: separator between the two keys (default: ":")

:return: a one-dimension dictionary with keys combined
:rtype: dict[str, Union[float, str]]
:rtype: dict[str, Any]
"""
return {
f"{k1}{sep}{k2}": v
Expand Down Expand Up @@ -129,7 +130,7 @@ def preprocessing(txt: str, remove_space: bool = True) -> str:
return txt


def compute_stats(ref_sample: str, raw_sample: str) -> dict:
def compute_stats(ref_sample: str, raw_sample: str) -> dict[str, Any]:
"""Compute statistics for tokenization quality

These statistics include:
Expand All @@ -146,7 +147,7 @@ def compute_stats(ref_sample: str, raw_sample: str) -> dict:
:param str samples: samples that we want to evaluate

:return: metrics at character- and word-level and indicators of correctly tokenized words
:rtype: dict[str, Union[float, str]]
:rtype: dict[str, Any]
"""
ref_sample_arr = _binary_representation(ref_sample)
sample_arr = _binary_representation(raw_sample)
Expand Down Expand Up @@ -222,7 +223,11 @@ def _binary_representation(txt: str, verbose: bool = False) -> np.ndarray:
sample_wo_seps = list(txt.replace(SEPARATOR, ""))

# sanity check
assert len(sample_wo_seps) == len(bin_rept)
if len(sample_wo_seps) != len(bin_rept):
raise ValueError(
f"Length mismatch: sample_wo_seps={len(sample_wo_seps)}, "
f"bin_rept={len(bin_rept)}"
)

if verbose:
for c, m in zip(sample_wo_seps, bin_rept):
Expand All @@ -231,13 +236,13 @@ def _binary_representation(txt: str, verbose: bool = False) -> np.ndarray:
return bin_rept


def _find_word_boundaries(bin_reps) -> list:
def _find_word_boundaries(bin_reps) -> list[tuple[int, int]]:
"""Find the starting and ending location of each word.

:param str bin_reps: binary representation of a text

:return: list of tuples (start, end)
:rtype: list[tuple(int, int)]
:rtype: list[tuple[int, int]]
"""
boundary = np.argwhere(bin_reps == 1).reshape(-1)
start_idx = boundary
Expand All @@ -252,8 +257,8 @@ def _find_words_correctly_tokenised(
) -> tuple[int, ...]:
"""Find whether each word is correctly tokenized.

:param list[tuple(int, int)] ref_boundaries: word boundaries of reference tokenization
:param list[tuple(int, int)] predicted_boundaries: word boundareies of predicted tokenization
:param list[tuple[int, int]] ref_boundaries: word boundaries of reference tokenization
:param list[tuple[int, int]] predicted_boundaries: word boundaries of predicted tokenization

:return: binary sequence where 1 indicates the corresponding word is tokenized correctly
:rtype: tuple[int, ...]
Expand Down
7 changes: 4 additions & 3 deletions pythainlp/cli/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ def __init__(self, name: str, argv: Sequence[str]) -> None:
actual = _read_file(args.input_file)
expected = _read_file(args.test_file)

assert len(actual) == len(expected), (
"Input and test files do not have the same number of samples"
)
if len(actual) != len(expected):
raise ValueError(
"Input and test files do not have the same number of samples"
)

safe_print(
"Benchmarking %s against %s with %d samples in total"
Expand Down
4 changes: 2 additions & 2 deletions pythainlp/coref/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def coreference_resolution(
) -> list[dict]:
"""Coreference Resolution

:param List[str] texts: list of texts to apply coreference resolution to
:param Union[str, list[str]] texts: list of texts to apply coreference resolution to
:param str model_name: coreference resolution model
:param str device: device for running coreference resolution model on\
("cpu", "cuda", and others)
:return: List of texts with coreference resolution
:rtype: List[dict]
:rtype: list[dict]

:Options for model_name:
* *han-coref-v1.0* - (default) Han-Coref: Thai coreference resolution\
Expand Down
4 changes: 2 additions & 2 deletions pythainlp/el/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def get_el(
) -> Union[list[dict], str]:
"""Get Entity Linking from Thai Text

:param str Union[List[str], str]: list of Thai text or text
:param str Union[list[str], str]: list of Thai text or text
:return: list of entity linking
:rtype: Union[List[dict], str]
:rtype: Union[list[dict], str]

:Example:
::
Expand Down
6 changes: 3 additions & 3 deletions pythainlp/generate/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def gen_sentence(
:param bool duplicate: allow duplicate words in sentence

:return: list of words or a word string
:rtype: List[str], str
:rtype: list[str], str

:Example:
::
Expand Down Expand Up @@ -153,7 +153,7 @@ def gen_sentence(
:param bool duplicate: allow duplicate words in sentence

:return: list of words or a word string
:rtype: List[str], str
:rtype: list[str], str

:Example:
::
Expand Down Expand Up @@ -244,7 +244,7 @@ def gen_sentence(
:param bool duplicate: allow duplicate words in sentence

:return: list of words or a word string
:rtype: List[str], str
:rtype: list[str], str

:Example:
::
Expand Down
2 changes: 1 addition & 1 deletion pythainlp/generate/thai2fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def gen_sentence(
:param bool duplicate: allow duplicate words in sentence

:return: list words or str words
:rtype: List[str], str
:rtype: list[str], str

:Example:
::
Expand Down
Loading
Loading