add IBM Watson support

This commit is contained in:
Arya Kiran 2023-07-22 11:12:26 +05:30
parent 1cf822eb80
commit cf02a689d3
No known key found for this signature in database
GPG key ID: 842D12BDA50DF120
4 changed files with 275 additions and 26 deletions

View file

@ -41,6 +41,14 @@ var translateCmd = &cobra.Command{
} else { } else {
fmt.Println(utils.TranslateLibreTranslate(dest, source, query)) 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" { } else if engine == "all" {
fmt.Println(utils.TranslateAll(dest, source, query)) fmt.Println(utils.TranslateAll(dest, source, query))
} else { } else {

View file

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

View file

@ -34,6 +34,13 @@ func TranslateLibreTranslate(to string, from string, query string) string {
answer := gjsonArr[0].String() answer := gjsonArr[0].String()
return answer 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 { func TranslateAll(to string, from string, query string) string {
reverso := TranslateReverso(to, from, query) reverso := TranslateReverso(to, from, query)
google := TranslateGoogle(to, from, query) google := TranslateGoogle(to, from, query)

View file

@ -54,67 +54,297 @@ func LangListLibreTranslate(listType string) []List {
} }
return ListData 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 { func LangListReverso(listType string) []List {
// IDs got from original simplytranslate-web and trial and error. Usually first three letters of language. // IDs got from original simplytranslate-web and trial and error. Usually first three letters of language.
var ListData = []List{ var ListData = []List{
List{ List{
Id: "ara", Id: "ar",
Name: "Arabic", Name: "Arabic",
}, },
List{ List{
Id: "chi", Id: "zh",
Name: "Chinese (Simplified)", Name: "Chinese (Simplified)",
}, },
List{ List{
Id: "cze", Id: "cs",
Name: "Czech", Name: "Czech",
}, },
List{ List{
Id: "dan", Id: "da",
Name: "Danish", Name: "Danish",
}, },
List{ List{
Id: "dut", Id: "nl",
Name: "Dutch", Name: "Dutch",
}, },
List{ List{
Id: "eng", Id: "en",
Name: "English", Name: "English",
}, },
List{ List{
Id: "fra", Id: "fr",
Name: "French", Name: "French",
}, },
List{ List{
Id: "ger", Id: "de",
Name: "German", Name: "German",
}, },
List{ List{
Id: "gre", Id: "el",
Name: "Greek", Name: "Greek",
}, },
List{ List{
Id: "heb", Id: "he",
Name: "Hebrew", Name: "Hebrew",
}, },
List{ List{
Id: "hin", Id: "hi",
Name: "Hindi", Name: "Hindi",
}, },
List{ List{
Id: "hun", Id: "hu",
Name: "Hungarian", Name: "Hungarian",
}, },
List{ List{
Id: "ita", Id: "it",
Name: "Italian", Name: "Italian",
}, },
List{ List{
Id: "jpn", Id: "ja",
Name: "Japanese", Name: "Japanese",
}, },
List{ List{
Id: "kor", Id: "ko",
Name: "Korean", Name: "Korean",
}, },
List{ List{
@ -122,43 +352,43 @@ func LangListReverso(listType string) []List {
Name: "Persian", Name: "Persian",
}, },
List{ List{
Id: "pol", Id: "pl",
Name: "Polish", Name: "Polish",
}, },
List{ List{
Id: "por", Id: "pt",
Name: "Portugese", Name: "Portugese",
}, },
List{ List{
Id: "rum", Id: "ro",
Name: "Romanian", Name: "Romanian",
}, },
List{ List{
Id: "rus", Id: "ru",
Name: "Russian", Name: "Russian",
}, },
List{ List{
Id: "slo", Id: "sk",
Name: "Slovakian", Name: "Slovak",
}, },
List{ List{
Id: "spa", Id: "es",
Name: "Spanish", Name: "Spanish",
}, },
List{ List{
Id: "swe", Id: "sv",
Name: "Swedish", Name: "Swedish",
}, },
List{ List{
Id: "tha", Id: "th",
Name: "Thai", Name: "Thai",
}, },
List{ List{
Id: "tur", Id: "tr",
Name: "Turkish", Name: "Turkish",
}, },
List{ List{
Id: "ukr", Id: "uk",
Name: "Ukrainian", Name: "Ukrainian",
}, },
} }