add IBM Watson support

This commit is contained in:
Arya 2023-07-22 11:12:26 +05:30
parent 1cf822eb80
commit cf02a689d3
Signed by: arya
GPG Key ID: 842D12BDA50DF120
4 changed files with 275 additions and 26 deletions

View File

@ -41,6 +41,14 @@ var translateCmd = &cobra.Command{
} else {
fmt.Println(utils.TranslateLibreTranslate(dest, source, query))
}
} else if engine == "watson" {
if langlist == "sl" {
fmt.Println(utils.LangListWatson(langlist))
} else if langlist == "tl" {
fmt.Println(utils.LangListWatson(langlist))
} else {
fmt.Println(utils.TranslateWatson(dest, source, query))
}
} else if engine == "all" {
fmt.Println(utils.TranslateAll(dest, source, query))
} else {

View File

@ -19,6 +19,8 @@ func HandleSourceLanguages(c *fiber.Ctx) error {
data = utils.LangListReverso("sl")
} else if engine == "deepl" {
data = utils.LangListDeepl("sl")
} else if engine == "watson" {
data = utils.LangListWatson("sl")
}
return c.JSON(data)
}
@ -36,6 +38,8 @@ func HandleTargetLanguages(c *fiber.Ctx) error {
data = utils.LangListReverso("tl")
} else if engine == "deepl" {
data = utils.LangListDeepl("tl")
} else if engine == "watson" {
data = utils.LangListWatson("tl")
}
return c.JSON(data)
}

View File

