Skip to content

Commit

Permalink
Refactored the process method of the Handler class
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekakhrone committed Mar 10, 2021
1 parent 76d0714 commit 9afd2e3
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions tps/handler.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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]:
"""
Expand Down

0 comments on commit 9afd2e3

Please sign in to comment.