Skip to content

Commit

Permalink
Mandatory translate command line parameter for add command
Browse files Browse the repository at this point in the history
Added context for translations
  • Loading branch information
trezorg committed May 13, 2024
1 parent ccda230 commit b2a8e93
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 31 deletions.
11 changes: 8 additions & 3 deletions internal/fakeapi/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ", "),
)
}
13 changes: 13 additions & 0 deletions internal/slice/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 14 additions & 6 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
36 changes: 18 additions & 18 deletions pkg/api/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/translator/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions pkg/translator/linguleo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit b2a8e93

Please sign in to comment.