@ -34,6 +34,13 @@ func TranslateLibreTranslate(to string, from string, query string) string {
answer := gjsonArr[0].String()
return answer
}
func TranslateWatson(to string, from string, query string) string {
json := []byte(`{"text":"`+query+`","source":"`+from+`","target":"`+to+`"}`)
watsonOut:= GetRequest("https://www.ibm.com/demos/live/watson-language-translator/api/translate/text", json)
gjsonArr := watsonOut.Get("payload.translations.0.translation").Array()
answer := gjsonArr[0].String()
return answer
}
func TranslateAll(to string, from string, query string) string {
reverso := TranslateReverso(to, from, query)
google := TranslateGoogle(to, from, query)

View File

@ -54,67 +54,297 @@ func LangListLibreTranslate(listType string) []List {
}
return ListData
}
func LangListWatson(listType string) []List {
// IDs got from https://www.loc.gov/standards/iso639-2/php/code_list.php and tested to make sure they work. Exceptions fr-CA zh-CN/TW
var ListData = []List{
List{
Id: "ar",
Name: "Arabic",
},
List{
Id: "ba",
Name: "Basque",
},
List{
Id: "bn",
Name: "Bengali",
},
List{
Id: "bs",
Name: "Bosnian",
},
List{
Id: "bg",
Name: "Bulgarian",
},
List{
Id: "ca",
Name: "Catalan",
},
List{
Id: "zh",
Name: "Chinese (Simplified)",
},
List{
Id: "zh-TW",
Name: "Chinese (Traditional)",
},
List{
Id: "hr",
Name: "Croatian",
},
List{
Id: "cs",
Name: "Czech",
},
List{
Id: "da",
Name: "Danish",
},
List{
Id: "nl",
Name: "Dutch",
},
List{
Id: "et",
Name: "Estonian",
},
List{
Id: "fi",
Name: "Finnish",
},
List{
Id: "fr",
Name: "French",
},
List{
Id: "fr-CA",
Name: "Canadian French",
},
List{
Id: "de",
Name: "German",
},
List{
Id: "el",
Name: "Greek",
},
List{
Id: "gu",
Name: "Gujarati",
},
List{
Id: "he",
Name: "Hebrew",
},
List{
Id: "hi",
Name: "Hindi",
},
List{
Id: "hu",
Name: "Hungarian",
},
List{
Id: "id",
Name: "Indonesian",
},
List{
Id: "ga",
Name: "Irish",
},
List{
Id: "it",
Name: "Italian",
},
List{
Id: "ja",
Name: "Japanese",
},
List{
Id: "kn",
Name: "Kannada",
},
List{
Id: "ko",
Name: "Korean",
},
List{
Id: "lv",
Name: "Latvian",
},
List{
Id: "lt",
Name: "Lithuanian",
},
List{
Id: "ms",
Name: "Malay",
},
List{
Id: "ml",
Name: "Malayalam",
},
List{
Id: "mt",
Name: "Maltese",
},
List{
Id: "mr",
Name: "Marathi",
},
List{
Id: "cnr",
Name: "Montenegrin",
},
List{
Id: "ne",
Name: "Nepali",
},
List{
Id: "nb",
Name: "Norwegian",
},
List{
Id: "pl",
Name: "Polish",
},
List{
Id: "pt",
Name: "Portugese",
},
List{
Id: "pa",
Name: "Punjabi",
},
List{
Id: "ro",
Name: "Romanian",
},
List{
Id: "ru",
Name: "Russian",
},
List{
Id: "sr",
Name: "Serbian",
},
List{
Id: "si",
Name: "Sinhalese",
},
List{
Id: "sk",
Name: "Slovak",
},
List{
Id: "sl",
Name: "Slovenian",
},
List{
Id: "es",
Name: "Spanish",
},
List{
Id: "sv",
Name: "Swedish",
},
List{
Id: "ta",
Name: "Tamil",
},
List{
Id: "te",
Name: "Telugu",
},
List{
Id: "th",
Name: "Thai",
},
List{
Id: "tr",
Name: "Turkish",
},
List{
Id: "uk",
Name: "Ukrainian",
},
List{
Id: "ur",
Name: "Urdu",
},
List{
Id: "vi",
Name: "Vietnamese",
},
List{
Id: "cy",
Name: "Welsh",
},
}
return ListData
}
func LangListReverso(listType string) []List {
// IDs got from original simplytranslate-web and trial and error. Usually first three letters of language.
var ListData = []List{
List{
Id: "ara",
Id: "ar",
Name: "Arabic",
},
List{
Id: "chi",
Id: "zh",
Name: "Chinese (Simplified)",
},
List{
Id: "cze",
Id: "cs",
Name: "Czech",
},
List{
Id: "dan",
Id: "da",
Name: "Danish",
},
List{
Id: "dut",
Id: "nl",
Name: "Dutch",
},
List{
Id: "eng",
Id: "en",
Name: "English",
},
List{
Id: "fra",
Id: "fr",
Name: "French",
},
List{
Id: "ger",
Id: "de",
Name: "German",
},
List{
Id: "gre",
Id: "el",
Name: "Greek",
},
List{
Id: "heb",
Id: "he",
Name: "Hebrew",
},
List{
Id: "hin",
Id: "hi",
Name: "Hindi",
},
List{
Id: "hun",
Id: "hu",
Name: "Hungarian",
},
List{
Id: "ita",
Id: "it",
Name: "Italian",
},
List{
Id: "jpn",
Id: "ja",
Name: "Japanese",
},
List{
Id: "kor",
Id: "ko",
Name: "Korean",
},
List{
@ -122,43 +352,43 @@ func LangListReverso(listType string) []List {
Name: "Persian",
},
List{
Id: "pol",
Id: "pl",
Name: "Polish",
},
List{
Id: "por",
Id: "pt",
Name: "Portugese",
},
List{
Id: "rum",
Id: "ro",
Name: "Romanian",
},
List{
Id: "rus",
Id: "ru",
Name: "Russian",
},
List{
Id: "slo",
Name: "Slovakian",
Id: "sk",
Name: "Slovak",
},
List{
Id: "spa",
Id: "es",
Name: "Spanish",
},
List{
Id: "swe",
Id: "sv",
Name: "Swedish",
},
List{
Id: "tha",
Id: "th",
Name: "Thai",
},
List{
Id: "tur",
Id: "tr",
Name: "Turkish",
},
List{
Id: "ukr",
Id: "uk",
Name: "Ukrainian",
},
}