-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
650 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package volcengine | ||
|
||
type Cfg struct { | ||
AccessKey string `mapstructure:"access_key"` | ||
SecretKey string `mapstructure:"secret_key"` | ||
} | ||
|
||
func (customC Cfg) Default() *Cfg { | ||
return &Cfg{AccessKey: "", SecretKey: ""} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package volcengine | ||
|
||
import ( | ||
"anto/dependency/service/translator" | ||
) | ||
|
||
var langSupported = []translator.LangPair{ | ||
{"zh", "中文"}, | ||
{"en", "英语"}, | ||
{"ja", "日语"}, | ||
{"ru", "俄语"}, | ||
{"fr", "法语"}, | ||
{"ko", "韩语"}, | ||
{"de", "德语"}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package volcengine | ||
|
||
import ( | ||
"anto/dependency/service/translator" | ||
"anto/lib/log" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/golang-module/carbon" | ||
"github.com/volcengine/volc-sdk-golang/base" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const ( | ||
host = "open.volcengineapi.com" | ||
service = "translate" | ||
version = "2020-06-01" | ||
) | ||
|
||
var ( | ||
apiSingleton *Translator | ||
onceSingleton sync.Once | ||
) | ||
|
||
func Singleton() *Translator { | ||
onceSingleton.Do(func() { | ||
apiSingleton = New() | ||
}) | ||
return apiSingleton | ||
} | ||
|
||
func New() *Translator { | ||
return &Translator{ | ||
id: "volcengine", | ||
name: "火山翻译", | ||
qps: 10, | ||
procMax: 20, | ||
textMaxLen: 5000, | ||
sep: "\n", | ||
langSupported: langSupported, | ||
} | ||
} | ||
|
||
type Translator struct { | ||
id string | ||
name string | ||
cfg *Cfg | ||
qps int | ||
procMax int | ||
textMaxLen int | ||
langSupported []translator.LangPair | ||
sep string | ||
mtClient *base.Client | ||
} | ||
|
||
func (customT *Translator) Init(cfg interface{}) { customT.cfg = cfg.(*Cfg) } | ||
|
||
func (customT *Translator) GetId() string { return customT.id } | ||
func (customT *Translator) GetName() string { return customT.name } | ||
func (customT *Translator) GetCfg() interface{} { return nil } | ||
func (customT *Translator) GetQPS() int { return customT.qps } | ||
func (customT *Translator) GetProcMax() int { return customT.procMax } | ||
func (customT *Translator) GetTextMaxLen() int { return customT.textMaxLen } | ||
func (customT *Translator) GetLangSupported() []translator.LangPair { return customT.langSupported } | ||
func (customT *Translator) GetSep() string { return customT.sep } | ||
func (customT *Translator) IsValid() bool { | ||
return customT.cfg != nil && customT.cfg.AccessKey != "" && customT.cfg.SecretKey != "" | ||
} | ||
|
||
func (customT *Translator) Translate(args *translator.TranslateArgs) (*translator.TranslateRes, error) { | ||
timeStart := carbon.Now() | ||
|
||
params := &translateRequestParams{ | ||
SourceLanguage: args.FromLang, | ||
TargetLanguage: args.ToLang, | ||
} | ||
params.TextList = append(params.TextList, args.TextContent) | ||
jsonBytes, _ := json.Marshal(params) | ||
respBytes, _, err := customT.client().Json("TranslateText", nil, string(jsonBytes)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp := new(translateResponse) | ||
if err = json.Unmarshal(respBytes, resp); err != nil { | ||
log.Singleton().Error(fmt.Sprintf("解析报文异常, 引擎: %s, 错误: %s", customT.GetName(), err)) | ||
return nil, fmt.Errorf("解析报文出现异常, 错误: %s", err.Error()) | ||
} | ||
|
||
if resp.ResponseMetaData.Error.Code != "" { | ||
log.Singleton().Error(fmt.Sprintf("接口响应异常, 引擎: %s, 错误: %s(%s)", customT.GetName(), resp.ResponseMetaData.Error.Message, resp.ResponseMetaData.Error.Code)) | ||
return nil, fmt.Errorf("接口响应异常, 引擎: %s, 错误: %s", customT.GetName(), resp.ResponseMetaData.Error.Message) | ||
} | ||
srcTexts := strings.Split(args.TextContent, customT.GetSep()) | ||
tgtTexts := strings.Split(resp.TranslationList[0].Translation, customT.GetSep()) | ||
if len(srcTexts) != len(tgtTexts) { | ||
log.Singleton().Error(fmt.Sprintf("响应解析错误, 引擎: %s, 错误: 译文和原文数量匹配失败", customT.GetName())) | ||
return nil, fmt.Errorf("翻译异常, 错误: 源文和译文数量不对等") | ||
} | ||
|
||
ret := new(translator.TranslateRes) | ||
|
||
for textIdx, textTarget := range tgtTexts { | ||
ret.Results = append(ret.Results, &translator.TranslateResBlock{ | ||
Id: srcTexts[textIdx], TextTranslated: textTarget, | ||
}) | ||
} | ||
|
||
ret.TimeUsed = int(carbon.Now().DiffAbsInSeconds(timeStart)) | ||
return ret, nil | ||
|
||
} | ||
|
||
func (customT *Translator) client() *base.Client { | ||
if customT.mtClient == nil { | ||
serviceInfo := &base.ServiceInfo{ | ||
Timeout: 5 * time.Second, Host: host, | ||
Header: http.Header{"Accept": []string{"application/json"}}, | ||
Credentials: base.Credentials{Region: base.RegionCnNorth1, Service: service}, | ||
} | ||
|
||
apiInfoList := map[string]*base.ApiInfo{ | ||
"TranslateText": { | ||
Method: http.MethodPost, Path: "/", | ||
Query: url.Values{"Action": []string{"TranslateText"}, "Version": []string{version}}, | ||
}, | ||
} | ||
client := base.NewClient(serviceInfo, apiInfoList) | ||
client.SetAccessKey(customT.cfg.AccessKey) | ||
client.SetSecretKey(customT.cfg.SecretKey) | ||
customT.mtClient = client | ||
} | ||
|
||
return customT.mtClient | ||
} | ||
|
||
type translateRequestParams struct { | ||
SourceLanguage string | ||
TargetLanguage string | ||
TextList []string | ||
} | ||
|
||
type translateResponse struct { | ||
ResponseMetaData struct { | ||
RequestId string `json:"RequestId"` | ||
Action string `json:"Action"` | ||
Version string `json:"Version"` | ||
Service string `json:"Service"` | ||
Region string `json:"Region"` | ||
Error struct { | ||
Code string `json:"Code"` | ||
Message string `json:"Message"` | ||
} `json:"Error"` | ||
} `json:"ResponseMetaData"` | ||
TranslationList []struct { | ||
Translation string `json:"Translation"` | ||
//DetectedSourceLanguage string `json:"DetectedSourceLanguage"` | ||
//Extra interface{} `json:"Extra"` | ||
} `json:"TranslationList"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.