Use DeepL native glossary

This commit is contained in:
2025-01-19 23:37:58 +00:00
parent e262119e54
commit eb8653696e
6 changed files with 227 additions and 44 deletions

View File

@@ -19,36 +19,6 @@ class GenericmessageCommand extends SystemCommand
protected $version = '1.0.0';
private Translator $translator;
private const ZH_TO_EN = [
'兽设' => 'fursona',
'兽人' => 'furry character',
'兽控' => 'furry',
'毛毛' => 'fursuit',
'兽装' => 'fursuit',
'大佬' => 'da lao',
'丸吞' => 'vore',
'哦不' => 'oh no',
'哦哇塞' => 'o wa sai',
'哇塞' => 'wa sai',
'色色' => 'hentai',
];
private const EN_TO_ZH = [
'fursona' => '兽设',
'furry character' => '兽人',
'furry' => '兽控',
'fursuit' => '兽装',
'hentai' => '色色',
'sese' => '色色',
'sheshe' => '射射',
'wun tun' => '丸吞',
'vore' => '丸吞',
'money power' => '钞能力',
'oh no' => '哦不',
'touch fish' => '摸鱼',
'o wa sai' => '哦哇塞',
'wa sai' => '哇塞'
];
public function execute(): ServerResponse
{
global $deepl_api_key, $allowed_chat_ids;
@@ -70,21 +40,25 @@ class GenericmessageCommand extends SystemCommand
if ($text_english_count >= ($text_chara_count*2 /3)) {
$sourceLang = 'en';
$targetLang = 'zh';
$glossary = self::EN_TO_ZH;
} else {
$sourceLang = 'zh';
$targetLang = 'en-US';
$glossary = self::ZH_TO_EN;
$text = (new Chinese())->to(Chinese::ZH_HANS, $text);
}
foreach ($glossary as $search => $replace) {
$text = str_ireplace($search, $replace, $text);
}
$deeplOptions = [
'formality' => 'prefer_less',
'model_type' => 'prefer_quality_optimized',
];
try {
$glosasaryConfig = json_decode(file_get_contents(__DIR__ . '/../../glossary.json'), true);
if (isset($glosasaryConfig[$sourceLang][$targetLang])) {
$deeplOptions['glossary'] = $glosasaryConfig[$sourceLang][$targetLang];
}
} catch(\Exception $e) {}
$this->translator = new Translator($deepl_api_key);
try {
$translated = $this->translator->translateText($text, $sourceLang, $targetLang);
$translated = $this->translator->translateText($text, $sourceLang, $targetLang, $deeplOptions);
} catch (\Exception $e) {
return $this->replyToChat(get_class($e) . ': ' . $e->getMessage()); // ?? var_export($e, true)
}