Refactor and improve API
This commit is contained in:
parent
a6f5cd6268
commit
3ad5e57d84
@ -12,7 +12,7 @@ type Translation struct {
|
|||||||
word string
|
word string
|
||||||
fromLang string
|
fromLang string
|
||||||
toLang string
|
toLang string
|
||||||
Executor func(*Translation) ([]byte, error)
|
Translator func(*Translation) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(word, fromLang, toLang string) Translation {
|
func New(word, fromLang, toLang string) Translation {
|
||||||
@ -20,11 +20,11 @@ func New(word, fromLang, toLang string) Translation {
|
|||||||
word: word,
|
word: word,
|
||||||
fromLang: fromLang,
|
fromLang: fromLang,
|
||||||
toLang: toLang,
|
toLang: toLang,
|
||||||
Executor: executeTransShell,
|
Translator: executeTransShell,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeTransShell(t *Translation) ([]byte, error) {
|
func executeTransShell(t *Translation) (string, error) {
|
||||||
config := []string{
|
config := []string{
|
||||||
"-no-ansi",
|
"-no-ansi",
|
||||||
"-show-original", "n",
|
"-show-original", "n",
|
||||||
@ -35,15 +35,16 @@ func executeTransShell(t *Translation) ([]byte, error) {
|
|||||||
fmt.Sprintf("%s:%s", t.fromLang, t.toLang),
|
fmt.Sprintf("%s:%s", t.fromLang, t.toLang),
|
||||||
t.word,
|
t.word,
|
||||||
}
|
}
|
||||||
return exec.Command("trans", config...).Output()
|
outBytes, err := exec.Command("trans", config...).Output()
|
||||||
|
return string(outBytes), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Translation) Translate() []string {
|
func (t Translation) Translate() []string {
|
||||||
outBytes, err := t.Executor(&t)
|
output, err := t.Translator(&t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return uniqueSlice(parseTransOutput(string(outBytes)))
|
return uniqueSlice(parseTransOutput(output))
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTransOutput(out string) []string {
|
func parseTransOutput(out string) []string {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
type testData struct {
|
type testData struct {
|
||||||
job []string
|
job []string
|
||||||
transOutput []byte
|
transOutput string
|
||||||
expectedResults []string
|
expectedResults []string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ func TestTranslation(t *testing.T) {
|
|||||||
cases := []testData{
|
cases := []testData{
|
||||||
testData{
|
testData{
|
||||||
[]string{"actuator", "en", "hu"},
|
[]string{"actuator", "en", "hu"},
|
||||||
[]byte(`működtető
|
`működtető
|
||||||
|
|
||||||
noun
|
noun
|
||||||
indítókar
|
indítókar
|
||||||
@ -25,21 +25,21 @@ noun
|
|||||||
|
|
||||||
actuator
|
actuator
|
||||||
működtető, beavatkozó, hajtómű, aktuátor, hajtás
|
működtető, beavatkozó, hajtómű, aktuátor, hajtás
|
||||||
`),
|
`,
|
||||||
[]string{"működtető", "indítókar", "beavatkozó", "hajtómű", "aktuátor", "hajtás"},
|
[]string{"működtető", "indítókar", "beavatkozó", "hajtómű", "aktuátor", "hajtás"},
|
||||||
},
|
},
|
||||||
testData{
|
testData{
|
||||||
[]string{"szaxofon", "hu", "en"},
|
[]string{"szaxofon", "hu", "en"},
|
||||||
[]byte(`saxophone
|
`saxophone
|
||||||
|
|
||||||
szaxofon
|
szaxofon
|
||||||
saxophone
|
saxophone
|
||||||
`),
|
`,
|
||||||
[]string{"saxophone"},
|
[]string{"saxophone"},
|
||||||
},
|
},
|
||||||
testData{
|
testData{
|
||||||
[]string{"cat", "en", "hu"},
|
[]string{"cat", "en", "hu"},
|
||||||
[]byte(`macska
|
`macska
|
||||||
|
|
||||||
noun
|
noun
|
||||||
macska
|
macska
|
||||||
@ -53,12 +53,12 @@ verb
|
|||||||
|
|
||||||
cat
|
cat
|
||||||
macska, cica
|
macska, cica
|
||||||
`),
|
`,
|
||||||
[]string{"macska", "pletykás nő", "felvon", "cica"},
|
[]string{"macska", "pletykás nő", "felvon", "cica"},
|
||||||
},
|
},
|
||||||
testData{
|
testData{
|
||||||
[]string{"méltányosság", "hu", "en"},
|
[]string{"méltányosság", "hu", "en"},
|
||||||
[]byte(`equity
|
`equity
|
||||||
|
|
||||||
noun
|
noun
|
||||||
equity
|
equity
|
||||||
@ -70,14 +70,14 @@ noun
|
|||||||
|
|
||||||
méltányosság
|
méltányosság
|
||||||
equity, fairness
|
equity, fairness
|
||||||
`),
|
`,
|
||||||
[]string{"equity", "fairness", "justness"},
|
[]string{"equity", "fairness", "justness"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, data := range cases {
|
for _, data := range cases {
|
||||||
j := trans.New(data.job[0], data.job[1], data.job[2])
|
j := trans.New(data.job[0], data.job[1], data.job[2])
|
||||||
j.Executor = func(t *trans.Translation) ([]byte, error) {
|
j.Translator = func(t *trans.Translation) (string, error) {
|
||||||
return data.transOutput, nil
|
return data.transOutput, nil
|
||||||
}
|
}
|
||||||
r := j.Translate()
|
r := j.Translate()
|
||||||
|
Loading…
Reference in New Issue
Block a user