Skip to content

Commit

Permalink
add support option: translate text API: formality #1
Browse files Browse the repository at this point in the history
  • Loading branch information
michimani committed Aug 7, 2021
1 parent 6a378c9 commit 611dc17
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 7 deletions.
6 changes: 5 additions & 1 deletion params/translate_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type TranslateTextParams struct {
TargetLang types.TargetLangCode
SplitSentences types.SplitSentences
PreserveFormatting types.PreserveFormatting
Formality string
Formality types.Formality
}

func (p *TranslateTextParams) SetAuthnKey(k string) {
Expand Down Expand Up @@ -48,6 +48,10 @@ func (p *TranslateTextParams) Body() (*strings.Reader, error) {
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
28 changes: 22 additions & 6 deletions params/translate_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,51 @@ func TestTranslateTextBody(t *testing.T) {
AuthKey: "test-authn-key",
Text: []string{"text1", "text2"},
SourceLang: types.SourceLangEN,
TargetLang: types.TargetLangJA,
TargetLang: types.TargetLangRU,
SplitSentences: types.SplitSentencesNoSplit,
PreserveFormatting: types.PreserveFormattingDisabled,
Formality: types.FormalityMore,
},
expect: "auth_key=test-authn-key&preserve_formatting=0&source_lang=EN&split_sentences=0&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 with white space",
params: params.TranslateTextParams{
AuthKey: "test key",
Text: []string{"text 1", "text 2"},
SourceLang: types.SourceLangEN,
TargetLang: types.TargetLangJA,
TargetLang: types.TargetLangRU,
SplitSentences: types.SplitSentencesSplit,
PreserveFormatting: types.PreserveFormattingEnabled,
Formality: types.FormalityMore,
},
expect: "auth_key=test+key&preserve_formatting=1&source_lang=EN&split_sentences=1&target_lang=JA&text=text+1&text=text+2",
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.TargetLangJA,
TargetLang: types.TargetLangRU,
SplitSentences: "invalid value",
PreserveFormatting: "invalid value",
Formality: "invalid value",
},
expect: "auth_key=test+key&source_lang=EN&target_lang=JA&text=text+1&text=text+2",
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-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-authn-key&preserve_formatting=0&source_lang=EN&split_sentences=0&target_lang=JA&text=text1&text=text2",
},
{
name: "normal: empty",
Expand Down
37 changes: 37 additions & 0 deletions types/translate_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,40 @@ func (p PreserveFormatting) Valid() bool {

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 611dc17

Please sign in to comment.