From 9afd2e320290ff494919e161ef7819c7ce3c5e1e Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 11 Mar 2021 02:09:24 +0300 Subject: [PATCH] Refactored the process method of the Handler class --- tps/handler.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tps/handler.py b/tps/handler.py index ce12274..b2824ce 100644 --- a/tps/handler.py +++ b/tps/handler.py @@ -1,6 +1,7 @@ import re from collections import defaultdict -from typing import Union, Callable, Iterator, Tuple +from typing import Union, Callable, Iterator, Tuple, List +from functools import singledispatch from loguru import logger @@ -53,8 +54,8 @@ def __init__(self, charset: str, modules: list, out_max_length: int=None, save_s self.save_state = save_state - def process(self, string: str, cleaners: Tuple[Union[str, Callable[[str], str]]]=None, user_dict: dict=None, - **kwargs) -> str: + @singledispatch + def process(self, string: str, cleaners: str=None, user_dict: dict=None, **kwargs) -> str: """ Apply the user_dict, the chain of modules and some cleaners to the passed sentence. @@ -70,6 +71,7 @@ def process(self, string: str, cleaners: Tuple[Union[str, Callable[[str], str]]] origin_string = string cleaners = [] if cleaners is None else cleaners + cleaners = [cleaners] if not isinstance(cleaners, (tuple, list)) else cleaners for _cleaner in cleaners: if isinstance(_cleaner, Callable): cleaner = _cleaner @@ -95,6 +97,20 @@ def process(self, string: str, cleaners: Tuple[Union[str, Callable[[str], str]]] return string + @process.register + def _(self, string: str, cleaners: Callable[[str], str]=None, user_dict: dict=None, **kwargs) -> str: ... + + + @process.register + def _(self, string: str, cleaners: Tuple[Union[str, Callable[[str], str]]], user_dict: dict=None, + **kwargs) -> str: ... + + + @process.register + def _(self, string: str, cleaners: List[Union[str, Callable[[str], str]]], user_dict: dict=None, + **kwargs) -> str: ... + + def process_text(self, text: Union[str, list], cleaners: Tuple[Union[str, Callable[[str], str]]]=None, user_dict: dict=None, keep_delimiters: bool=True, **kwargs) -> Union[str, list]: """