Include more translations in result list
This commit is contained in:
		@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user