mozhi/utils/engines.go

261 lines
7.5 KiB
Go
Raw Normal View History

2023-06-25 00:10:57 +05:30
package utils
import (
2023-08-15 14:56:57 +05:30
"errors"
"github.com/OwO-Network/gdeeplx"
2023-06-25 00:10:57 +05:30
"github.com/gocolly/colly"
"github.com/google/go-querystring/query"
2023-08-15 14:56:57 +05:30
"github.com/google/uuid"
2023-06-25 00:10:57 +05:30
"os"
2023-08-15 14:56:57 +05:30
"strings"
2023-06-25 00:10:57 +05:30
)
2023-06-25 00:11:11 +05:30
type LangOut struct {
2023-08-15 21:41:02 +05:30
OutputText string `json:"translated-text"`
AutoDetect string `json:"detected"`
SourceLang string `json:"source_language"`
TargetLang string `json:"target_language"`
}
func TranslateGoogle(to string, from string, text string) (LangOut, error) {
// For some reason google uses no for norwegian instead of nb like the rest of the translators. This is for the All function primarily
if to == "nb" {
to = "no"
} else if from == "nb" {
to = "no"
}
var ToValid bool
var FromValid bool
for _, v := range LangListGoogle("sl") {
if v.Id == to {
2023-08-15 14:56:57 +05:30
ToValid = true
}
if v.Id == from {
FromValid = true
}
if FromValid == true && ToValid == true {
break
}
}
if ToValid != true {
return LangOut{}, errors.New("Target Language Code invalid")
}
if FromValid != true {
return LangOut{}, errors.New("Source language code invalid")
}
2023-06-25 00:10:57 +05:30
UserAgent, ok := os.LookupEnv("SIMPLYTRANSLATE_USER_AGENT")
if !ok {
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}
sc := colly.NewCollector(colly.AllowedDomains("translate.google.com"), colly.UserAgent(UserAgent))
var answer string
sc.OnHTML("div.result-container", func(e *colly.HTMLElement) {
answer = e.Text
})
type Options struct {
2023-07-22 19:54:15 +05:30
To string `url:"tl"`
UI string `url:"hl"`
From string `url:"sl"`
Text string `url:"q"`
}
2023-07-22 19:54:15 +05:30
opt := Options{to, to, from, text}
v, _ := query.Values(opt)
2023-07-22 19:54:15 +05:30
url := "https://translate.google.com/m?" + v.Encode()
2023-06-25 00:10:57 +05:30
sc.Visit(url)
var langout LangOut
langout.OutputText = answer
return langout, nil
2023-06-25 00:10:57 +05:30
}
func TranslateReverso(to string, from string, query string) (LangOut, error) {
var ToValid bool
var FromValid bool
for _, v := range LangListReverso("sl") {
if v.Id == to {
2023-08-15 14:56:57 +05:30
ToValid = true
}
if v.Id == from {
FromValid = true
}
if FromValid == true && ToValid == true {
break
}
}
if ToValid != true {
return LangOut{}, errors.New("Target language code invalid")
}
if FromValid != true {
return LangOut{}, errors.New("Source language code invalid")
}
2023-06-25 00:11:11 +05:30
json := []byte(`{ "format": "text", "from": "` + from + `", "to": "` + to + `", "input":"` + query + `", "options": {"sentenceSplitter": false, "origin":"translation.web", contextResults: false, languageDetection: true} }`)
reversoOut := PostRequest("https://api.reverso.net/translate/v1/translation", json)
2023-06-25 00:10:57 +05:30
gjsonArr := reversoOut.Get("translation").Array()
var langout LangOut
langout.OutputText = gjsonArr[0].String()
return langout, nil
2023-06-25 00:10:57 +05:30
}
func TranslateLibreTranslate(to string, from string, query string) (LangOut, error) {
var ToValid bool
var FromValid bool
for _, v := range LangListLibreTranslate("sl") {
if v.Id == to {
2023-08-15 14:56:57 +05:30
ToValid = true
}
if v.Id == from {
FromValid = true
}
if FromValid == true && ToValid == true {
break
}
}
if ToValid != true {
return LangOut{}, errors.New("Target language code invalid")
}
if FromValid != true {
return LangOut{}, errors.New("Source language code invalid")
}
2023-07-22 19:54:15 +05:30
json := []byte(`{"q":"` + query + `","source":"` + from + `","target":"` + to + `"}`)
2023-07-03 17:29:46 +05:30
// TODO: Make it configurable
libreTranslateOut := PostRequest("https://translate.argosopentech.com/translate", json)
2023-07-03 17:29:46 +05:30
gjsonArr := libreTranslateOut.Get("translatedText").Array()
var langout LangOut
langout.OutputText = gjsonArr[0].String()
return langout, nil
2023-07-03 17:29:46 +05:30
}
func TranslateWatson(to string, from string, query string) (LangOut, error) {
var ToValid bool
var FromValid bool
for _, v := range LangListWatson("sl") {
if v.Id == to {
2023-08-15 14:56:57 +05:30
ToValid = true
}
if v.Id == from {
FromValid = true
}
if FromValid == true && ToValid == true {
break
}
}
if ToValid != true {
return LangOut{}, errors.New("Target language code invalid")
}
if FromValid != true {
return LangOut{}, errors.New("Source language code invalid")
}
2023-07-22 19:54:15 +05:30
json := []byte(`{"text":"` + query + `","source":"` + from + `","target":"` + to + `"}`)
watsonOut := PostRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/text", json)
2023-07-22 11:12:26 +05:30
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
var langout LangOut
langout.OutputText = gjsonArr[0].String()
return langout, nil
2023-07-22 11:12:26 +05:30
}
func TranslateMyMemory(to string, from string, text string) (LangOut, error) {
2023-07-23 11:32:48 +05:30
var ToValid bool
var FromValid bool
for _, v := range LangListMyMemory("sl") {
if v.Id == to {
2023-08-15 14:56:57 +05:30
ToValid = true
2023-07-23 11:32:48 +05:30
}
if v.Id == from {
FromValid = true
}
if FromValid == true && ToValid == true {
break
}
}
if ToValid != true {
return LangOut{}, errors.New("Target language code invalid")
2023-07-23 11:32:48 +05:30
}
if FromValid != true {
return LangOut{}, errors.New("Source language code invalid")
2023-07-23 11:32:48 +05:30
}
type Options struct {
Translate string `url:"langpair"`
Text string `url:"q"`
}
2023-08-15 14:56:57 +05:30
opt := Options{from + "|" + to, text}
2023-07-23 11:32:48 +05:30
v, _ := query.Values(opt)
myMemoryOut := GetRequest("https://api.mymemory.translated.net/get?" + v.Encode())
gjsonArr := myMemoryOut.Get("responseData.translatedText").Array()
var langout LangOut
langout.OutputText = gjsonArr[0].String()
return langout, nil
2023-07-23 11:32:48 +05:30
}
func TranslateYandex(to string, from string, text string) (LangOut, error) {
var ToValid bool
var FromValid bool
for _, v := range LangListYandex("sl") {
if v.Id == to {
2023-08-15 14:56:57 +05:30
ToValid = true
}
if v.Id == from {
FromValid = true
}
if FromValid == true && ToValid == true {
break
}
}
if ToValid != true {
return LangOut{}, errors.New("Target language code invalid")
}
if FromValid != true {
return LangOut{}, errors.New("Source language code invalid")
}
type Options struct {
2023-07-22 19:54:15 +05:30
Translate string `url:"lang"`
Text string `url:"text"`
Srv string `url:"srv"`
Id string `url:"sid"`
}
2023-08-15 14:56:57 +05:30
uuidWithHyphen := uuid.New()
uuid := strings.Replace(uuidWithHyphen.String(), "-", "", -1)
opt := Options{from + "-" + to, text, "android", uuid + "-0-0"}
v, _ := query.Values(opt)
2023-08-15 14:56:57 +05:30
yandexOut := PostRequest("https://translate.yandex.net/api/v1/tr.json/translate?"+v.Encode(), []byte(""))
gjsonArr := yandexOut.Get("text.0").Array()
var langout LangOut
langout.OutputText = gjsonArr[0].String()
return langout, nil
}
func TranslateDeepl(to string, from string, text string) (LangOut, error) {
2023-07-23 20:00:20 +05:30
var ToValid bool
var FromValid bool
for _, v := range LangListDeepl("sl") {
if v.Id == to {
2023-08-15 14:56:57 +05:30
ToValid = true
2023-07-23 20:00:20 +05:30
}
if v.Id == from {
FromValid = true
}
if FromValid == true && ToValid == true {
break
}
}
if ToValid != true {
return LangOut{}, errors.New("Target language code invalid")
2023-07-23 20:00:20 +05:30
}
if FromValid != true {
return LangOut{}, errors.New("Source language code invalid")
2023-07-23 20:00:20 +05:30
}
answer, err := gdeeplx.Translate(text, from, to, 0)
if err != nil {
return LangOut{}, errors.New("failed")
2023-07-23 20:00:20 +05:30
}
answer1 := answer.(map[string]interface{})
2023-08-15 14:56:57 +05:30
ans := answer1["data"].(string)
var langout LangOut
langout.OutputText = ans
return langout, nil
2023-07-23 20:00:20 +05:30
}
func TranslateAll(to string, from string, query string) (string, string, string, string, string, string, string) {
reverso, _ := TranslateReverso(to, from, query)
google, _ := TranslateGoogle(to, from, query)
libretranslate, _ := TranslateLibreTranslate(to, from, query)
watson, _ := TranslateWatson(to, from, query)
mymemory, _ := TranslateMyMemory(to, from, query)
yandex, _ := TranslateYandex(to, from, query)
deepl, _ := TranslateDeepl(to, from, query)
return google.OutputText, reverso.OutputText, libretranslate.OutputText, watson.OutputText, mymemory.OutputText, yandex.OutputText, deepl.OutputText
2023-06-25 00:10:57 +05:30
}