|
|
@@ -2,7 +2,7 @@
|
|
|
|
* @Author: Vincent Young
|
|
|
|
* @Author: Vincent Young
|
|
|
|
* @Date: 2024-09-16 11:59:24
|
|
|
|
* @Date: 2024-09-16 11:59:24
|
|
|
|
* @LastEditors: Vincent Yang
|
|
|
|
* @LastEditors: Vincent Yang
|
|
|
|
* @LastEditTime: 2025-02-01 03:21:41
|
|
|
|
* @LastEditTime: 2025-01-20 17:09:59
|
|
|
|
* @FilePath: /DeepLX/translate/translate.go
|
|
|
|
* @FilePath: /DeepLX/translate/translate.go
|
|
|
|
* @Telegram: https://t.me/missuo
|
|
|
|
* @Telegram: https://t.me/missuo
|
|
|
|
* @GitHub: https://github.com/missuo
|
|
|
|
* @GitHub: https://github.com/missuo
|
|
|
@@ -19,7 +19,6 @@ import (
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/abadojack/whatlanggo"
|
|
|
|
"github.com/abadojack/whatlanggo"
|
|
|
|
"github.com/imroc/req/v3"
|
|
|
|
"github.com/imroc/req/v3"
|
|
|
@@ -127,89 +126,92 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Split text by newlines
|
|
|
|
// Split text by newlines and store them for later reconstruction
|
|
|
|
textParts := strings.Split(text, "\n")
|
|
|
|
textParts := strings.Split(text, "\n")
|
|
|
|
|
|
|
|
var translatedParts []string
|
|
|
|
|
|
|
|
var allAlternatives [][]string // Store alternatives for each part
|
|
|
|
|
|
|
|
|
|
|
|
// Create channels for results
|
|
|
|
for _, part := range textParts {
|
|
|
|
type translationResult struct {
|
|
|
|
if strings.TrimSpace(part) == "" {
|
|
|
|
index int
|
|
|
|
translatedParts = append(translatedParts, "")
|
|
|
|
translation string
|
|
|
|
allAlternatives = append(allAlternatives, []string{""})
|
|
|
|
alternatives []string
|
|
|
|
continue
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
results := make(chan translationResult, len(textParts))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create a wait group to track all goroutines
|
|
|
|
// Split text first
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
splitResult, err := splitText(part, tagHandling == "html" || tagHandling == "xml", proxyURL, dlSession)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return DeepLXTranslationResult{
|
|
|
|
|
|
|
|
Code: http.StatusServiceUnavailable,
|
|
|
|
|
|
|
|
Message: err.Error(),
|
|
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Launch goroutines for each text part
|
|
|
|
// Get detected language if source language is auto
|
|
|
|
for i := range textParts {
|
|
|
|
if sourceLang == "auto" || sourceLang == "" {
|
|
|
|
wg.Add(1)
|
|
|
|
sourceLang = strings.ToUpper(whatlanggo.DetectLang(part).Iso6391())
|
|
|
|
go func(index int, text string) {
|
|
|
|
}
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if strings.TrimSpace(text) == "" {
|
|
|
|
// Prepare jobs from split result
|
|
|
|
results <- translationResult{
|
|
|
|
var jobs []Job
|
|
|
|
index: index,
|
|
|
|
chunks := splitResult.Get("result.texts.0.chunks").Array()
|
|
|
|
translation: "",
|
|
|
|
for idx, chunk := range chunks {
|
|
|
|
alternatives: []string{""},
|
|
|
|
sentence := chunk.Get("sentences.0")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
// Handle context
|
|
|
|
|
|
|
|
contextBefore := []string{}
|
|
|
|
|
|
|
|
contextAfter := []string{}
|
|
|
|
|
|
|
|
if idx > 0 {
|
|
|
|
|
|
|
|
contextBefore = []string{chunks[idx-1].Get("sentences.0.text").String()}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx < len(chunks)-1 {
|
|
|
|
|
|
|
|
contextAfter = []string{chunks[idx+1].Get("sentences.0.text").String()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Split text first
|
|
|
|
jobs = append(jobs, Job{
|
|
|
|
splitResult, err := splitText(text, tagHandling == "html" || tagHandling == "xml", proxyURL, dlSession)
|
|
|
|
Kind: "default",
|
|
|
|
if err != nil {
|
|
|
|
PreferredNumBeams: 4,
|
|
|
|
results <- translationResult{index: index, err: err}
|
|
|
|
RawEnContextBefore: contextBefore,
|
|
|
|
return
|
|
|
|
RawEnContextAfter: contextAfter,
|
|
|
|
}
|
|
|
|
Sentences: []Sentence{{
|
|
|
|
|
|
|
|
Prefix: sentence.Get("prefix").String(),
|
|
|
|
|
|
|
|
Text: sentence.Get("text").String(),
|
|
|
|
|
|
|
|
ID: idx + 1,
|
|
|
|
|
|
|
|
}},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get detected language if source language is auto
|
|
|
|
hasRegionalVariant := false
|
|
|
|
currentSourceLang := sourceLang
|
|
|
|
targetLangCode := targetLang
|
|
|
|
if currentSourceLang == "auto" || currentSourceLang == "" {
|
|
|
|
targetLangParts := strings.Split(targetLang, "-")
|
|
|
|
currentSourceLang = strings.ToUpper(whatlanggo.DetectLang(text).Iso6391())
|
|
|
|
if len(targetLangParts) > 1 {
|
|
|
|
}
|
|
|
|
targetLangCode = targetLangParts[0]
|
|
|
|
|
|
|
|
hasRegionalVariant = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare jobs from split result
|
|
|
|
// Prepare translation request
|
|
|
|
var jobs []Job
|
|
|
|
id := getRandomNumber()
|
|
|
|
chunks := splitResult.Get("result.texts.0.chunks").Array()
|
|
|
|
|
|
|
|
for idx, chunk := range chunks {
|
|
|
|
|
|
|
|
sentence := chunk.Get("sentences.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handle context
|
|
|
|
postData := &PostData{
|
|
|
|
contextBefore := []string{}
|
|
|
|
Jsonrpc: "2.0",
|
|
|
|
contextAfter := []string{}
|
|
|
|
Method: "LMT_handle_jobs",
|
|
|
|
if idx > 0 {
|
|
|
|
ID: id,
|
|
|
|
contextBefore = []string{chunks[idx-1].Get("sentences.0.text").String()}
|
|
|
|
Params: Params{
|
|
|
|
}
|
|
|
|
CommonJobParams: CommonJobParams{
|
|
|
|
if idx < len(chunks)-1 {
|
|
|
|
Mode: "translate",
|
|
|
|
contextAfter = []string{chunks[idx+1].Get("sentences.0.text").String()}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Lang: Lang{
|
|
|
|
|
|
|
|
SourceLangComputed: strings.ToUpper(sourceLang),
|
|
|
|
|
|
|
|
TargetLang: strings.ToUpper(targetLangCode),
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
Jobs: jobs,
|
|
|
|
|
|
|
|
Priority: 1,
|
|
|
|
|
|
|
|
Timestamp: getTimeStamp(getICount(part)),
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
jobs = append(jobs, Job{
|
|
|
|
if hasRegionalVariant {
|
|
|
|
Kind: "default",
|
|
|
|
postData = &PostData{
|
|
|
|
PreferredNumBeams: 4,
|
|
|
|
|
|
|
|
RawEnContextBefore: contextBefore,
|
|
|
|
|
|
|
|
RawEnContextAfter: contextAfter,
|
|
|
|
|
|
|
|
Sentences: []Sentence{{
|
|
|
|
|
|
|
|
Prefix: sentence.Get("prefix").String(),
|
|
|
|
|
|
|
|
Text: sentence.Get("text").String(),
|
|
|
|
|
|
|
|
ID: idx + 1,
|
|
|
|
|
|
|
|
}},
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hasRegionalVariant := false
|
|
|
|
|
|
|
|
targetLangCode := targetLang
|
|
|
|
|
|
|
|
targetLangParts := strings.Split(targetLang, "-")
|
|
|
|
|
|
|
|
if len(targetLangParts) > 1 {
|
|
|
|
|
|
|
|
targetLangCode = targetLangParts[0]
|
|
|
|
|
|
|
|
hasRegionalVariant = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare translation request
|
|
|
|
|
|
|
|
id := getRandomNumber()
|
|
|
|
|
|
|
|
postData := &PostData{
|
|
|
|
|
|
|
|
Jsonrpc: "2.0",
|
|
|
|
Jsonrpc: "2.0",
|
|
|
|
Method: "LMT_handle_jobs",
|
|
|
|
Method: "LMT_handle_jobs",
|
|
|
|
ID: id,
|
|
|
|
ID: id,
|
|
|
@@ -219,82 +221,62 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
|
|
|
RegionalVariant: map[bool]string{true: targetLang, false: ""}[hasRegionalVariant],
|
|
|
|
RegionalVariant: map[bool]string{true: targetLang, false: ""}[hasRegionalVariant],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Lang: Lang{
|
|
|
|
Lang: Lang{
|
|
|
|
SourceLangComputed: strings.ToUpper(currentSourceLang),
|
|
|
|
SourceLangComputed: strings.ToUpper(sourceLang),
|
|
|
|
TargetLang: strings.ToUpper(targetLangCode),
|
|
|
|
TargetLang: strings.ToUpper(targetLangCode),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Jobs: jobs,
|
|
|
|
Jobs: jobs,
|
|
|
|
Priority: 1,
|
|
|
|
Priority: 1,
|
|
|
|
Timestamp: getTimeStamp(getICount(text)),
|
|
|
|
Timestamp: getTimeStamp(getICount(part)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Make translation request
|
|
|
|
// Make translation request
|
|
|
|
result, err := makeRequest(postData, "LMT_handle_jobs", proxyURL, dlSession)
|
|
|
|
result, err := makeRequest(postData, "LMT_handle_jobs", proxyURL, dlSession)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
results <- translationResult{index: index, err: err}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Process translation results
|
|
|
|
|
|
|
|
var partTranslation string
|
|
|
|
|
|
|
|
var partAlternatives []string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
translations := result.Get("result.translations").Array()
|
|
|
|
|
|
|
|
if len(translations) > 0 {
|
|
|
|
|
|
|
|
// Process main translation
|
|
|
|
|
|
|
|
for _, translation := range translations {
|
|
|
|
|
|
|
|
partTranslation += translation.Get("beams.0.sentences.0.text").String() + " "
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
partTranslation = strings.TrimSpace(partTranslation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Process alternatives
|
|
|
|
|
|
|
|
numBeams := len(translations[0].Get("beams").Array())
|
|
|
|
|
|
|
|
for i := 1; i < numBeams; i++ {
|
|
|
|
|
|
|
|
var altText string
|
|
|
|
|
|
|
|
for _, translation := range translations {
|
|
|
|
|
|
|
|
beams := translation.Get("beams").Array()
|
|
|
|
|
|
|
|
if i < len(beams) {
|
|
|
|
|
|
|
|
altText += beams[i].Get("sentences.0.text").String() + " "
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if altText != "" {
|
|
|
|
|
|
|
|
partAlternatives = append(partAlternatives, strings.TrimSpace(altText))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if partTranslation == "" {
|
|
|
|
|
|
|
|
results <- translationResult{index: index, err: fmt.Errorf("translation failed")}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results <- translationResult{
|
|
|
|
|
|
|
|
index: index,
|
|
|
|
|
|
|
|
translation: partTranslation,
|
|
|
|
|
|
|
|
alternatives: partAlternatives,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}(i, textParts[i])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Close results channel when all goroutines are done
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
close(results)
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Collect results maintaining original order
|
|
|
|
|
|
|
|
translatedParts := make([]string, len(textParts))
|
|
|
|
|
|
|
|
allAlternatives := make([][]string, len(textParts))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for result := range results {
|
|
|
|
|
|
|
|
if result.err != nil {
|
|
|
|
|
|
|
|
return DeepLXTranslationResult{
|
|
|
|
return DeepLXTranslationResult{
|
|
|
|
Code: http.StatusServiceUnavailable,
|
|
|
|
Code: http.StatusServiceUnavailable,
|
|
|
|
Message: result.err.Error(),
|
|
|
|
Message: err.Error(),
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
translatedParts[result.index] = result.translation
|
|
|
|
|
|
|
|
allAlternatives[result.index] = result.alternatives
|
|
|
|
// Process translation results
|
|
|
|
|
|
|
|
var partTranslation string
|
|
|
|
|
|
|
|
var partAlternatives []string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
translations := result.Get("result.translations").Array()
|
|
|
|
|
|
|
|
if len(translations) > 0 {
|
|
|
|
|
|
|
|
// Process main translation
|
|
|
|
|
|
|
|
for _, translation := range translations {
|
|
|
|
|
|
|
|
partTranslation += translation.Get("beams.0.sentences.0.text").String() + " "
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
partTranslation = strings.TrimSpace(partTranslation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Process alternatives
|
|
|
|
|
|
|
|
numBeams := len(translations[0].Get("beams").Array())
|
|
|
|
|
|
|
|
for i := 1; i < numBeams; i++ { // Start from 1 since 0 is the main translation
|
|
|
|
|
|
|
|
var altText string
|
|
|
|
|
|
|
|
for _, translation := range translations {
|
|
|
|
|
|
|
|
beams := translation.Get("beams").Array()
|
|
|
|
|
|
|
|
if i < len(beams) {
|
|
|
|
|
|
|
|
altText += beams[i].Get("sentences.0.text").String() + " "
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if altText != "" {
|
|
|
|
|
|
|
|
partAlternatives = append(partAlternatives, strings.TrimSpace(altText))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if partTranslation == "" {
|
|
|
|
|
|
|
|
return DeepLXTranslationResult{
|
|
|
|
|
|
|
|
Code: http.StatusServiceUnavailable,
|
|
|
|
|
|
|
|
Message: "Translation failed",
|
|
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
translatedParts = append(translatedParts, partTranslation)
|
|
|
|
|
|
|
|
allAlternatives = append(allAlternatives, partAlternatives)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Join all translated parts with newlines
|
|
|
|
// Join all translated parts with newlines
|
|
|
@@ -316,9 +298,9 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
|
|
|
if i < len(alts) {
|
|
|
|
if i < len(alts) {
|
|
|
|
altParts = append(altParts, alts[i])
|
|
|
|
altParts = append(altParts, alts[i])
|
|
|
|
} else if len(translatedParts[j]) == 0 {
|
|
|
|
} else if len(translatedParts[j]) == 0 {
|
|
|
|
altParts = append(altParts, "")
|
|
|
|
altParts = append(altParts, "") // Keep empty lines
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
altParts = append(altParts, translatedParts[j])
|
|
|
|
altParts = append(altParts, translatedParts[j]) // Use main translation if no alternative
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
combinedAlternatives = append(combinedAlternatives, strings.Join(altParts, "\n"))
|
|
|
|
combinedAlternatives = append(combinedAlternatives, strings.Join(altParts, "\n"))
|
|
|
@@ -326,7 +308,7 @@ func TranslateByDeepLX(sourceLang, targetLang, text string, tagHandling string,
|
|
|
|
|
|
|
|
|
|
|
|
return DeepLXTranslationResult{
|
|
|
|
return DeepLXTranslationResult{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ID: getRandomNumber(),
|
|
|
|
ID: getRandomNumber(), // Using new ID for the complete translation
|
|
|
|
Data: translatedText,
|
|
|
|
Data: translatedText,
|
|
|
|
Alternatives: combinedAlternatives,
|
|
|
|
Alternatives: combinedAlternatives,
|
|
|
|
SourceLang: sourceLang,
|
|
|
|
SourceLang: sourceLang,
|
|
|
|