Skip to content

Commit

Permalink
Refactored adding tranlation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
trezorg committed May 10, 2024
1 parent ce762bc commit ccda230
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 112 deletions.
55 changes: 3 additions & 52 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
Expand All @@ -12,12 +11,9 @@ import (
"net/http/cookiejar"
"net/http/httputil"
"strconv"
"strings"
"sync"
"time"

"github.com/trezorg/lingualeo/internal/logger"
"github.com/trezorg/lingualeo/pkg/channel"

"golang.org/x/net/publicsuffix"
)
Expand Down Expand Up @@ -206,10 +202,10 @@ func (api *API) translateRequest(word string) ([]byte, error) {
return request("POST", translateURL, api.client, jsonValue, "", api.Debug)
}

func (api *API) addRequest(word string, translate []string) ([]byte, error) {
func (api *API) addRequest(word string, translate string) ([]byte, error) {
values := map[string]string{
"word": word,
"tword": strings.Join(translate, ", "),
"tword": translate,
"port": "1001",
}
jsonValue, _ := json.Marshal(values)
Expand All @@ -224,55 +220,10 @@ func (api *API) TranslateWord(word string) OperationResult {
return opResultFromBody(word, body)
}

func (api *API) AddWord(word string, translate []string) OperationResult {
func (api *API) AddWord(word string, translate string) OperationResult {
body, err := api.addRequest(word, translate)
if err != nil {
return OperationResult{Error: err}
}
return opResultFromBody(word, body)
}

// TranslateWords transate words from string channel
func (api *API) TranslateWords(ctx context.Context, results <-chan string) <-chan OperationResult {
out := make(chan OperationResult)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for word := range channel.OrDone(ctx, results) {
wg.Add(1)
go func(word string) {
defer wg.Done()
out <- api.TranslateWord(word)
}(word)
}
}()
go func() {
defer close(out)
wg.Wait()
}()
return out
}

// AddWords add words
func (api *API) AddWords(ctx context.Context, results <-chan Result) <-chan OperationResult {
out := make(chan OperationResult)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for res := range channel.OrDone(ctx, results) {
wg.Add(1)
result := res
go func(result Result) {
defer wg.Done()
out <- api.AddWord(result.Word, result.Words)
}(result)
}
}()
go func() {
defer close(out)
wg.Wait()
}()
return out
}
4 changes: 3 additions & 1 deletion pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log/slog"
"net/http"
"strings"

"github.com/trezorg/lingualeo/pkg/messages"
)
Expand Down Expand Up @@ -67,7 +68,8 @@ func printAddedTranslation(result *Result) {
if err != nil {
slog.Error("cannot show message", "error", err)
}
err = messages.Message(messages.GREEN, "['%s']\n", result.Word)

err = messages.Message(messages.GREEN, "['%s'] ['%s']\n", result.Word, strings.Join(result.AddWords, ", "))
if err != nil {
slog.Error("cannot show message", "error", err)
}
Expand Down
11 changes: 4 additions & 7 deletions pkg/api/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Result struct {
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"`
Expand Down Expand Up @@ -91,13 +92,9 @@ func (result *Result) parse() {
result.Words = slice.Unique(result.Words)
}

// SetTranslate sets custom translation for a word
func (result *Result) SetTranslate(translates []string, replace bool) {
if replace {
result.Words = slice.Unique(translates)
} else {
result.Words = slice.Unique(append(result.Words, translates...))
}
// SetTranslation sets custom translation for a word
func (result *Result) SetTranslation(translates []string) {
result.AddWords = slice.Unique(translates)
}

// InDictionary checks either word is already has been added into the dictionary
Expand Down
40 changes: 10 additions & 30 deletions pkg/translator/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ import (
"gopkg.in/yaml.v2"
)

type configType int
type decodeFunc func(data []byte, args *Lingualeo) error
type (
configType int
decodeFunc func(data []byte, args *Lingualeo) error
)

const (
yamlType = configType(1)
jsonType = configType(2)
tomlType = configType(3)
)

var (
decodeMapping = map[configType]decodeFunc{
yamlType: readYamlConfig,
jsonType: readJSONConfig,
tomlType: readTomlConfig,
}
)
var decodeMapping = map[configType]decodeFunc{
yamlType: readYamlConfig,
jsonType: readJSONConfig,
tomlType: readTomlConfig,
}

type configFile struct {
filename string
Expand Down Expand Up @@ -73,7 +73,6 @@ func newConfigFile(filename string) *configFile {
}

func prepareArgs(version string) (Lingualeo, error) {

args := Lingualeo{}

var translate cli.StringSlice
Expand All @@ -87,7 +86,7 @@ func prepareArgs(version string) (Lingualeo, error) {
return fmt.Errorf("there are no words to translate")
}
args.Words = slice.Unique(c.Args().Slice())
args.Translation = translate.Value()
args.Translation = slice.Unique(translate.Value())
if args.Add && len(args.Translation) > 0 && len(args.Words) > 1 {
return fmt.Errorf("you should add only one word with custom translation")
}
Expand Down Expand Up @@ -212,18 +211,6 @@ func prepareArgs(version string) (Lingualeo, error) {
Aliases: []string{"a"},
Usage: "Add to lingualeo dictionary",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Force add to lingualeo dictionary",
Destination: &args.Force,
},
&cli.BoolFlag{
Name: "replace",
Aliases: []string{"r"},
Usage: "Custom translation. Replace word instead of adding",
Destination: &args.TranslateReplace,
},
&cli.StringSliceFlag{
Name: "translate",
Aliases: []string{"t"},
Expand All @@ -242,7 +229,6 @@ func prepareArgs(version string) (Lingualeo, error) {
return args, err
}
return args, nil

}

func readTomlConfig(data []byte, args *Lingualeo) error {
Expand Down Expand Up @@ -343,9 +329,6 @@ func (args *Lingualeo) mergeConfigs(a *Lingualeo) {
if len(args.Player) == 0 && len(a.Player) > 0 {
args.Player = a.Player
}
if a.Force {
args.Force = a.Force
}
if a.Add {
args.Add = a.Add
}
Expand All @@ -364,9 +347,6 @@ func (args *Lingualeo) mergeConfigs(a *Lingualeo) {
if a.ReverseTranslate {
args.ReverseTranslate = a.ReverseTranslate
}
if a.TranslateReplace {
args.TranslateReplace = a.TranslateReplace
}
if len(args.LogLevel) == 0 && len(a.LogLevel) > 0 {
args.LogLevel = a.LogLevel
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/translator/get_word_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestProcessTranslationResponseJson(t *testing.T) {
downloader.EXPECT().Download(fakeapi.SoundURL).Return(testFile, nil).Times(count)
translator.EXPECT().TranslateWord(fakeapi.SearchWord).Return(res).Times(count)

logger.Prepare(slog.LevelError + 1)
logger.Prepare(slog.LevelError + 10)
searchWords := make([]string, 0, count)

for i := 0; i < count; i++ {
Expand Down
30 changes: 15 additions & 15 deletions pkg/translator/linguleo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (args *Lingualeo) checkMediaPlayer() {
//go:generate mockery
type Translator interface {
TranslateWord(word string) api.OperationResult
AddWord(word string, translate []string) api.OperationResult
AddWord(word string, translate string) api.OperationResult
}

type Lingualeo struct {
Expand All @@ -54,11 +54,9 @@ type Lingualeo struct {
Words []string
Translation []string
Add bool `yaml:"add" json:"add" toml:"add"`
TranslateReplace bool `yaml:"translate_replace" json:"translate_replace" toml:"translate_replace"`
Sound bool `yaml:"sound" json:"sound" toml:"sound"`
Debug bool `yaml:"debug" json:"debug" toml:"debug"`
DownloadSoundFile bool `yaml:"download" json:"download" toml:"download"`
Force bool `yaml:"force" json:"force" toml:"force"`
LogPrettyPrint bool `yaml:"log_pretty_print" json:"log_pretty_print" toml:"log_pretty_print"`
ReverseTranslate bool `yaml:"reverse_translate" json:"reverse_translate" toml:"reverse_translate"`
}
Expand Down Expand Up @@ -129,12 +127,16 @@ func addWords(ctx context.Context, translator Translator, results <-chan api.Res
go func() {
defer wg.Done()
for res := range channel.OrDone(ctx, results) {
wg.Add(1)
result := res
go func(result api.Result) {
defer wg.Done()
out <- translator.AddWord(result.Word, result.Words)
}(result)
for _, translate := range res.AddWords {
wg.Add(1)
result := res
go func(word, transate string) {
defer wg.Done()
added := translator.AddWord(word, transate)
added.Result.AddWords = []string{transate}
out <- added
}(result.Word, translate)
}
}
}()
go func() {
Expand Down Expand Up @@ -177,11 +179,9 @@ func (args *Lingualeo) translateWords(ctx context.Context) <-chan api.OperationR
}

func (args *Lingualeo) prepareResultToAdd(result *api.Result) bool {
if !result.InDictionary() || args.Force {
// Custom translation
if len(args.Translation) > 0 {
result.SetTranslate(args.Translation, args.TranslateReplace)
}
// Custom translation
if len(args.Translation) > 0 {
result.SetTranslation(args.Translation)
return true
}
return false
Expand Down Expand Up @@ -244,7 +244,7 @@ func (args *Lingualeo) AddToDictionary(ctx context.Context, resultsToAdd <-chan
// Process starts translation process
func (args *Lingualeo) Process(ctx context.Context, wg *sync.WaitGroup) (<-chan string, <-chan api.Result, <-chan api.Result) {
soundChan := make(chan string, len(args.Words))
addWordChan := make(chan api.Result, len(args.Words))
addWordChan := make(chan api.Result, len(args.Translation))
resultsChan := make(chan api.Result, len(args.Words))

go func() {
Expand Down
12 changes: 6 additions & 6 deletions pkg/translator/mock_Translator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ccda230

Please sign in to comment.