From b2a8e93f736cb555348d36dd94c33892626d244e Mon Sep 17 00:00:00 2001 From: Igor Nemilentsev Date: Mon, 13 May 2024 13:31:11 +0300 Subject: [PATCH] Mandatory translate command line parameter for add command Added context for translations --- internal/fakeapi/data.go | 11 ++++++++--- internal/slice/slices.go | 13 +++++++++++++ pkg/api/helpers.go | 20 ++++++++++++++------ pkg/api/items.go | 36 ++++++++++++++++++------------------ pkg/messages/messages.go | 2 ++ pkg/translator/args.go | 1 + pkg/translator/linguleo.go | 8 ++++---- 7 files changed, 60 insertions(+), 31 deletions(-) diff --git a/internal/fakeapi/data.go b/internal/fakeapi/data.go index 13e8066..0ca941f 100644 --- a/internal/fakeapi/data.go +++ b/internal/fakeapi/data.go @@ -26,10 +26,15 @@ var ( ) func CheckResult(t *testing.T, res api.Result, searchWord string, expected []string) { + words := make([]string, 0, len(res.Translate)) + for _, tr := range res.Translate { + words = append(words, tr.Value) + } + assert.Equalf(t, res.Word, searchWord, "Incorrect search word: %s", searchWord) - assert.Len(t, res.Words, 4, "Incorrect number of translated words: %d. Expected: %d", len(res.Words), len(expected)) - assert.Equalf(t, res.Words, expected, "Incorrect translated words order: %s. Expected: %s", + assert.Len(t, res.Translate, 4, "Incorrect number of translated words: %d. Expected: %d", len(res.Translate), len(expected)) + assert.Equalf(t, words, expected, "Incorrect translated words order: %s. Expected: %s", strings.Join(expected, ", "), - strings.Join(res.Words, ", "), + strings.Join(words, ", "), ) } diff --git a/internal/slice/slices.go b/internal/slice/slices.go index 48bddc8..512020e 100644 --- a/internal/slice/slices.go +++ b/internal/slice/slices.go @@ -15,3 +15,16 @@ func Unique[T cmp.Ordered](in []T) []T { } return list } + +func UniqueFunc[T cmp.Ordered, E any](in []E, f func(E) T) []E { + keys := make(map[T]struct{}, len(in)) + var list []E + for _, entry := range in { + key := f(entry) + if _, ok := keys[key]; !ok { + keys[key] = struct{}{} + list = append(list, entry) + } + } + return list +} diff --git a/pkg/api/helpers.go b/pkg/api/helpers.go index dbb7523..3bff1bb 100644 --- a/pkg/api/helpers.go +++ b/pkg/api/helpers.go @@ -44,16 +44,24 @@ func printTranslation(result *Result) { } else { strTitle = "new" } - err := messages.Message(messages.RED, "Found %s word:\n", strTitle) - if err != nil { + if err := messages.Message(messages.RED, "Found %s word:\n", strTitle); err != nil { slog.Error("cannot show message", "error", err) } - err = messages.Message(messages.GREEN, "['%s'] (%s)\n", result.Word, result.Transcription) - if err != nil { + if err := messages.Message(messages.GREEN, "['%s'] (%s)\n", result.Word, result.Transcription); err != nil { slog.Error("cannot show message", "error", err) } - for _, word := range result.Words { - _ = messages.Message(messages.YELLOW, "%s\n", word) + for _, word := range result.Translate { + if err := messages.Message(messages.YELLOW, "%s", word.Value); err != nil { + slog.Error("cannot show message", "error", err) + } + if len(word.Context) > 0 { + if err := messages.Message(messages.WHITE, " (%s)", word.Context); err != nil { + slog.Error("cannot show message", "error", err) + } + } + if err := messages.Message(messages.YELLOW, "\n"); err != nil { + slog.Error("cannot show message", "error", err) + } } } diff --git a/pkg/api/items.go b/pkg/api/items.go index 16fb806..3d2658d 100644 --- a/pkg/api/items.go +++ b/pkg/api/items.go @@ -32,11 +32,13 @@ type apiError struct { // Word translates word structure type Word struct { - Value string `json:"value"` - Picture string `json:"pic_url"` - ID int `json:"id"` - Votes int `json:"votes"` - Exists convertibleBoolean `json:"ut"` + Value string `json:"value"` + Picture string `json:"pic_url"` + Context string `json:"ctx"` + Translate string `json:"tr"` + ID int `json:"id"` + Votes int `json:"votes"` + Exists convertibleBoolean `json:"ut"` } // OperationResult represents operation result @@ -56,15 +58,16 @@ func opResultFromBody(word string, body []byte) OperationResult { // Result represents API response type Result struct { - Word string `json:"-"` - SoundURL string `json:"sound_url"` - Transcription string `json:"transcription"` - ErrorMsg string `json:"error_msg"` - Words []string `json:"-"` - AddWords []string `json:"-"` - Translate []Word `json:"translate"` - Exists convertibleBoolean `json:"is_user"` - DirectionEnglish bool `json:"directionEnglish"` + Word string `json:"-"` + SoundURL string `json:"sound_url"` + Transcription string `json:"transcription"` + ErrorMsg string `json:"error_msg"` + Pos string `json:"pos"` + AddWords []string `json:"-"` + Translate []Word `json:"translate"` + Exists convertibleBoolean `json:"is_user"` + DirectionEnglish bool `json:"directionEnglish"` + InvertTranslateDirection bool `json:"invertTranslateDirection"` } // FromResponse fills TranslationResult from http response @@ -83,13 +86,10 @@ func (result *Result) PrintAddedTranslation() { } func (result *Result) parse() { + result.Translate = slice.UniqueFunc(result.Translate, func(w Word) string { return w.Value }) sort.Slice(result.Translate, func(i, j int) bool { return result.Translate[i].Votes > result.Translate[j].Votes }) - for _, translate := range result.Translate { - result.Words = append(result.Words, translate.Value) - } - result.Words = slice.Unique(result.Words) } // SetTranslation sets custom translation for a word diff --git a/pkg/messages/messages.go b/pkg/messages/messages.go index d2a9695..8f4f8ff 100644 --- a/pkg/messages/messages.go +++ b/pkg/messages/messages.go @@ -16,6 +16,8 @@ const ( GREEN Color = "@{g}" // YELLOW color YELLOW Color = "@{y}" + // WHITE color + WHITE Color = "@{w}" ) // Message shows a message with color package diff --git a/pkg/translator/args.go b/pkg/translator/args.go index 1e36ceb..20d44ef 100644 --- a/pkg/translator/args.go +++ b/pkg/translator/args.go @@ -216,6 +216,7 @@ func prepareArgs(version string) (Lingualeo, error) { Aliases: []string{"t"}, Usage: "Custom translation: lingualeo add -t word1 -t word2 word", Destination: &translate, + Required: true, }, }, Action: func(c *cli.Context) error { diff --git a/pkg/translator/linguleo.go b/pkg/translator/linguleo.go index b13afdb..a9e58f8 100644 --- a/pkg/translator/linguleo.go +++ b/pkg/translator/linguleo.go @@ -164,7 +164,7 @@ func (args *Lingualeo) translateWords(ctx context.Context) <-chan api.OperationR } continue } - if len(res.Result.Words) == 0 { + if len(res.Result.Translate) == 0 { _ = messages.Message(messages.RED, "There are no translations for word: ") err := messages.Message(messages.GREEN, "['%s']\n", res.Result.Word) if err != nil { @@ -317,9 +317,9 @@ func (args *Lingualeo) TranslateWithReverseRussian(ctx context.Context, resultFu if err := resultFunc(result); err != nil { slog.Error("cannot translate word", "word", result.Word, "error", err) } - for _, word := range result.Words { - if args.ReverseTranslate && isEnglishWord(word) { - englishWords = append(englishWords, word) + for _, word := range result.Translate { + if args.ReverseTranslate && isEnglishWord(word.Value) { + englishWords = append(englishWords, word.Value) } } }