27 lines
601 B
Python
27 lines
601 B
Python
|
|
"""翻译后端抽象。"""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from abc import ABC, abstractmethod
|
||
|
|
from dataclasses import dataclass
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class TranslationResult:
|
||
|
|
text: str
|
||
|
|
engine: str
|
||
|
|
chars: int
|
||
|
|
cached: bool = False
|
||
|
|
|
||
|
|
|
||
|
|
class BaseTranslator(ABC):
|
||
|
|
name: str = "base"
|
||
|
|
|
||
|
|
@abstractmethod
|
||
|
|
async def translate(self, text: str, source: str = "auto", target: str = "zh") -> TranslationResult:
|
||
|
|
"""同步调用,失败抛异常。"""
|
||
|
|
|
||
|
|
|
||
|
|
def count_chars(s: str) -> int:
|
||
|
|
"""近似的字符计数(Unicode 码点)。腾讯 TMT 按字符数计费。"""
|
||
|
|
return len(s)
|