add autodetect support for libre, watson, ddg

This commit is contained in:
Arya 2023-08-16 13:15:21 +05:30
parent 75f94266d7
commit 6b7a9b3f0d
Signed by: arya
GPG Key ID: 842D12BDA50DF120
5 changed files with 49 additions and 3 deletions

View File

@ -1,7 +1,7 @@
# TODO
- Create a web interface
- Proper Error handling for requests.go
- Tell which language Detect Language chose
- Tell which language Detect Language chose -- Only support for deepl and google is pending
- Finish simplytranslate-py compatible API for translations
- Alternative words
- Dictionary

16
utils/autodetect.go Normal file
View File

@ -0,0 +1,16 @@
package utils
func AutoDetectWatson(query string) (string, error) {
json := []byte(`{"text":"` + query + `"}`)
watsonOut := PostRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/detect", json)
gjsonArr := watsonOut.Get("payload.languages.0.language.language").Array()
answer := gjsonArr[0].String()
return answer, nil
}
func AutoDetectLibreTranslate(query string) (string, error) {
json := []byte(`{"q":"` + query + `"}`)
libreTranslateOut := PostRequest("https://translate.argosopentech.com/detect", json)
gjsonArr := libreTranslateOut.Get("0.language").Array()
answer := gjsonArr[0].String()
return answer, nil
}

View File

@ -136,6 +136,9 @@ func TranslateLibreTranslate(to string, from string, query string) (LangOut, err
langout.Engine = "libretranslate"
langout.SourceLang = FromOrig
langout.TargetLang = ToOrig
if from == "auto" {
langout.AutoDetect, _ = AutoDetectLibreTranslate(query)
}
return langout, nil
}
func TranslateWatson(to string, from string, query string) (LangOut, error) {
@ -160,10 +163,14 @@ func TranslateWatson(to string, from string, query string) (LangOut, error) {
if FromValid != true {
return LangOut{}, errors.New("Source language code invalid")
}
var langout LangOut
if from == "auto" {
langout.AutoDetect, _ = AutoDetectWatson(query)
from = langout.AutoDetect
}
json := []byte(`{"text":"` + query + `","source":"` + from + `","target":"` + to + `"}`)
watsonOut := PostRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/text", json)
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
var langout LangOut
langout.OutputText = gjsonArr[0].String()
langout.Engine = "watson"
langout.SourceLang = FromOrig
@ -306,13 +313,22 @@ func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error)
if FromValid != true {
return LangOut{}, errors.New("Source language code invalid")
}
duckDuckGoOut := PostRequest("https://duckduckgo.com/translation.js?vqd=4-80922924764394623683473042291214994119&query=translate&to="+to+"&from="+from, []byte(query))
var url string
if from == "auto" {
url = "https://duckduckgo.com/translation.js?vqd=4-80922924764394623683473042291214994119&query=translate&to="+to
} else {
url = "https://duckduckgo.com/translation.js?vqd=4-80922924764394623683473042291214994119&query=translate&to="+to+"&from="+from
}
duckDuckGoOut := PostRequest(url, []byte(query))
gjsonArr := duckDuckGoOut.Get("translated").Array()
var langout LangOut
langout.OutputText = gjsonArr[0].String()
langout.Engine = "duckduckgo"
langout.SourceLang = FromOrig
langout.TargetLang = ToOrig
if from == "auto" {
langout.AutoDetect = duckDuckGoOut.Get("detected_language").String()
}
return langout, nil
}
func TranslateAll(to string, from string, query string) ([]LangOut) {

View File

@ -517,5 +517,12 @@ func LangListDuckDuckGo(listType string) []List {
Name: "Zulu",
},
}
if listType == "sl" {
auto := List{
Id: "auto",
Name: "Detect Language",
}
ListData = append(ListData, auto)
}
return ListData
}

View File

@ -232,5 +232,12 @@ func LangListWatson(listType string) []List {
Name: "Welsh",
},
}
if listType == "sl" {
auto := List{
Id: "auto",
Name: "Detect Language",
}
ListData = append(ListData, auto)
}
return ListData
}