add autodetect support for libre, watson, ddg
This commit is contained in:
parent
75f94266d7
commit
6b7a9b3f0d
5 changed files with 49 additions and 3 deletions
2
TODO.md
2
TODO.md
|
@ -1,7 +1,7 @@
|
||||||
# TODO
|
# TODO
|
||||||
- Create a web interface
|
- Create a web interface
|
||||||
- Proper Error handling for requests.go
|
- 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
|
- Finish simplytranslate-py compatible API for translations
|
||||||
- Alternative words
|
- Alternative words
|
||||||
- Dictionary
|
- Dictionary
|
||||||
|
|
16
utils/autodetect.go
Normal file
16
utils/autodetect.go
Normal 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
|
||||||
|
}
|
|
@ -136,6 +136,9 @@ func TranslateLibreTranslate(to string, from string, query string) (LangOut, err
|
||||||
langout.Engine = "libretranslate"
|
langout.Engine = "libretranslate"
|
||||||
langout.SourceLang = FromOrig
|
langout.SourceLang = FromOrig
|
||||||
langout.TargetLang = ToOrig
|
langout.TargetLang = ToOrig
|
||||||
|
if from == "auto" {
|
||||||
|
langout.AutoDetect, _ = AutoDetectLibreTranslate(query)
|
||||||
|
}
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
func TranslateWatson(to string, from string, query string) (LangOut, error) {
|
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 {
|
if FromValid != true {
|
||||||
return LangOut{}, errors.New("Source language code invalid")
|
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 + `"}`)
|
json := []byte(`{"text":"` + query + `","source":"` + from + `","target":"` + to + `"}`)
|
||||||
watsonOut := PostRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/text", json)
|
watsonOut := PostRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/text", json)
|
||||||
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
|
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
|
||||||
var langout LangOut
|
|
||||||
langout.OutputText = gjsonArr[0].String()
|
langout.OutputText = gjsonArr[0].String()
|
||||||
langout.Engine = "watson"
|
langout.Engine = "watson"
|
||||||
langout.SourceLang = FromOrig
|
langout.SourceLang = FromOrig
|
||||||
|
@ -306,13 +313,22 @@ func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error)
|
||||||
if FromValid != true {
|
if FromValid != true {
|
||||||
return LangOut{}, errors.New("Source language code invalid")
|
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()
|
gjsonArr := duckDuckGoOut.Get("translated").Array()
|
||||||
var langout LangOut
|
var langout LangOut
|
||||||
langout.OutputText = gjsonArr[0].String()
|
langout.OutputText = gjsonArr[0].String()
|
||||||
langout.Engine = "duckduckgo"
|
langout.Engine = "duckduckgo"
|
||||||
langout.SourceLang = FromOrig
|
langout.SourceLang = FromOrig
|
||||||
langout.TargetLang = ToOrig
|
langout.TargetLang = ToOrig
|
||||||
|
if from == "auto" {
|
||||||
|
langout.AutoDetect = duckDuckGoOut.Get("detected_language").String()
|
||||||
|
}
|
||||||
return langout, nil
|
return langout, nil
|
||||||
}
|
}
|
||||||
func TranslateAll(to string, from string, query string) ([]LangOut) {
|
func TranslateAll(to string, from string, query string) ([]LangOut) {
|
||||||
|
|
|
@ -517,5 +517,12 @@ func LangListDuckDuckGo(listType string) []List {
|
||||||
Name: "Zulu",
|
Name: "Zulu",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if listType == "sl" {
|
||||||
|
auto := List{
|
||||||
|
Id: "auto",
|
||||||
|
Name: "Detect Language",
|
||||||
|
}
|
||||||
|
ListData = append(ListData, auto)
|
||||||
|
}
|
||||||
return ListData
|
return ListData
|
||||||
}
|
}
|
||||||
|
|
|
@ -232,5 +232,12 @@ func LangListWatson(listType string) []List {
|
||||||
Name: "Welsh",
|
Name: "Welsh",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if listType == "sl" {
|
||||||
|
auto := List{
|
||||||
|
Id: "auto",
|
||||||
|
Name: "Detect Language",
|
||||||
|
}
|
||||||
|
ListData = append(ListData, auto)
|
||||||
|
}
|
||||||
return ListData
|
return ListData
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue