import re import pinyin import pyfiglet import toml from fastapi import FastAPI from pydantic.types import Callable def extract_brand_name(input_str): """ 提取品牌名称,规则如下: - 中文:返回拼音首字母(大写) - 英文:根据单词数量决定,少量单词返回大驼峰,大量单词返回纯大写 - 支持多种英文命名法(驼峰、中划线、下划线等)的解析 参数: input_str: 输入的字符串(中英文均可) 返回: 处理后的品牌名称字符串 """ # 判断是否为中文(包含中文字符) if re.search(r"[\u4e00-\u9fff]", input_str): # 提取中文并转换为拼音首字母 chinese_chars = re.findall(r"[\u4e00-\u9fff]", input_str) pinyin_initials = "".join( [ pinyin.get(c, format="strip", delimiter="")[0].upper() for c in chinese_chars ] ) return pinyin_initials # 处理英文情况:拆分不同命名法的单词 # 处理驼峰命名(大小写转换处拆分) s = re.sub(r"(?<=[a-z])(?=[A-Z])", " ", input_str) # 处理中划线、下划线和其他非字母字符,统一替换为空格 s = re.sub(r"[-_^&%$#@!~`]", " ", s) # 提取所有单词(忽略非字母字符) words = re.findall(r"[A-Za-z]+", s) # 统一转为小写后再处理(避免大小写影响) words = [word.lower() for word in words if word] if not words: return "" # 空输入处理 # 定义少量单词与大量单词的阈值(这里使用3个作为分界) threshold = 3 if len(words) <= threshold: # 少量单词:返回大驼峰格式 return "".join([word.capitalize() for word in words]) else: # 大量单词:返回纯大写字母(取每个单词首字母) return "".join([word[0].upper() for word in words]) # Load the TOML file config = toml.load("pyproject.toml") # Access specific values project_name = config.get("project", {}).get("name", None) project_description = config.get("project", {}).get("description", None) project_version = config.get("project", {}).get("version", None) project_authors = config.get("project", {}).get("authors", []) def start(startup: Callable | None = None) -> FastAPI: # from pyfiglet import FigletFont # fonts = FigletFont().getFonts() # for font in fonts: # try: # result = pyfiglet.figlet_format(project_name, font="red_phoenix") # print(f"Font: {font}") # print(result) # print("-" * 50) # except Exception as e: # logger.warning(f"Failed to render font {font}: {e}") # if not startup: raise ValueError("startup function is required") print("-" * 50) print(pyfiglet.figlet_format(project_name, font="red_phoenix")) print(f"Project Description: {project_description}") print(f"Version: {project_version}") print("Author: ", end="") [ print(f"{author.get('name', '')} {author.get('email', '')}", end="") for author in project_authors ] print() print("-" * 50) return startup()