trans-alfred/trans/trans.go
2019-10-13 18:09:48 +02:00

104 lines
2.2 KiB
Go

package trans
import (
"fmt"
"os/exec"
"strings"
"regexp"
)
type Translation struct {
Word string
Language string
Translator func(string, string, string) (string, error)
Identifier func(string) (string, error)
}
func New(word string) Translation {
return Translation{
Word: word,
Language: "",
Translator: executeTrans,
Identifier: executeIdentify,
}
}
func executeTrans(word, fromLang, toLang string) (string, error) {
config := []string{
"-no-ansi",
"-show-original", "n",
"-show-original-phonetics", "n",
"-show-dictionary", "y",
"-show-languages", "n",
"-show-prompt-message", "n",
fmt.Sprintf("%s:%s", fromLang, toLang),
word,
}
outBytes, err := exec.Command("trans", config...).Output()
return string(outBytes), err
}
func (t Translation) Translate(toLang string) []string {
output, err := t.Translator(t.Word, t.Language, toLang)
if err != nil {
panic(err)
}
return parseTransOutput(output)
}
func parseTransOutput(out string) []string {
re := regexp.MustCompile(`(?m)^\s{4}\w.*$`)
lineMatches := re.FindAllString(out, -1)
results := []string{strings.Split(out, "\n")[0]}
for _, line := range lineMatches {
for _, word := range strings.Split(strings.TrimSpace(line), ",") {
results = append(results, strings.TrimSpace(word))
}
}
return uniqueSlice(results)
}
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
}
func executeIdentify(word string) (string, error) {
config := []string{
"-no-ansi",
"-id",
word,
}
outBytes, err := exec.Command("trans", config...).Output()
return string(outBytes), err
}
func (t Translation) Identify() string {
output, err := t.Identifier(t.Word)
if err != nil {
panic(err)
}
return parseTransIdentifyOutput(output)
}
func parseTransIdentifyOutput(out string) string {
re := regexp.MustCompile(`(?m)^Code\s+(\w+)$`)
result := ""
matches := re.FindStringSubmatch(out)
if (len(matches) >= 2) {
result = matches[1]
}
return result
}