mirror of
https://codeberg.org/aryak/mozhi
synced 2024-11-17 01:32:57 +05:30
add autodetect support for libre, watson, ddg
This commit is contained in:
parent
75f94266d7
commit
6b7a9b3f0d
2
TODO.md
2
TODO.md
@ -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
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.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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user