From 3bbada446186f8f906c6c4df660f2600c7252b4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 9 Oct 2019 21:58:25 +0200 Subject: [PATCH] Include more translations in result list --- trans/trans.go | 18 ++++++++------ trans/trans_test.go | 59 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/trans/trans.go b/trans/trans.go index 784877b..ceb8e49 100644 --- a/trans/trans.go +++ b/trans/trans.go @@ -5,6 +5,7 @@ import ( "os/exec" "strings" "log" + "regexp" ) @@ -35,7 +36,7 @@ func executeTransShell(tj TransJob) ([]byte, error) { "-no-ansi", "-show-original", "n", "-show-original-phonetics", "n", - "-show-dictionary", "n", + "-show-dictionary", "y", "-show-languages", "n", "-show-prompt-message", "n", fmt.Sprintf("%s:%s", tj.fromLang, tj.toLang), @@ -53,15 +54,16 @@ func (t Translation) Translate() []string { } func parseTransOutput(out string) []string { - outLines := strings.Split(out, "\n") - translation := outLines[0] + re := regexp.MustCompile(`(?m)^\s{4}\w.*$`) + lineMatches := re.FindAllString(out, -1) - additionalTranslations := strings.Split(outLines[len(outLines)-2], ",") - for i, translation := range additionalTranslations { - additionalTranslations[i] = strings.TrimSpace(translation) + 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 append([]string{translation}, additionalTranslations...) + return results } func uniqueSlice(items []string) []string { diff --git a/trans/trans_test.go b/trans/trans_test.go index ae4defc..e9c9a77 100644 --- a/trans/trans_test.go +++ b/trans/trans_test.go @@ -16,13 +16,62 @@ type testData struct { func TestTranslation(t *testing.T) { cases := []testData{ testData{ - []string{"actuator", "en", "hu"}, - []byte(`működtető +[]string{"actuator", "en", "hu"}, +[]byte(`működtető + +noun + indítókar + actuator, starting lever 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ő", "beavatkozó", "hajtómű", "aktuátor", "hajtás"}, +[]string{"működtető", "indítókar", "beavatkozó", "hajtómű", "aktuátor", "hajtás"}, + }, + testData{ +[]string{"szaxofon", "hu", "en"}, +[]byte(`saxophone + +szaxofon + saxophone +`), +[]string{"saxophone"}, + }, + testData{ +[]string{"cat", "en", "hu"}, +[]byte(`macska + +noun + macska + cat, pussy + pletykás nő + cat + +verb + felvon + hoist, cat + +cat + macska, cica +`), +[]string{"macska", "pletykás nő", "felvon", "cica"}, + }, + testData{ +[]string{"méltányosság", "hu", "en"}, +[]byte(`equity + +noun + equity + méltányosság, jogosság + fairness + méltányosság, szőkeség + justness + jogosság, méltányosság + +méltányosság + equity, fairness +`), +[]string{"equity", "fairness", "justness"}, }, } @@ -33,7 +82,7 @@ actuator } r := j.Translate() if (!reflect.DeepEqual(r, data.expectedResults)) { - t.Error() + t.Errorf("\nExpected: '%v'\nActual: '%v'", data.expectedResults, r) } } }