diff --git a/deep_utils/__init__.py b/deep_utils/__init__.py index 122083e8..1be52ca8 100644 --- a/deep_utils/__init__.py +++ b/deep_utils/__init__.py @@ -4,7 +4,7 @@ from .utils.lib_utils.integeration_utils import import_lazy_module # Deep Utils version number -__version__ = "1.3.40" +__version__ = "1.3.41" from .utils.constants import DUMMY_PATH, Backends @@ -35,6 +35,7 @@ "utils.str_utils.str_utils": ["StringUtils"], "medical.nnunet_utils.nnunet_utils": ["NNUnetUtils"], "utils.encodes.b64": ["BinaryUtils"], + "nlp.utils.persian.utils": ["PersianUtils"], DUMMY_PATH: [], # this is required for dummy check! } @@ -79,7 +80,6 @@ import_lazy_module("AIOHttpRequests", "utils.requests_utils.requests_utils") import_lazy_module("RequestsUtils", "utils.requests_utils.requests_utils") - if TYPE_CHECKING: from utils.numpy_utils.numpy_utils import NumpyUtils from .utils.box_utils.boxes import Box, Point diff --git a/deep_utils/nlp/utils/persian/utils.py b/deep_utils/nlp/utils/persian/utils.py index 10b622bf..c72ef6a6 100644 --- a/deep_utils/nlp/utils/persian/utils.py +++ b/deep_utils/nlp/utils/persian/utils.py @@ -2,89 +2,93 @@ from deep_utils.utils.re_utils.re_utils import REUtils -def persian_num2english(input_string: str, reverse: bool = False): - """ - Converts persian numbers to english - Args: - input_string: - reverse: If set to True, converts english 2 persian! +class PersianUtils: - Returns: + @staticmethod + def persian_num2english(input_string: str, reverse: bool = False): + """ + Converts persian numbers to english + Args: + input_string: + reverse: If set to True, converts english 2 persian! - """ - NUM_MAP = { - "۱": "1", - "۲": "2", - "۳": "3", - "۴": "4", - "۵": "5", - "۶": "6", - "۷": "7", - "۸": "8", - "۹": "9", - "۰": "0", - } - if reverse: - NUM_MAP = {v: k for k, v in NUM_MAP.items()} - output_string = "".join([NUM_MAP.get(c, c) for c in input_string]) - return output_string + Returns: + """ + NUM_MAP = { + "۱": "1", + "۲": "2", + "۳": "3", + "۴": "4", + "۵": "5", + "۶": "6", + "۷": "7", + "۸": "8", + "۹": "9", + "۰": "0", + } + if reverse: + NUM_MAP = {v: k for k, v in NUM_MAP.items()} + output_string = "".join([NUM_MAP.get(c, c) for c in input_string]) + return output_string -def arabic_char2fa_char(input_string: str): - arabic2persian = { - "ك": "ک", - "ي": "ی", - } - out_string = "".join(arabic2persian.get(s, s) for s in input_string) - return out_string + @staticmethod + def arabic_char2fa_char(input_string: str): + arabic2persian = { + "ك": "ک", + "ي": "ی", + } + out_string = "".join(arabic2persian.get(s, s) for s in input_string) + return out_string + @staticmethod + def num2fa_spoken(num_string: Union[str, int], split_num_char: bool = False): + """ + converting string number to the spoken format. This function converts both persian and english workds to persian + spoken words + :param num_string: + :param split_num_char: if set to True, splits connected numbers and characters + :return: + >>> num2fa_spoken("30") + 'سی ام' + >>> num2fa_spoken("21") + 'بیست و یکم' + >>> num2fa_spoken("۳۲") + 'سی و دوم' + >>> num2fa_spoken(2) + 'دوم' + >>> num2fa_spoken("204غربی", split_num_char=True) + 'دویست و چهارم غربی' + """ -def num2fa_spoken(num_string: Union[str, int], split_num_char: bool = False): - """ - converting string number to the spoken format. This function converts both persian and english workds to persian - spoken words - :param num_string: - :param split_num_char: if set to True, splits connected numbers and characters - :return: - >>> num2fa_spoken("30") - 'سی ام' - >>> num2fa_spoken("21") - 'بیست و یکم' - >>> num2fa_spoken("۳۲") - 'سی و دوم' - >>> num2fa_spoken(2) - 'دوم' - >>> num2fa_spoken("204غربی", split_num_char=True) - 'دویست و چهارم غربی' - """ + num_string = str(num_string) + if split_num_char: + num_string = REUtils.split_char_number(num_string) + num_string = " ".join([PersianUtils._num2fa_spoken(word) for word in num_string.split(" ")]) + return num_string - num_string = str(num_string) - if split_num_char: - num_string = REUtils.split_char_number(num_string) - num_string = " ".join([_num2fa_spoken(word) for word in num_string.split(" ")]) - return num_string - - -def _num2fa_spoken(num_string): - from num2fawords import words - if num_string.isdigit(): - spoken_num = words(num_string) - if spoken_num[-2:] == "سه": - spoken_num = spoken_num[:-2] + "سوم" - elif spoken_num[-1] in ["ی", ]: - spoken_num += " ام" + @staticmethod + def _num2fa_spoken(num_string): + from num2fawords import words + if num_string.isdigit(): + spoken_num = words(num_string) + if spoken_num[-2:] == "سه": + spoken_num = spoken_num[:-2] + "سوم" + elif spoken_num[-1] in ["ی", ]: + spoken_num += " ام" + else: + spoken_num += "م" else: - spoken_num += "م" - else: - spoken_num = num_string - return spoken_num - + spoken_num = num_string + return spoken_num -def num2fa_spoken_sentence(sentence: str, split_num_char=False) -> str: - """ - Applies num2fa_spoken on a sentence of words! - :param sentence: - :param split_num_char: if set to True, splits connected numbers and characters - :return: - """ - return " ".join([num2fa_spoken(word, split_num_char=split_num_char) for word in sentence.split(" ")]) + @staticmethod + def num2fa_spoken_sentence(sentence: str, split_num_char=False) -> str: + """ + Applies num2fa_spoken on a sentence of words! + :param sentence: + :param split_num_char: if set to True, splits connected numbers and characters + :return: + """ + return " ".join( + [PersianUtils.num2fa_spoken(word, split_num_char=split_num_char) for word in sentence.split(" ")]) diff --git a/deep_utils/utils/py_utils/py_utils.py b/deep_utils/utils/py_utils/py_utils.py index c34776f1..da45a25e 100644 --- a/deep_utils/utils/py_utils/py_utils.py +++ b/deep_utils/utils/py_utils/py_utils.py @@ -1,7 +1,8 @@ import inspect from argparse import Namespace from re import sub -from typing import Union, Optional +from typing import Optional +from typing import Union from deep_utils.utils.logging_utils.logging_utils import log_print @@ -110,6 +111,62 @@ def print_args(args: Optional[Union[dict, Namespace]] = None, show_file=True, sh s = (f'{file}: ' if show_file else '') + (f'{func}: ' if show_func else '') log_print(None, message=PyUtils.color_str(s + ', '.join(f'{k}={v}' for k, v in args.items()))) + @staticmethod + def get_obj_variables(obj) -> dict: + """ + Gets the attributes of an object not the methods. + :param obj: + :return: + """ + out = dict() + for key in dir(obj): + val = getattr(obj, key) + if (key.startswith("__") and key.endswith("__")) or type(val).__name__ == "method": + continue + else: + out[key] = val + return out + + @staticmethod + def get_attributes(obj) -> dict: + """ + Gets the attributes of an object not the methods. Deprecated: The naming was not appropriate, make sure to use + `get_variables` instead. + :param obj: + :return: + """ + return PyUtils.get_obj_variables(obj) + + @staticmethod + def update_obj_params(obj_instance: object, args: Union[dict, Namespace]): + """ + This func/method is used for updating parameters of a class especially a config class with input arguments + :param obj_instance: + :param args: + :return: + """ + if isinstance(args, Namespace): + variables = vars(args) + elif isinstance(args, dict): + variables = args + else: + raise ValueError() + for k, v in variables.items(): + if hasattr(obj_instance, k): + setattr(obj_instance, k, v) + else: + raise ValueError(f"value {k} is not defined in {obj_instance.__class__.__name__}") + + @staticmethod + def variable_repr(self): + """ + A representation func for objects + :param self: + :return: + """ + variables = PyUtils.get_obj_variables(self) + return f"{self.__class__.__name__} -> " + ", ".join(f"{k}: {v}" for k, v in variables.items()) + if __name__ == '__main__': PyUtils.print("pooya is walking", color="yellow") diff --git a/setup.py b/setup.py index 467977b1..0fc63103 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ import setuptools -VERSION = "1.3.40" +VERSION = "1.3.41" long_description = open("Readme.md", mode="r", encoding="utf-8").read()