From 162ba4b9b01efb32c5532a9527eac8b10a7738ed Mon Sep 17 00:00:00 2001 From: michimani Date: Sat, 7 Aug 2021 01:55:55 +0900 Subject: [PATCH 1/3] add support option: translate text API: split_sentences #1 --- params/translate_text.go | 7 ++++++- params/translate_text_test.go | 22 ++++++++++++---------- sample/main.go | 7 ++++--- types/translate_text.go | 22 ++++++++++++++++++++++ 4 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 types/translate_text.go diff --git a/params/translate_text.go b/params/translate_text.go index 0f7b290..9d990e3 100644 --- a/params/translate_text.go +++ b/params/translate_text.go @@ -14,7 +14,7 @@ type TranslateTextParams struct { Text []string SourceLang types.SourceLangCode TargetLang types.TargetLangCode - SplitSentences interface{} + SplitSentences types.SplitSentences PreserveFormatting interface{} Formality string } @@ -35,10 +35,15 @@ 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)) + } + return strings.NewReader(uv.Encode()), nil } diff --git a/params/translate_text_test.go b/params/translate_text_test.go index e1b8fef..afb1b2c 100644 --- a/params/translate_text_test.go +++ b/params/translate_text_test.go @@ -27,22 +27,24 @@ 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.TargetLangJA, + SplitSentences: types.SplitSentencesNoSplit, }, - expect: "auth_key=test-authn-key&source_lang=EN&target_lang=JA&text=text1&text=text2", + expect: "auth_key=test-authn-key&source_lang=EN&split_sentences=0&target_lang=JA&text=text1&text=text2", }, { name: "normal: all", params: params.TranslateTextParams{ - AuthKey: "test key", - Text: []string{"text 1", "text 2"}, - SourceLang: types.SourceLangEN, - TargetLang: types.TargetLangJA, + AuthKey: "test key", + Text: []string{"text 1", "text 2"}, + SourceLang: types.SourceLangEN, + TargetLang: types.TargetLangJA, + SplitSentences: types.SplitSentencesSplit, }, - 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&split_sentences=1&target_lang=JA&text=text+1&text=text+2", }, { name: "normal: empty", diff --git a/sample/main.go b/sample/main.go index 20f929e..c19c5ec 100644 --- a/sample/main.go +++ b/sample/main.go @@ -41,11 +41,12 @@ func translateText(c *deepl.Client) { fmt.Println("TranslateTextAPI sample (JA to EN)") text := []string{ "こんにちは", - "これはサンプルテキストです。", + "これは\nサンプルテキストです。", } params := ¶ms.TranslateTextParams{ - TargetLang: types.TargetLangEN, - Text: text, + TargetLang: types.TargetLangEN, + Text: text, + SplitSentences: types.SplitSentencesNoSplit, } res, errRes, err := c.TranslateText(context.TODO(), params) diff --git a/types/translate_text.go b/types/translate_text.go new file mode 100644 index 0000000..62526ec --- /dev/null +++ b/types/translate_text.go @@ -0,0 +1,22 @@ +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 +} From 6fc1d7a5f453d04e0b52f829d23595394c4b2277 Mon Sep 17 00:00:00 2001 From: michimani Date: Sat, 7 Aug 2021 02:13:11 +0900 Subject: [PATCH 2/3] add support option: translate text API: preserve_formatting #1 --- params/translate_text.go | 6 +++++- params/translate_text_test.go | 40 +++++++++++++++++++++++------------ sample/main.go | 7 +++--- types/translate_text.go | 17 +++++++++++++++ 4 files changed, 53 insertions(+), 17 deletions(-) diff --git a/params/translate_text.go b/params/translate_text.go index 9d990e3..2a20850 100644 --- a/params/translate_text.go +++ b/params/translate_text.go @@ -15,7 +15,7 @@ type TranslateTextParams struct { SourceLang types.SourceLangCode TargetLang types.TargetLangCode SplitSentences types.SplitSentences - PreserveFormatting interface{} + PreserveFormatting types.PreserveFormatting Formality string } @@ -44,6 +44,10 @@ func (p *TranslateTextParams) Body() (*strings.Reader, error) { uv.Add("split_sentences", string(p.SplitSentences)) } + if p.PreserveFormatting.Valid() { + uv.Add("preserve_formatting", string(p.PreserveFormatting)) + } + return strings.NewReader(uv.Encode()), nil } diff --git a/params/translate_text_test.go b/params/translate_text_test.go index afb1b2c..e96f27c 100644 --- a/params/translate_text_test.go +++ b/params/translate_text_test.go @@ -27,24 +27,38 @@ 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, - SplitSentences: types.SplitSentencesNoSplit, + AuthKey: "test-authn-key", + Text: []string{"text1", "text2"}, + SourceLang: types.SourceLangEN, + TargetLang: types.TargetLangJA, + SplitSentences: types.SplitSentencesNoSplit, + PreserveFormatting: types.PreserveFormattingDisabled, }, - expect: "auth_key=test-authn-key&source_lang=EN&split_sentences=0&target_lang=JA&text=text1&text=text2", + expect: "auth_key=test-authn-key&preserve_formatting=0&source_lang=EN&split_sentences=0&target_lang=JA&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.TargetLangJA, + SplitSentences: types.SplitSentencesSplit, + PreserveFormatting: types.PreserveFormattingEnabled, + }, + expect: "auth_key=test+key&preserve_formatting=1&source_lang=EN&split_sentences=1&target_lang=JA&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, - SplitSentences: types.SplitSentencesSplit, + AuthKey: "test key", + Text: []string{"text 1", "text 2"}, + SourceLang: types.SourceLangEN, + TargetLang: types.TargetLangJA, + SplitSentences: "invalid value", + PreserveFormatting: "invalid value", }, - expect: "auth_key=test+key&source_lang=EN&split_sentences=1&target_lang=JA&text=text+1&text=text+2", + expect: "auth_key=test+key&source_lang=EN&target_lang=JA&text=text+1&text=text+2", }, { name: "normal: empty", diff --git a/sample/main.go b/sample/main.go index c19c5ec..56d2c25 100644 --- a/sample/main.go +++ b/sample/main.go @@ -44,9 +44,10 @@ func translateText(c *deepl.Client) { "これは\nサンプルテキストです。", } params := ¶ms.TranslateTextParams{ - TargetLang: types.TargetLangEN, - Text: text, - SplitSentences: types.SplitSentencesNoSplit, + TargetLang: types.TargetLangEN, + Text: text, + SplitSentences: types.SplitSentencesNoSplit, + PreserveFormatting: types.PreserveFormattingDisabled, } res, errRes, err := c.TranslateText(context.TODO(), params) diff --git a/types/translate_text.go b/types/translate_text.go index 62526ec..3ebcd2f 100644 --- a/types/translate_text.go +++ b/types/translate_text.go @@ -20,3 +20,20 @@ func (s SplitSentences) Valid() bool { 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 +} From 611dc179357f2b7b9e7c37e2d75d73c439f1319c Mon Sep 17 00:00:00 2001 From: michimani Date: Sat, 7 Aug 2021 15:13:14 +0900 Subject: [PATCH 3/3] add support option: translate text API: formality #1 --- params/translate_text.go | 6 +- params/translate_text_test.go | 28 +++++-- types/translate_text.go | 37 +++++++++ types/translate_text_test.go | 137 ++++++++++++++++++++++++++++++++++ 4 files changed, 201 insertions(+), 7 deletions(-) create mode 100644 types/translate_text_test.go diff --git a/params/translate_text.go b/params/translate_text.go index 2a20850..7695e9d 100644 --- a/params/translate_text.go +++ b/params/translate_text.go @@ -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) { @@ -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 } diff --git a/params/translate_text_test.go b/params/translate_text_test.go index e96f27c..c5afccd 100644 --- a/params/translate_text_test.go +++ b/params/translate_text_test.go @@ -30,11 +30,12 @@ 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", @@ -42,11 +43,12 @@ func TestTranslateTextBody(t *testing.T) { 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", @@ -54,11 +56,25 @@ func TestTranslateTextBody(t *testing.T) { 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", diff --git a/types/translate_text.go b/types/translate_text.go index 3ebcd2f..f80543b 100644 --- a/types/translate_text.go +++ b/types/translate_text.go @@ -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 +} diff --git a/types/translate_text_test.go b/types/translate_text_test.go new file mode 100644 index 0000000..fd42c4c --- /dev/null +++ b/types/translate_text_test.go @@ -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) + }) + } +}