Skip to content

Commit

Permalink
Merge pull request #9 from michimani/issue/1/support-more-translate-o…
Browse files Browse the repository at this point in the history
…ptions

Draft: Issue/1/support more translate options
  • Loading branch information
michimani authored Aug 7, 2021
2 parents 7303a77 + 611dc17 commit 8d63e64
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 17 deletions.
19 changes: 16 additions & 3 deletions params/translate_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type TranslateTextParams struct {
Text []string
SourceLang types.SourceLangCode
TargetLang types.TargetLangCode
SplitSentences interface{}
PreserveFormatting interface{}
Formality string
SplitSentences types.SplitSentences
PreserveFormatting types.PreserveFormatting
Formality types.Formality
}

func (p *TranslateTextParams) SetAuthnKey(k string) {
Expand All @@ -35,10 +35,23 @@ func (p *TranslateTextParams) Body() (*strings.Reader, error) {
uv.Add("text", t)
}
uv.Add("target_lang", string(p.TargetLang))

if p.SourceLang != "" {
uv.Add("source_lang", string(p.SourceLang))
}

if p.SplitSentences.Valid() {
uv.Add("split_sentences", string(p.SplitSentences))
}

if p.PreserveFormatting.Valid() {
uv.Add("preserve_formatting", string(p.PreserveFormatting))
}

if p.Formality.Valid(p.TargetLang) {
uv.Add("formality", string(p.Formality))
}

return strings.NewReader(uv.Encode()), nil
}

Expand Down
54 changes: 43 additions & 11 deletions params/translate_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,54 @@ func TestTranslateTextBody(t *testing.T) {
{
name: "normal: all",
params: params.TranslateTextParams{
AuthKey: "test-authn-key",
Text: []string{"text1", "text2"},
SourceLang: types.SourceLangEN,
TargetLang: types.TargetLangJA,
AuthKey: "test-authn-key",
Text: []string{"text1", "text2"},
SourceLang: types.SourceLangEN,
TargetLang: types.TargetLangRU,
SplitSentences: types.SplitSentencesNoSplit,
PreserveFormatting: types.PreserveFormattingDisabled,
Formality: types.FormalityMore,
},
expect: "auth_key=test-authn-key&source_lang=EN&target_lang=JA&text=text1&text=text2",
expect: "auth_key=test-authn-key&formality=more&preserve_formatting=0&source_lang=EN&split_sentences=0&target_lang=RU&text=text1&text=text2",
},
{
name: "normal: all",
name: "normal: all with white space",
params: params.TranslateTextParams{
AuthKey: "test key",
Text: []string{"text 1", "text 2"},
SourceLang: types.SourceLangEN,
TargetLang: types.TargetLangRU,
SplitSentences: types.SplitSentencesSplit,
PreserveFormatting: types.PreserveFormattingEnabled,
Formality: types.FormalityMore,
},
expect: "auth_key=test+key&formality=more&preserve_formatting=1&source_lang=EN&split_sentences=1&target_lang=RU&text=text+1&text=text+2",
},
{
name: "normal: all with invalid value",
params: params.TranslateTextParams{
AuthKey: "test key",
Text: []string{"text 1", "text 2"},
SourceLang: types.SourceLangEN,
TargetLang: types.TargetLangRU,
SplitSentences: "invalid value",
PreserveFormatting: "invalid value",
Formality: "invalid value",
},
expect: "auth_key=test+key&source_lang=EN&target_lang=RU&text=text+1&text=text+2",
},
{
name: "normal: all ignore formality option",
params: params.TranslateTextParams{
AuthKey: "test key",
Text: []string{"text 1", "text 2"},
SourceLang: types.SourceLangEN,
TargetLang: types.TargetLangJA,
AuthKey: "test-authn-key",
Text: []string{"text1", "text2"},
SourceLang: types.SourceLangEN,
TargetLang: types.TargetLangJA,
SplitSentences: types.SplitSentencesNoSplit,
PreserveFormatting: types.PreserveFormattingDisabled,
Formality: types.FormalityMore,
},
expect: "auth_key=test+key&source_lang=EN&target_lang=JA&text=text+1&text=text+2",
expect: "auth_key=test-authn-key&preserve_formatting=0&source_lang=EN&split_sentences=0&target_lang=JA&text=text1&text=text2",
},
{
name: "normal: empty",
Expand Down
8 changes: 5 additions & 3 deletions sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ func translateText(c *deepl.Client) {
fmt.Println("TranslateTextAPI sample (JA to EN)")
text := []string{
"こんにちは",
"これはサンプルテキストです。",
"これは\nサンプルテキストです。",
}
params := &params.TranslateTextParams{
TargetLang: types.TargetLangEN,
Text: text,
TargetLang: types.TargetLangEN,
Text: text,
SplitSentences: types.SplitSentencesNoSplit,
PreserveFormatting: types.PreserveFormattingDisabled,
}

res, errRes, err := c.TranslateText(context.TODO(), params)
Expand Down
76 changes: 76 additions & 0 deletions types/translate_text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package types

type SplitSentences string

const (
// no splitting at all, whole input is treated as one sentence
SplitSentencesNoSplit = "0"

// splits on interpunction and on newlines (default)
SplitSentencesSplit = "1"

// splits on interpunction only, ignoring newlines
SplitSentencesNoNewLines = "nonewlines"
)

func (s SplitSentences) Valid() bool {
if s != SplitSentencesNoSplit && s != SplitSentencesSplit && s != SplitSentencesNoNewLines {
return false
}

return true
}

type PreserveFormatting string

const (
// default
PreserveFormattingDisabled = "0"

PreserveFormattingEnabled = "1"
)

func (p PreserveFormatting) Valid() bool {
if p != PreserveFormattingDisabled && p != PreserveFormattingEnabled {
return false
}

return true
}

type Formality string

const (
FormalityDefault = "default"

// for a more formal language
FormalityMore = "more"

// for a more informal language
FormalityLess = "less"
)

var validTargetLanguages map[TargetLangCode]struct{} = map[TargetLangCode]struct{}{
TargetLangDE: {},
TargetLangFR: {},
TargetLangIT: {},
TargetLangES: {},
TargetLangNL: {},
TargetLangPL: {},
TargetLangPTPT: {},
TargetLangPTBR: {},
TargetLangRU: {},
}

func (f Formality) Valid(t TargetLangCode) bool {
// This feature only works for certain target languages.
if _, ok := validTargetLanguages[t]; !ok {
return false
}

if f != FormalityDefault && f != FormalityMore && f != FormalityLess {
return false
}

return true
}
137 changes: 137 additions & 0 deletions types/translate_text_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package types_test

import (
"testing"

"github.com/michimani/deepl-sdk-go/types"
"github.com/stretchr/testify/assert"
)

func TestSplitSentencesValid(t *testing.T) {
cases := []struct {
name string
s types.SplitSentences
exect bool
}{
{
name: "SplitSentencesNoSplit",
s: types.SplitSentencesNoSplit,
exect: true,
},
{
name: "SplitSentencesSplit",
s: types.SplitSentencesSplit,
exect: true,
},
{
name: "SplitSentencesNoNewLines",
s: types.SplitSentencesNoNewLines,
exect: true,
},
{
name: "invalid value",
s: types.SplitSentences("invalid value"),
exect: false,
},
}

for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
b := c.s.Valid()

assert.Equal(tt, b, c.exect)
})
}
}

