Expand source code
@api_app.post("/convert")
async def convert_text(request: dict):
ptype = request.get("ptype")
text = request.get("text")
try:
if ptype == "tc_sc":
direction = request.get("direction")
if direction == 't2s':
converted_text = tc2sc(text)
elif direction == 's2t':
converted_text = sc2tc(text)
else:
raise HTTPException(status_code=400, detail="不支持的转换方向")
return {"converted_text": converted_text}
elif ptype == "pinyin":
hyphen = request.get("pinyinHyphen")
converted_text = create_pinyin_string(text, hyphen=hyphen)
return {"converted_text": converted_text}
elif ptype == "country_zh_abbr":
is_country_abbr = request.get("isCountryAbbr")
if is_country_abbr:
converted_text = get_full_country_name(text)
else:
converted_text = get_country_zh_abbr(text)
return {"converted_text": converted_text}
elif ptype == "encoding":
converted_text = print_encoding_convert_tab(text)
return {"converted_text": converted_text}
else:
raise HTTPException(status_code=400, detail="未知的处理类型")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))