Files

54 lines
1.9 KiB
Python
Raw Permalink Normal View History

2025-07-10 15:08:20 +08:00
from .Config import *
2025-07-09 17:39:50 +08:00
def LimitStringLength(data, max_length:int=50) -> str:
2025-07-11 01:36:10 +08:00
s:str = data if isinstance(data, str) else str(data)
2025-07-09 17:39:50 +08:00
if len(s) <= max_length:
return s
else:
inside_str = "\n...\n...\n"
# 计算头尾部分的长度
head_length = max_length // 2
tail_length = max_length - head_length - len(inside_str) # 3 是省略号的长度
# 截取头尾部分并连接
return s[:head_length] + inside_str + s[-tail_length:]
def FillString(data:Any,
max_length: int = 50,
fill_char: str = " ",
side: Literal["left", "right", "center"] = "right"
) -> str:
2025-07-11 01:36:10 +08:00
s:str = data if isinstance(data, str) else str(data)
2025-07-09 17:39:50 +08:00
char = fill_char[0]
if len(s) >= max_length:
return s
else:
if side == "left":
return s + char * (max_length - len(s))
elif side == "right":
return char * (max_length - len(s)) + s
elif side == "center":
left = (max_length - len(s)) // 2
right = max_length - len(s) - left
return char * left + s + char * right
else:
raise ValueError(f"Unsupported side: {side}")
def Bytes2Strings(lines:List[bytes], encoding='utf-8') -> List[str]:
return [line.decode(encoding) for line in lines]
def Bytes2String(lines:List[bytes], encoding='utf-8') -> str:
return "".join(Bytes2Strings(lines, encoding))
def word_segmentation(
2025-07-10 15:08:20 +08:00
sentence,
2025-07-09 17:39:50 +08:00
cut_all: bool = False,
HMM: bool = True,
use_paddle: bool = False
) -> Sequence[Optional[Union[Any, str]]]:
try:
import jieba
2025-07-10 15:08:20 +08:00
return jieba.dt.cut(str(sentence), cut_all=cut_all, HMM=HMM, use_paddle=use_paddle)
2025-07-09 17:39:50 +08:00
except ImportError:
raise ValueError("jieba is not install")