func TestPreserveFormattingValid(t *testing.T) {
cases := []struct {
name string
s types.PreserveFormatting
exect bool
}{
{
name: "PreserveFormattingDisabled",
s: types.PreserveFormattingDisabled,
exect: true,
},
{
name: "PreserveFormattingEnabled",
s: types.PreserveFormattingEnabled,
exect: true,
},
{
name: "invalid value",
s: types.PreserveFormatting("invalid value"),
exect: false,
},
}

for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
b := c.s.Valid()

assert.Equal(tt, b, c.exect)
})
}
}

func TestFormalityValid(t *testing.T) {
cases := []struct {
name string
s types.Formality
l types.TargetLangCode
exect bool
}{
{
name: "FormalityDefault",
s: types.FormalityDefault,
l: types.TargetLangRU,
exect: true,
},
{
name: "FormalityMore",
s: types.FormalityMore,
l: types.TargetLangRU,
exect: true,
},
{
name: "FormalityLess",
s: types.FormalityLess,
l: types.TargetLangRU,
exect: true,
},
{
name: "invalid value",
s: types.Formality("invalid value"),
l: types.TargetLangRU,
exect: false,
},
{
name: "FormalityDefault with not supported target language",
s: types.FormalityDefault,
l: types.TargetLangJA,
exect: false,
},
{
name: "FormalityMore with not supported target language",
s: types.FormalityMore,
l: types.TargetLangJA,
exect: false,
},
{
name: "FormalityLess with not supported target language",
s: types.FormalityLess,
l: types.TargetLangJA,
exect: false,
},
}

for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
b := c.s.Valid(c.l)

assert.Equal(tt, b, c.exect)
})
}
}

0 comments on commit 8d63e64

Please sign in to comment.