Skip to content

Commit

Permalink
feat(#19): 新增科大讯飞机器翻译(新)(xfyun);
Browse files Browse the repository at this point in the history
  • Loading branch information
speauty committed Aug 6, 2024
1 parent 4f9f1bf commit 3bbf118
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
tidy:
go mod tidy

BinName=anto-v3.6.3-windows.exe
BinName=anto-v3.7.0-windows.exe

deploy: rs build

Expand Down
3 changes: 3 additions & 0 deletions bootstrap/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"anto/domain/service/translator/openapi_youdao"
"anto/domain/service/translator/tencent_cloud_mt"
"anto/domain/service/translator/volcengine"
"anto/domain/service/translator/xfyun"
"anto/lib/log"
"context"
)
Expand Down Expand Up @@ -53,6 +54,7 @@ func Boot(_ context.Context) {
deepl_pro.API().Init(cfg.Singleton().DeepLPro)
ai_baidu.API().Init(cfg.Singleton().AiBaidu)
microsoft_edge.API().Init(cfg.Singleton().MicrosoftEdge)
xfyun.API().Init(cfg.Singleton().XFYun)

repository.GetTranslators().Register(
huawei_cloud_nlp.API(), baidu.API(),
Expand All @@ -61,5 +63,6 @@ func Boot(_ context.Context) {
volcengine.API(), g_deepl_x.API(),
google_cloud.API(), openai.API(), deepl.API(), deepl_pro.API(),
openai_sweet.API(), ai_baidu.API(), microsoft_edge.API(),
xfyun.API(),
)
}
3 changes: 3 additions & 0 deletions cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"anto/domain/service/translator/openapi_youdao"
"anto/domain/service/translator/tencent_cloud_mt"
"anto/domain/service/translator/volcengine"
"anto/domain/service/translator/xfyun"
"anto/domain/service/translator/youdao"
"anto/platform/win/ui"
"fmt"
Expand Down Expand Up @@ -52,6 +53,7 @@ func Singleton() *Cfg {
apiSingleton.DeepLPro = new(deepl_pro.Config).Default().(*deepl_pro.Config)
apiSingleton.AiBaidu = new(ai_baidu.Config).Default().(*ai_baidu.Config)
apiSingleton.MicrosoftEdge = new(microsoft_edge.Config).Default().(*microsoft_edge.Config)
apiSingleton.XFYun = new(xfyun.Config).Default().(*xfyun.Config)
})
return apiSingleton
}
Expand All @@ -77,6 +79,7 @@ type Cfg struct {
DeepLPro *deepl_pro.Config `mapstructure:"deepl_pro"`
AiBaidu *ai_baidu.Config `mapstructure:"ai_baidu"`
MicrosoftEdge *microsoft_edge.Config `mapstructure:"microsoft_edge"`
XFYun *xfyun.Config `mapstructure:"xfyun"`

currentViper *viper.Viper `mapstructure:"-"`
}
Expand Down
2 changes: 2 additions & 0 deletions cfg/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (customC *Cfg) InitConfig() error {
_ = customC.VolcEngine.SyncDisk(currentViper)
_ = customC.YouDao.SyncDisk(currentViper)
_ = customC.AiBaidu.SyncDisk(currentViper)
_ = customC.MicrosoftEdge.SyncDisk(currentViper)
_ = customC.XFYun.SyncDisk(currentViper)

return customC.Sync()
}
Expand Down
2 changes: 1 addition & 1 deletion common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
const (
AppName = "anto"
Author = "speauty"
Version = "v3.6.3"
Version = "v3.7.0"
DownloadLatestVersionUrl = "http://kodo.app.speauty.cn/anto-latest-windows.exe"

GoUidLen = 8
Expand Down
57 changes: 33 additions & 24 deletions cron/translate/srt_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,41 +86,50 @@ func (customCron *SrtTranslator) jobTranslator() {
}

var blockChunked []string
tmpBlockStr := ""
for _, block := range currentData.PrtSrt.Blocks {
if block.SubTrack != "" && currentData.PtrOpts.TranslateMode == common.ModeDelta {
continue
}
if currentData.PtrOpts.Translator.GetSep() != "" {
tmpBlockStr := ""
for _, block := range currentData.PrtSrt.Blocks {
if block.SubTrack != "" && currentData.PtrOpts.TranslateMode == common.ModeDelta {
continue
}

if len(tmpBlockStr) >= currentData.PtrOpts.Translator.GetCfg().GetMaxCharNum() {
blockChunked = append(blockChunked, tmpBlockStr)
tmpBlockStr = ""
}
if len(tmpBlockStr) >= currentData.PtrOpts.Translator.GetCfg().GetMaxCharNum() {
blockChunked = append(blockChunked, tmpBlockStr)
tmpBlockStr = ""
}

if len(block.MainTrack) >= currentData.PtrOpts.Translator.GetCfg().GetMaxCharNum() {
if tmpBlockStr != "" {
if len(block.MainTrack) >= currentData.PtrOpts.Translator.GetCfg().GetMaxCharNum() {
if tmpBlockStr != "" {
blockChunked = append(blockChunked, tmpBlockStr)
tmpBlockStr = ""
}
blockChunked = append(blockChunked, block.MainTrack)
continue
}

if len(block.MainTrack)+len(tmpBlockStr) >= currentData.PtrOpts.Translator.GetCfg().GetMaxCharNum() {
blockChunked = append(blockChunked, tmpBlockStr)
tmpBlockStr = ""
}
blockChunked = append(blockChunked, block.MainTrack)
continue
}

if len(block.MainTrack)+len(tmpBlockStr) >= currentData.PtrOpts.Translator.GetCfg().GetMaxCharNum() {
if tmpBlockStr == "" {
tmpBlockStr = block.MainTrack
} else {
tmpBlockStr = fmt.Sprintf("%s%s%s", tmpBlockStr, currentData.PtrOpts.Translator.GetSep(), block.MainTrack)
}
}
if tmpBlockStr != "" {
blockChunked = append(blockChunked, tmpBlockStr)
tmpBlockStr = ""
}

if tmpBlockStr == "" {
tmpBlockStr = block.MainTrack
} else {
tmpBlockStr = fmt.Sprintf("%s%s%s", tmpBlockStr, currentData.PtrOpts.Translator.GetSep(), block.MainTrack)
} else {
for _, block := range currentData.PrtSrt.Blocks {
if block.SubTrack != "" && currentData.PtrOpts.TranslateMode == common.ModeDelta {
continue
}
blockChunked = append(blockChunked, block.MainTrack)
}
}
if tmpBlockStr != "" {
blockChunked = append(blockChunked, tmpBlockStr)
tmpBlockStr = ""
}

if len(blockChunked) == 0 {
chanMsg <- fmt.Sprintf("字幕文件(%s)未解析到需要翻译的字幕块, 疑似增量翻译模式", currentData.PrtSrt.FileName)
Expand Down
2 changes: 1 addition & 1 deletion domain/service/translator/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func RequestSimpleHttp(ctx context.Context, engine ImplTranslator, url string, i
headers["content-type"] = "application/json"
headers["accept"] = "application/json"

client := req.C().SetCommonHeaders(headers).SetCommonRetryCount(3)
client := req.C().DevMode().SetCommonHeaders(headers).SetCommonRetryCount(3)
if strings.Contains(url, "api.openai.com") {
client.SetProxyURL("http://127.0.0.1:7890")
}
Expand Down
88 changes: 88 additions & 0 deletions domain/service/translator/xfyun/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package xfyun

import (
"anto/domain/service/translator"
"github.com/spf13/viper"
)

type Config struct {
*translator.DefaultConfig
AppId string `mapstructure:"app_id"`
ApiKey string `mapstructure:"api_key"`
ApiSecret string `mapstructure:"api_secret"`
QPS int `mapstructure:"qps"`
MaxCharNum int `mapstructure:"max_single_text_length"`
MaxCoroutineNum int `mapstructure:"max_coroutine_num"`
}

func (config *Config) Default() translator.ImplConfig {
return &Config{
AppId: "", ApiKey: "", ApiSecret: "",
MaxCharNum: 5000, QPS: 50, MaxCoroutineNum: 20,
}
}

func (config *Config) SyncDisk(currentViper *viper.Viper) error {
tagAndVal := config.JoinAllTagAndValue(API(), config, "mapstructure")

for tag, val := range tagAndVal {
currentViper.Set(tag, val)
}
return nil
}

func (config *Config) GetProjectKey() string { return config.AppId }
func (config *Config) GetAK() string { return config.ApiKey }
func (config *Config) GetSK() string { return config.ApiSecret }

func (config *Config) GetQPS() int { return config.QPS }
func (config *Config) GetMaxCharNum() int { return config.MaxCharNum }
func (config *Config) GetMaxCoroutineNum() int { return config.MaxCoroutineNum }

func (config *Config) SetProjectKey(str string) error {
if err := config.ValidatorStr(str); err != nil {
return err
}
config.AppId = str
return nil
}

func (config *Config) SetAK(str string) error {
if err := config.ValidatorStr(str); err != nil {
return err
}
config.ApiKey = str
return nil
}

func (config *Config) SetSK(str string) error {
if err := config.ValidatorStr(str); err != nil {
return err
}
config.ApiSecret = str
return nil
}

func (config *Config) SetQPS(num int) error {
if err := config.ValidatorNum(num); err != nil {
return err
}
config.QPS = num
return nil
}

func (config *Config) SetMaxCharNum(num int) error {
if err := config.ValidatorNum(num); err != nil {
return err
}
config.MaxCharNum = num
return nil
}

func (config *Config) SetMaxCoroutineNum(num int) error {
if err := config.ValidatorNum(num); err != nil {
return err
}
config.MaxCoroutineNum = num
return nil
}
21 changes: 21 additions & 0 deletions domain/service/translator/xfyun/lang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package xfyun

import (
"anto/domain/service/translator"
)

// @link https://www.xfyun.cn/doc/nlp/xftrans_new/API.html#%E8%AF%AD%E7%A7%8D%E5%88%97%E8%A1%A8
var langSupported = []translator.LangPair{
{"cn", "中文"}, {"en", "英语"}, {"cs", "捷克语"}, {"ha", "豪萨语"},
{"ja", "日语"}, {"ro", "罗马尼亚语"}, {"hu", "匈牙利语"}, {"ko", "韩语"},
{"sv", "瑞典语"}, {"sw", "斯瓦希里语"}, {"th", "泰语"}, {"nl", "荷兰语"},
{"uz", "乌兹别克语"}, {"ru", "俄语"}, {"pl", "波兰语"}, {"zu", "祖鲁语"},
{"bg", "保加利亚语"}, {"ar", "阿拉伯语"}, {"el", "希腊语"}, {"uk", "乌克兰语"},
{"fa", "波斯语"}, {"he", "希伯来语"}, {"vi", "越南语"}, {"ps", "普什图语"},
{"hy", "亚美尼亚语"}, {"ms", "马来语"}, {"ur", "乌尔都语"}, {"hy", "亚美尼亚语"},
{"ms", "马来语"}, {"ur", "乌尔都语"}, {"ka", "格鲁吉亚语"}, {"id", "印尼语"},
{"yue", "广东话"}, {"tl", "菲律宾语"}, {"bn", "孟加拉语"}, {"ii", "彝语"},
{"de", "德语"}, {"nm", "外蒙语"}, {"zua", "壮语"}, {"es", "西班牙语"},
{"kk", "外哈语"}, {"mn", "内蒙语"}, {"fr", "法语"}, {"tr", "土耳其语"},
{"kka", "内哈萨克语"},
}
Loading

0 comments on commit 3bbf118

Please sign in to comment.