Implement first POC
This commit is contained in:
commit
87320b19b8
56
trans.go
Normal file
56
trans.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
outBytes, err := execTrans(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
translations := parseTransOutput(string(outBytes))
|
||||||
|
fmt.Println(strings.Join(uniqueSlice(translations), "\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func execTrans(input string) ([]byte, error) {
|
||||||
|
config := []string{
|
||||||
|
"-show-original", "n",
|
||||||
|
"-show-original-phonetics", "n",
|
||||||
|
"-show-dictionary", "n",
|
||||||
|
"-show-languages", "n",
|
||||||
|
"-show-prompt-message", "n",
|
||||||
|
"hu:en",
|
||||||
|
input,
|
||||||
|
}
|
||||||
|
return exec.Command("trans", config...).Output()
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseTransOutput(out string) []string {
|
||||||
|
outLines := strings.Split(out, "\n")
|
||||||
|
translation := outLines[0]
|
||||||
|
|
||||||
|
additionalTranslations := strings.Split(outLines[len(outLines)-2], ",")
|
||||||
|
for i, translation := range additionalTranslations {
|
||||||
|
additionalTranslations[i] = strings.TrimSpace(translation)
|
||||||
|
}
|
||||||
|
|
||||||
|
return append([]string{translation}, additionalTranslations...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func uniqueSlice(items []string) []string {
|
||||||
|
encountered := map[string]bool{}
|
||||||
|
|
||||||
|
uniqueSlice := []string{}
|
||||||
|
for _, item := range items {
|
||||||
|
if !encountered[item] {
|
||||||
|
encountered[item] = true
|
||||||
|
uniqueSlice = append(uniqueSlice, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return uniqueSlice
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user