diff --git a/sdk/translation/Azure.AI.Translation.Text/README.md b/sdk/translation/Azure.AI.Translation.Text/README.md index 4bea6f5ad33ca..d0a4bb6e7ff9c 100644 --- a/sdk/translation/Azure.AI.Translation.Text/README.md +++ b/sdk/translation/Azure.AI.Translation.Text/README.md @@ -97,16 +97,16 @@ We guarantee that all client instance methods are thread-safe and independent of ## Examples -The following section provides several code snippets using the `client` [created above](#create-a-texttranslationclient-using-an-api-key-and-region-credential), and covers the main features present in this client library. Although most of the snippets below make use of asynchronous service calls, keep in mind that the `Azure.AI.Translation.Text` package supports both synchronous and asynchronous APIs. +The following section provides several code snippets using the `client` [created above](#create-a-texttranslationclient-using-an-api-key-and-region-credential), and covers the main features present in this client library. Although the snippets below make use of synchronous service calls, keep in mind that the `Azure.AI.Translation.Text` package supports both synchronous and asynchronous APIs. ### Get Supported Languages Gets the set of languages currently supported by other operations of the Translator. -```C# Snippet:GetTextTranslationLanguagesAsync +```C# Snippet:GetTextTranslationLanguages try { - Response response = await client.GetLanguagesAsync(cancellationToken: CancellationToken.None).ConfigureAwait(false); + Response response = client.GetLanguages(cancellationToken: CancellationToken.None); GetLanguagesResult languages = response.Value; Console.WriteLine($"Number of supported languages for translate operations: {languages.Translation.Count}."); @@ -126,13 +126,13 @@ Please refer to the service documentation for a conceptual discussion of [langua Renders single source-language text to multiple target-language texts with a single request. -```C# Snippet:GetTextTranslationAsync +```C# Snippet:GetTextTranslation try { string targetLanguage = "cs"; string inputText = "This is a test."; - Response> response = await client.TranslateAsync(targetLanguage, inputText).ConfigureAwait(false); + Response> response = client.Translate(targetLanguage, inputText); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -154,7 +154,7 @@ Please refer to the service documentation for a conceptual discussion of [transl Converts characters or letters of a source language to the corresponding characters or letters of a target language. -```C# Snippet:GetTransliteratedTextAsync +```C# Snippet:GetTransliteratedText try { string language = "zh-Hans"; @@ -163,7 +163,7 @@ try string inputText = "这是个测试。"; - Response> response = await client.TransliterateAsync(language, fromScript, toScript, inputText).ConfigureAwait(false); + Response> response = client.Transliterate(language, fromScript, toScript, inputText); IReadOnlyList transliterations = response.Value; TransliteratedText transliteration = transliterations.FirstOrDefault(); @@ -184,12 +184,12 @@ Please refer to the service documentation for a conceptual discussion of [transl Identifies the positioning of sentence boundaries in a piece of text. -```C# Snippet:FindTextSentenceBoundariesAsync +```C# Snippet:FindTextSentenceBoundaries try { string inputText = "How are you? I am fine. What did you do today?"; - Response> response = await client.FindSentenceBoundariesAsync(inputText).ConfigureAwait(false); + Response> response = client.FindSentenceBoundaries(inputText); IReadOnlyList brokenSentences = response.Value; BreakSentenceItem brokenSentence = brokenSentences.FirstOrDefault(); @@ -211,14 +211,14 @@ Please refer to the service documentation for a conceptual discussion of [break Returns equivalent words for the source term in the target language. -```C# Snippet:LookupDictionaryEntriesAsync +```C# Snippet:LookupDictionaryEntries try { string sourceLanguage = "en"; string targetLanguage = "es"; string inputText = "fly"; - Response> response = await client.LookupDictionaryEntriesAsync(sourceLanguage, targetLanguage, inputText).ConfigureAwait(false); + Response> response = client.LookupDictionaryEntries(sourceLanguage, targetLanguage, inputText); IReadOnlyList dictionaryEntries = response.Value; DictionaryLookupItem dictionaryEntry = dictionaryEntries.FirstOrDefault(); @@ -240,7 +240,7 @@ Please refer to the service documentation for a conceptual discussion of [dictio Returns grammatical structure and context examples for the source term and target term pair. -```C# Snippet:GetGrammaticalStructureAsync +```C# Snippet:GetGrammaticalStructure try { string sourceLanguage = "en"; @@ -250,7 +250,7 @@ try new InputTextWithTranslation("fly", "volar") }; - Response> response = await client.LookupDictionaryExamplesAsync(sourceLanguage, targetLanguage, inputTextElements).ConfigureAwait(false); + Response> response = client.LookupDictionaryExamples(sourceLanguage, targetLanguage, inputTextElements); IReadOnlyList dictionaryEntries = response.Value; DictionaryExampleItem dictionaryEntry = dictionaryEntries.FirstOrDefault(); @@ -275,10 +275,10 @@ When you interact with the Translator Service using the Text Translation client For example, if you submit a translation request without a target translate language, a `400` error is returned, indicating "Bad Request". -```C# Snippet:HandleBadRequestAsync +```C# Snippet:HandleBadRequest try { - var translation = await client.TranslateAsync(Array.Empty(), new[] { "This is a Test" }).ConfigureAwait(false); + var translation = client.Translate(Array.Empty(), new[] { "This is a Test" }); } catch (RequestFailedException e) { diff --git a/sdk/translation/Azure.AI.Translation.Text/samples/Sample1_GetLanguages.md b/sdk/translation/Azure.AI.Translation.Text/samples/Sample1_GetLanguages.md index 6e283b539fcbe..565afc2afccb7 100644 --- a/sdk/translation/Azure.AI.Translation.Text/samples/Sample1_GetLanguages.md +++ b/sdk/translation/Azure.AI.Translation.Text/samples/Sample1_GetLanguages.md @@ -6,10 +6,10 @@ This sample demonstrates how to get languages that are supported by other operat This will return language metadata from all supported scopes. -```C# Snippet:GetTextTranslationLanguagesMetadataAsync +```C# Snippet:GetTextTranslationLanguagesMetadata try { - Response response = await client.GetLanguagesAsync().ConfigureAwait(false); + Response response = client.GetLanguages(); GetLanguagesResult languages = response.Value; Console.WriteLine($"Number of supported languages for translate operation: {languages.Translation.Count}."); @@ -45,11 +45,11 @@ catch (RequestFailedException exception) You can limit the scope of the response of the languages API by providing the optional paramter `scope`. A comma-separated list of names defining the group of languages to return. Allowed group names are: `translation`, `transliteration` and `dictionary`. If no scope is given, then all groups are returned, which is equivalent to passing `translation,transliteration,dictionary`. -```C# Snippet:GetTextTranslationLanguagesByScopeAsync +```C# Snippet:GetTextTranslationLanguagesByScope try { string scope = "translation"; - Response response = await client.GetLanguagesAsync(scope: scope).ConfigureAwait(false); + Response response = client.GetLanguages(scope: scope); GetLanguagesResult languages = response.Value; Console.WriteLine($"Number of supported languages for translate operations: {languages.Translation.Count}."); @@ -86,11 +86,11 @@ catch (RequestFailedException exception) You can select the language to use for user interface strings. Some of the fields in the response are names of languages or names of regions. Use this parameter to define the language in which these names are returned. The language is specified by providing a well-formed BCP 47 language tag. For instance, use the value `fr` to request names in French or use the value `zh-Hant` to request names in Chinese Traditional. Names are provided in the English language when a target language is not specified or when localization is not available. -```C# Snippet:GetTextTranslationLanguagesByCultureAsync +```C# Snippet:GetTextTranslationLanguagesByCulture try { string acceptLanguage = "es"; - Response response = await client.GetLanguagesAsync(acceptLanguage: acceptLanguage).ConfigureAwait(false); + Response response = client.GetLanguages(acceptLanguage: acceptLanguage); GetLanguagesResult languages = response.Value; Console.WriteLine($"Number of supported languages for translate operations: {languages.Translation.Count}."); diff --git a/sdk/translation/Azure.AI.Translation.Text/samples/Sample2_Translate.md b/sdk/translation/Azure.AI.Translation.Text/samples/Sample2_Translate.md index cd00506a3d33d..8824c5cc72dc6 100644 --- a/sdk/translation/Azure.AI.Translation.Text/samples/Sample2_Translate.md +++ b/sdk/translation/Azure.AI.Translation.Text/samples/Sample2_Translate.md @@ -6,14 +6,14 @@ All samples are using `client` created in [Create a `TextTranslationClient`][cre Translate text from known source language to target language. -```C# Snippet:GetTextTranslationBySourceAsync +```C# Snippet:GetTextTranslationBySource try { string from = "en"; string targetLanguage = "cs"; string inputText = "This is a test."; - Response> response = await client.TranslateAsync(targetLanguage, inputText, sourceLanguage: from).ConfigureAwait(false); + Response> response = client.Translate(targetLanguage, inputText, sourceLanguage: from); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -34,13 +34,13 @@ You can ommit source languge of the input text. In this case, API will try to au > Note that you must provide the source language rather than autodetection when using the dynamic dictionary feature. > Note you can use `suggestedFrom` paramter that specifies a fallback language if the language of the input text can't be identified. Language autodetection is applied when the from parameter is omitted. If detection fails, the suggestedFrom language will be assumed. -```C# Snippet:GetTextTranslationAutoDetectAsync +```C# Snippet:GetTextTranslationAutoDetect try { string targetLanguage = "cs"; string inputText = "This is a test."; - Response> response = await client.TranslateAsync(targetLanguage, inputText).ConfigureAwait(false); + Response> response = client.Translate(targetLanguage, inputText); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -58,7 +58,7 @@ catch (RequestFailedException exception) You can combine both Translation and Transliteration in one Translate call. Your source Text can be in non-standard Script of a language as well as you can ask for non-standard Script of a target language. -```C# Snippet:GetTranslationTextTransliteratedAsync +```C# Snippet:GetTranslationTextTransliterated try { string fromScript = "Latn"; @@ -70,7 +70,7 @@ try "hudha akhtabar." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, sourceLanguage: fromLanguage, fromScript: fromScript, toScript: toScript).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements, sourceLanguage: fromLanguage, fromScript: fromScript, toScript: toScript); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -89,7 +89,7 @@ catch (RequestFailedException exception) You can translate multiple text elements. Each input element can be in different language (source language parameter needs to be omitted and language auto-detection is used). Refer to [Request limits for Translator](https://learn.microsoft.com/azure/cognitive-services/translator/request-limits) for current limits. -```C# Snippet:GetMultipleTextTranslationsAsync +```C# Snippet:GetMultipleTextTranslations try { IEnumerable targetLanguages = new[] { "cs" }; @@ -100,7 +100,7 @@ try "Dies ist ein Test." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements); IReadOnlyList translations = response.Value; foreach (TranslatedTextItem translation in translations) @@ -120,7 +120,7 @@ catch (RequestFailedException exception) You can provide multiple target languages which results in each input element being translated to all target languages. -```C# Snippet:GetTextTranslationMatrixAsync +```C# Snippet:GetTextTranslationMatrix try { IEnumerable targetLanguages = new[] { "cs", "es", "de" }; @@ -129,7 +129,7 @@ try "This is a test." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements); IReadOnlyList translations = response.Value; foreach (TranslatedTextItem translation in translations) @@ -150,7 +150,7 @@ catch (RequestFailedException exception) You can select whether the translated text is plain text or HTML text. Any HTML needs to be a well-formed, complete element. Possible values are: plain (default) or html. -```C# Snippet:GetTextTranslationFormatAsync +```C# Snippet:GetTextTranslationFormat try { IEnumerable targetLanguages = new[] { "cs" }; @@ -159,7 +159,7 @@ try "This is a test." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, textType: TextType.Html).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements, textType: TextType.Html); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -177,7 +177,7 @@ catch (RequestFailedException exception) It's sometimes useful to exclude specific content from translation. You can use the attribute class=notranslate to specify content that should remain in its original language. In the following example, the content inside the first div element won't be translated, while the content in the second div element will be translated. -```C# Snippet:GetTextTranslationFilterAsync +```C# Snippet:GetTextTranslationFilter try { string from = "en"; @@ -187,7 +187,7 @@ try "
This will not be translated.
This will be translated.
" }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, textType: TextType.Html, sourceLanguage: from).ConfigureAwait(false); + Response> response =client.Translate(targetLanguages, inputTextElements, textType: TextType.Html, sourceLanguage: from); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -207,7 +207,7 @@ If you already know the translation you want to apply to a word or a phrase, you > Note You must include the From parameter in your API translation request instead of using the autodetect feature. -```C# Snippet:GetTextTranslationMarkupAsync +```C# Snippet:GetTextTranslationMarkup try { string from = "en"; @@ -217,7 +217,7 @@ try "The word wordomatic is a dictionary entry." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, sourceLanguage: from).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements, sourceLanguage: from); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -237,7 +237,7 @@ catch (RequestFailedException exception) If you want to avoid getting profanity in the translation, regardless of the presence of profanity in the source text, you can use the profanity filtering option. The option allows you to choose whether you want to see profanity deleted, whether you want to mark profanities with appropriate tags (giving you the option to add your own post-processing), or you want no action taken. The accepted values of `ProfanityAction` are `Deleted`, `Marked` and `NoAction` (default). -```C# Snippet:GetTextTranslationProfanityAsync +```C# Snippet:GetTextTranslationProfanity try { ProfanityAction profanityAction = ProfanityAction.Marked; @@ -249,7 +249,7 @@ try "This is ***." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, profanityAction: profanityAction, profanityMarker: profanityMarkers).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements, profanityAction: profanityAction, profanityMarker: profanityMarkers); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -267,7 +267,7 @@ catch (RequestFailedException exception) You can ask translation service to include alignment projection from source text to translated text. -```C# Snippet:GetTextTranslationAlignmentAsync +```C# Snippet:GetTextTranslationAlignment try { bool includeAlignment = true; @@ -278,7 +278,7 @@ try "The answer lies in machine translation." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, includeAlignment: includeAlignment).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements, includeAlignment: includeAlignment); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -297,7 +297,7 @@ catch (RequestFailedException exception) You can ask translator service to include sentence boundaries for the input text and the translated text. -```C# Snippet:GetTextTranslationSentencesAsync +```C# Snippet:GetTextTranslationSentences try { bool includeSentenceLength = true; @@ -308,7 +308,7 @@ try "The answer lies in machine translation. This is a test." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, includeSentenceLength: includeSentenceLength).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements, includeSentenceLength: includeSentenceLength); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -332,7 +332,7 @@ It is possible to set `allowFalback` paramter. It specifies that the service is `allowFallback=false` specifies that the translation should only use systems trained for the category specified by the request. If a translation for language X to language Y requires chaining through a pivot language E, then all the systems in the chain (X → E and E → Y) will need to be custom and have the same category. If no system is found with the specific category, the request will return a 400 status code. `allowFallback=true` specifies that the service is allowed to fall back to a general system when a custom system doesn't exist. -```C# Snippet:GetTextTranslationFallbackAsync +```C# Snippet:GetTextTranslationFallback try { string category = "<>"; @@ -342,7 +342,7 @@ try "This is a test." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, category: category).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements, category: category); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); diff --git a/sdk/translation/Azure.AI.Translation.Text/samples/Sample3_Transliterate.md b/sdk/translation/Azure.AI.Translation.Text/samples/Sample3_Transliterate.md index 7b5cf565a9da2..d5927f7180263 100644 --- a/sdk/translation/Azure.AI.Translation.Text/samples/Sample3_Transliterate.md +++ b/sdk/translation/Azure.AI.Translation.Text/samples/Sample3_Transliterate.md @@ -6,19 +6,16 @@ All samples are using `client` created in [Create a `TextTranslationClient`][cre Converts characters or letters of a source language to the corresponding characters or letters of a target language. -```C# +```C# Snippet:GetTransliteratedText try { string language = "zh-Hans"; string fromScript = "Hans"; string toScript = "Latn"; - IEnumerable inputTextElements = new[] - { - "这是个测试。" - }; + string inputText = "这是个测试。"; - Response> response = await client.TransliterateAsync(language, fromScript, toScript, inputTextElements).ConfigureAwait(false); + Response> response = client.Transliterate(language, fromScript, toScript, inputText); IReadOnlyList transliterations = response.Value; TransliteratedText transliteration = transliterations.FirstOrDefault(); diff --git a/sdk/translation/Azure.AI.Translation.Text/samples/Sample4_BreakSentence.md b/sdk/translation/Azure.AI.Translation.Text/samples/Sample4_BreakSentence.md index f502970056daf..46533ad1c4d48 100644 --- a/sdk/translation/Azure.AI.Translation.Text/samples/Sample4_BreakSentence.md +++ b/sdk/translation/Azure.AI.Translation.Text/samples/Sample4_BreakSentence.md @@ -6,7 +6,7 @@ All samples are using `client` created in [Create a `TextTranslationClient`][cre When the input language is known, you can provide those to the service call. -```C# Snippet:GetTextTranslationSentencesSourceAsync +```C# Snippet:GetTextTranslationSentencesSource try { string sourceLanguage = "zh-Hans"; @@ -16,7 +16,7 @@ try "zhè shì gè cè shì。" }; - Response> response = await client.FindSentenceBoundariesAsync(inputTextElements, language: sourceLanguage, script: sourceScript).ConfigureAwait(false); + Response> response = client.FindSentenceBoundaries(inputTextElements, language: sourceLanguage, script: sourceScript); IReadOnlyList brokenSentences = response.Value; BreakSentenceItem brokenSentence = brokenSentences.FirstOrDefault(); @@ -34,7 +34,7 @@ catch (RequestFailedException exception) You can ommit source languge of the input text. In this case, API will try to auto-detect the language. -```C# Snippet:GetTextTranslationSentencesAutoAsync +```C# Snippet:GetTextTranslationSentencesAuto try { IEnumerable inputTextElements = new[] @@ -42,7 +42,7 @@ try "How are you? I am fine. What did you do today?" }; - Response> response = await client.FindSentenceBoundariesAsync(inputTextElements).ConfigureAwait(false); + Response> response = client.FindSentenceBoundaries(inputTextElements); IReadOnlyList brokenSentences = response.Value; BreakSentenceItem brokenSentence = brokenSentences.FirstOrDefault(); diff --git a/sdk/translation/Azure.AI.Translation.Text/samples/Sample5_DictionaryLookup.md b/sdk/translation/Azure.AI.Translation.Text/samples/Sample5_DictionaryLookup.md index d12c184b5e2f5..d14e7eeb1d478 100644 --- a/sdk/translation/Azure.AI.Translation.Text/samples/Sample5_DictionaryLookup.md +++ b/sdk/translation/Azure.AI.Translation.Text/samples/Sample5_DictionaryLookup.md @@ -6,14 +6,14 @@ All samples are using `client` created in [Create a `TextTranslationClient`][cre Returns equivalent words for the source term in the target language. -```C# Snippet:LookupDictionaryEntriesAsync +```C# Snippet:LookupDictionaryEntries try { string sourceLanguage = "en"; string targetLanguage = "es"; string inputText = "fly"; - Response> response = await client.LookupDictionaryEntriesAsync(sourceLanguage, targetLanguage, inputText).ConfigureAwait(false); + Response> response = client.LookupDictionaryEntries(sourceLanguage, targetLanguage, inputText); IReadOnlyList dictionaryEntries = response.Value; DictionaryLookupItem dictionaryEntry = dictionaryEntries.FirstOrDefault(); diff --git a/sdk/translation/Azure.AI.Translation.Text/samples/Sample6_DictionaryExamples.md b/sdk/translation/Azure.AI.Translation.Text/samples/Sample6_DictionaryExamples.md index ad160928ac9d8..cfc9874556d45 100644 --- a/sdk/translation/Azure.AI.Translation.Text/samples/Sample6_DictionaryExamples.md +++ b/sdk/translation/Azure.AI.Translation.Text/samples/Sample6_DictionaryExamples.md @@ -6,7 +6,7 @@ All samples are using `client` created in [Create a `TextTranslationClient`][cre Returns grammatical structure and context examples for the source term and target term pair. -```C# Snippet:GetGrammaticalStructureAsync +```C# Snippet:GetGrammaticalStructure try { string sourceLanguage = "en"; @@ -16,7 +16,7 @@ try new InputTextWithTranslation("fly", "volar") }; - Response> response = await client.LookupDictionaryExamplesAsync(sourceLanguage, targetLanguage, inputTextElements).ConfigureAwait(false); + Response> response = client.LookupDictionaryExamples(sourceLanguage, targetLanguage, inputTextElements); IReadOnlyList dictionaryEntries = response.Value; DictionaryExampleItem dictionaryEntry = dictionaryEntries.FirstOrDefault(); diff --git a/sdk/translation/Azure.AI.Translation.Text/tests/Samples/SampleSnippets.cs b/sdk/translation/Azure.AI.Translation.Text/tests/Samples/SampleSnippets.cs index 7ef018e83631b..d9affd436b1f0 100644 --- a/sdk/translation/Azure.AI.Translation.Text/tests/Samples/SampleSnippets.cs +++ b/sdk/translation/Azure.AI.Translation.Text/tests/Samples/SampleSnippets.cs @@ -127,12 +127,32 @@ public TextTranslationClient CreateTextTranslationClientWithToken() return client; } + [Test] + public void GetTextTranslationLanguages() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationLanguages + try + { + Response response = client.GetLanguages(cancellationToken: CancellationToken.None); + GetLanguagesResult languages = response.Value; + + Console.WriteLine($"Number of supported languages for translate operations: {languages.Translation.Count}."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } + #endregion + } + [Test] public async void GetTextTranslationLanguagesAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationLanguagesAsync try { Response response = await client.GetLanguagesAsync(cancellationToken: CancellationToken.None).ConfigureAwait(false); @@ -145,6 +165,46 @@ public async void GetTextTranslationLanguagesAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetTextTranslationLanguagesMetadata() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationLanguagesMetadata + try + { + Response response = client.GetLanguages(); + GetLanguagesResult languages = response.Value; + + Console.WriteLine($"Number of supported languages for translate operation: {languages.Translation.Count}."); + Console.WriteLine($"Number of supported languages for transliterate operation: {languages.Transliteration.Count}."); + Console.WriteLine($"Number of supported languages for dictionary operations: {languages.Dictionary.Count}."); + + Console.WriteLine("Translation Languages:"); + foreach (var translationLanguage in languages.Translation) + { + Console.WriteLine($"{translationLanguage.Key} -- name: {translationLanguage.Value.Name} ({translationLanguage.Value.NativeName})"); + } + + Console.WriteLine("Transliteration Languages:"); + foreach (var transliterationLanguage in languages.Transliteration) + { + Console.WriteLine($"{transliterationLanguage.Key} -- name: {transliterationLanguage.Value.Name}, supported script count: {transliterationLanguage.Value.Scripts.Count}"); + } + + Console.WriteLine("Dictionary Languages:"); + foreach (var dictionaryLanguage in languages.Dictionary) + { + Console.WriteLine($"{dictionaryLanguage.Key} -- name: {dictionaryLanguage.Value.Name}, supported target languages count: {dictionaryLanguage.Value.Translations.Count}"); + } + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -153,7 +213,6 @@ public async void GetTextTranslationLanguagesMetadataAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationLanguagesMetadataAsync try { Response response = await client.GetLanguagesAsync().ConfigureAwait(false); @@ -186,6 +245,47 @@ public async void GetTextTranslationLanguagesMetadataAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetTextTranslationLanguagesByScope() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationLanguagesByScope + try + { + string scope = "translation"; + Response response = client.GetLanguages(scope: scope); + GetLanguagesResult languages = response.Value; + + Console.WriteLine($"Number of supported languages for translate operations: {languages.Translation.Count}."); + Console.WriteLine($"Number of supported languages for translate operations: {languages.Transliteration.Count}."); + Console.WriteLine($"Number of supported languages for translate operations: {languages.Dictionary.Count}."); + + Console.WriteLine("Translation Languages:"); + foreach (var translationLanguage in languages.Translation) + { + Console.WriteLine($"{translationLanguage.Key} -- name: {translationLanguage.Value.Name} ({translationLanguage.Value.NativeName})"); + } + + Console.WriteLine("Transliteration Languages:"); + foreach (var transliterationLanguage in languages.Transliteration) + { + Console.WriteLine($"{transliterationLanguage.Key} -- name: {transliterationLanguage.Value.Name}, supported script count: {transliterationLanguage.Value.Scripts.Count}"); + } + + Console.WriteLine("Dictionary Languages:"); + foreach (var dictionaryLanguage in languages.Dictionary) + { + Console.WriteLine($"{dictionaryLanguage.Key} -- name: {dictionaryLanguage.Value.Name}, supported target languages count: {dictionaryLanguage.Value.Translations.Count}"); + } + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -194,7 +294,6 @@ public async void GetTextTranslationLanguagesByScopeAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationLanguagesByScopeAsync try { string scope = "translation"; @@ -228,6 +327,47 @@ public async void GetTextTranslationLanguagesByScopeAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetTextTranslationLanguagesByCulture() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationLanguagesByCulture + try + { + string acceptLanguage = "es"; + Response response = client.GetLanguages(acceptLanguage: acceptLanguage); + GetLanguagesResult languages = response.Value; + + Console.WriteLine($"Number of supported languages for translate operations: {languages.Translation.Count}."); + Console.WriteLine($"Number of supported languages for translate operations: {languages.Transliteration.Count}."); + Console.WriteLine($"Number of supported languages for translate operations: {languages.Dictionary.Count}."); + + Console.WriteLine("Translation Languages:"); + foreach (var translationLanguage in languages.Translation) + { + Console.WriteLine($"{translationLanguage.Key} -- name: {translationLanguage.Value.Name} ({translationLanguage.Value.NativeName})"); + } + + Console.WriteLine("Transliteration Languages:"); + foreach (var transliterationLanguage in languages.Transliteration) + { + Console.WriteLine($"{transliterationLanguage.Key} -- name: {transliterationLanguage.Value.Name}, supported script count: {transliterationLanguage.Value.Scripts.Count}"); + } + + Console.WriteLine("Dictionary Languages:"); + foreach (var dictionaryLanguage in languages.Dictionary) + { + Console.WriteLine($"{dictionaryLanguage.Key} -- name: {dictionaryLanguage.Value.Name}, supported target languages count: {dictionaryLanguage.Value.Translations.Count}"); + } + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -236,7 +376,6 @@ public async void GetTextTranslationLanguagesByCultureAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationLanguagesByCultureAsync try { string acceptLanguage = "es"; @@ -270,6 +409,31 @@ public async void GetTextTranslationLanguagesByCultureAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetTextTranslation() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslation + try + { + string targetLanguage = "cs"; + string inputText = "This is a test."; + + Response> response = client.Translate(targetLanguage, inputText); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -278,7 +442,6 @@ public async void GetTextTranslationAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationAsync try { string targetLanguage = "cs"; @@ -296,6 +459,32 @@ public async void GetTextTranslationAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetTextTranslationBySource() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationBySource + try + { + string from = "en"; + string targetLanguage = "cs"; + string inputText = "This is a test."; + + Response> response = client.Translate(targetLanguage, inputText, sourceLanguage: from); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -304,7 +493,6 @@ public async void GetTextTranslationBySourceAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationBySourceAsync try { string from = "en"; @@ -323,6 +511,31 @@ public async void GetTextTranslationBySourceAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetTextTranslationAutoDetect() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationAutoDetect + try + { + string targetLanguage = "cs"; + string inputText = "This is a test."; + + Response> response = client.Translate(targetLanguage, inputText); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -331,7 +544,6 @@ public async void GetTextTranslationAutoDetectAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationAutoDetectAsync try { string targetLanguage = "cs"; @@ -349,6 +561,38 @@ public async void GetTextTranslationAutoDetectAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetMultipleTextTranslations() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetMultipleTextTranslations + try + { + IEnumerable targetLanguages = new[] { "cs" }; + IEnumerable inputTextElements = new[] + { + "This is a test.", + "Esto es una prueba.", + "Dies ist ein Test." + }; + + Response> response = client.Translate(targetLanguages, inputTextElements); + IReadOnlyList translations = response.Value; + + foreach (TranslatedTextItem translation in translations) + { + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -357,7 +601,6 @@ public async void GetMultipleTextTranslationsAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetMultipleTextTranslationsAsync try { IEnumerable targetLanguages = new[] { "cs" }; @@ -382,6 +625,37 @@ public async void GetMultipleTextTranslationsAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetTextTranslationMatrix() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationMatrix + try + { + IEnumerable targetLanguages = new[] { "cs", "es", "de" }; + IEnumerable inputTextElements = new[] + { + "This is a test." + }; + + Response> response = client.Translate(targetLanguages, inputTextElements); + IReadOnlyList translations = response.Value; + + foreach (TranslatedTextItem translation in translations) + { + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -390,7 +664,6 @@ public async void GetTextTranslationMatrixAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationMatrixAsync try { IEnumerable targetLanguages = new[] { "cs", "es", "de" }; @@ -406,8 +679,211 @@ public async void GetTextTranslationMatrixAsync() { Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); - Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); - } + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } + } + + [Test] + public void GetTextTranslationFormat() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationFormat + try + { + IEnumerable targetLanguages = new[] { "cs" }; + IEnumerable inputTextElements = new[] + { + "This is a test." + }; + + Response> response = client.Translate(targetLanguages, inputTextElements, textType: TextType.Html); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } + #endregion + } + + [Test] + public async void GetTextTranslationFormatAsync() + { + TextTranslationClient client = CreateTextTranslationClient(); + + try + { + IEnumerable targetLanguages = new[] { "cs" }; + IEnumerable inputTextElements = new[] + { + "This is a test." + }; + + Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, textType: TextType.Html).ConfigureAwait(false); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } + } + + [Test] + public void GetTextTranslationFilter() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationFilter + try + { + string from = "en"; + IEnumerable targetLanguages = new[] { "cs" }; + IEnumerable inputTextElements = new[] + { + "
This will not be translated.
This will be translated.
" + }; + + Response> response =client.Translate(targetLanguages, inputTextElements, textType: TextType.Html, sourceLanguage: from); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } + #endregion + } + + [Test] + public async void GetTextTranslationFilterAsync() + { + TextTranslationClient client = CreateTextTranslationClient(); + + try + { + string from = "en"; + IEnumerable targetLanguages = new[] { "cs" }; + IEnumerable inputTextElements = new[] + { + "
This will not be translated.
This will be translated.
" + }; + + Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, textType: TextType.Html, sourceLanguage: from).ConfigureAwait(false); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } + } + + [Test] + public void GetTextTranslationMarkup() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationMarkup + try + { + string from = "en"; + IEnumerable targetLanguages = new[] { "cs" }; + IEnumerable inputTextElements = new[] + { + "The word wordomatic is a dictionary entry." + }; + + Response> response = client.Translate(targetLanguages, inputTextElements, sourceLanguage: from); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } + #endregion + } + + [Test] + public async void GetTextTranslationMarkupAsync() + { + TextTranslationClient client = CreateTextTranslationClient(); + + try + { + string from = "en"; + IEnumerable targetLanguages = new[] { "cs" }; + IEnumerable inputTextElements = new[] + { + "The word wordomatic is a dictionary entry." + }; + + Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, sourceLanguage: from).ConfigureAwait(false); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } + } + + [Test] + public void GetTextTranslationProfanity() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationProfanity + try + { + ProfanityAction profanityAction = ProfanityAction.Marked; + ProfanityMarker profanityMarkers = ProfanityMarker.Asterisk; + + IEnumerable targetLanguages = new[] { "cs" }; + IEnumerable inputTextElements = new[] + { + "This is ***." + }; + + Response> response = client.Translate(targetLanguages, inputTextElements, profanityAction: profanityAction, profanityMarker: profanityMarkers); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); + Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); } catch (RequestFailedException exception) { @@ -418,20 +894,22 @@ public async void GetTextTranslationMatrixAsync() } [Test] - public async void GetTextTranslationFormatAsync() + public async void GetTextTranslationProfanityAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationFormatAsync try { + ProfanityAction profanityAction = ProfanityAction.Marked; + ProfanityMarker profanityMarkers = ProfanityMarker.Asterisk; + IEnumerable targetLanguages = new[] { "cs" }; IEnumerable inputTextElements = new[] { - "This is a test." + "This is ***." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, textType: TextType.Html).ConfigureAwait(false); + Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, profanityAction: profanityAction, profanityMarker: profanityMarkers).ConfigureAwait(false); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); @@ -443,30 +921,31 @@ public async void GetTextTranslationFormatAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } - #endregion } [Test] - public async void GetTextTranslationFilterAsync() + public void GetTextTranslationAlignment() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationFilterAsync + #region Snippet:GetTextTranslationAlignment try { - string from = "en"; + bool includeAlignment = true; + IEnumerable targetLanguages = new[] { "cs" }; IEnumerable inputTextElements = new[] { - "
This will not be translated.
This will be translated.
" + "The answer lies in machine translation." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, textType: TextType.Html, sourceLanguage: from).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements, includeAlignment: includeAlignment); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + Console.WriteLine($"Alignments: {translation?.Translations?.FirstOrDefault()?.Alignment?.Proj}"); } catch (RequestFailedException exception) { @@ -477,58 +956,59 @@ public async void GetTextTranslationFilterAsync() } [Test] - public async void GetTextTranslationMarkupAsync() + public async void GetTextTranslationAlignmentAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationMarkupAsync try { - string from = "en"; + bool includeAlignment = true; + IEnumerable targetLanguages = new[] { "cs" }; IEnumerable inputTextElements = new[] { - "The word wordomatic is a dictionary entry." - }; + "The answer lies in machine translation." + }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, sourceLanguage: from).ConfigureAwait(false); + Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, includeAlignment: includeAlignment).ConfigureAwait(false); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + Console.WriteLine($"Alignments: {translation?.Translations?.FirstOrDefault()?.Alignment?.Proj}"); } catch (RequestFailedException exception) { Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } - #endregion } [Test] - public async void GetTextTranslationProfanityAsync() + public void GetTextTranslationSentences() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationProfanityAsync + #region Snippet:GetTextTranslationSentences try { - ProfanityAction profanityAction = ProfanityAction.Marked; - ProfanityMarker profanityMarkers = ProfanityMarker.Asterisk; + bool includeSentenceLength = true; IEnumerable targetLanguages = new[] { "cs" }; IEnumerable inputTextElements = new[] { - "This is ***." + "The answer lies in machine translation. This is a test." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, profanityAction: profanityAction, profanityMarker: profanityMarkers).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements, includeSentenceLength: includeSentenceLength); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + Console.WriteLine($"Source Sentece length: {string.Join(",", translation?.Translations?.FirstOrDefault()?.SentLen?.SrcSentLen)}"); + Console.WriteLine($"Translated Sentece length: {string.Join(",", translation?.Translations?.FirstOrDefault()?.SentLen?.TransSentLen)}"); } catch (RequestFailedException exception) { @@ -539,61 +1019,57 @@ public async void GetTextTranslationProfanityAsync() } [Test] - public async void GetTextTranslationAlignmentAsync() + public async void GetTextTranslationSentencesAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationAlignmentAsync try { - bool includeAlignment = true; + bool includeSentenceLength = true; IEnumerable targetLanguages = new[] { "cs" }; IEnumerable inputTextElements = new[] { - "The answer lies in machine translation." + "The answer lies in machine translation. This is a test." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, includeAlignment: includeAlignment).ConfigureAwait(false); + Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, includeSentenceLength: includeSentenceLength).ConfigureAwait(false); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); - Console.WriteLine($"Alignments: {translation?.Translations?.FirstOrDefault()?.Alignment?.Proj}"); + Console.WriteLine($"Source Sentece length: {string.Join(",", translation?.Translations?.FirstOrDefault()?.SentLen?.SrcSentLen)}"); + Console.WriteLine($"Translated Sentece length: {string.Join(",", translation?.Translations?.FirstOrDefault()?.SentLen?.TransSentLen)}"); } catch (RequestFailedException exception) { Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } - #endregion } [Test] - public async void GetTextTranslationSentencesAsync() + public void GetTextTranslationFallback() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationSentencesAsync + #region Snippet:GetTextTranslationFallback try { - bool includeSentenceLength = true; - + string category = "<>"; IEnumerable targetLanguages = new[] { "cs" }; IEnumerable inputTextElements = new[] { - "The answer lies in machine translation. This is a test." + "This is a test." }; - Response> response = await client.TranslateAsync(targetLanguages, inputTextElements, includeSentenceLength: includeSentenceLength).ConfigureAwait(false); + Response> response = client.Translate(targetLanguages, inputTextElements, category: category); IReadOnlyList translations = response.Value; TranslatedTextItem translation = translations.FirstOrDefault(); Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}."); Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().To}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'."); - Console.WriteLine($"Source Sentece length: {string.Join(",", translation?.Translations?.FirstOrDefault()?.SentLen?.SrcSentLen)}"); - Console.WriteLine($"Translated Sentece length: {string.Join(",", translation?.Translations?.FirstOrDefault()?.SentLen?.TransSentLen)}"); } catch (RequestFailedException exception) { @@ -608,7 +1084,6 @@ public async void GetTextTranslationFallbackAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationFallbackAsync try { string category = "<>"; @@ -630,6 +1105,35 @@ public async void GetTextTranslationFallbackAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetTextTranslationSentencesSource() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationSentencesSource + try + { + string sourceLanguage = "zh-Hans"; + string sourceScript = "Latn"; + IEnumerable inputTextElements = new[] + { + "zhè shì gè cè shì。" + }; + + Response> response = client.FindSentenceBoundaries(inputTextElements, language: sourceLanguage, script: sourceScript); + IReadOnlyList brokenSentences = response.Value; + BreakSentenceItem brokenSentence = brokenSentences.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {brokenSentence?.DetectedLanguage?.Language} with score: {brokenSentence?.DetectedLanguage?.Score}."); + Console.WriteLine($"The detected sentece boundaries: '{string.Join(",", brokenSentence?.SentLen)}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -638,7 +1142,6 @@ public async void GetTextTranslationSentencesSourceAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationSentencesSourceAsync try { string sourceLanguage = "zh-Hans"; @@ -660,6 +1163,32 @@ public async void GetTextTranslationSentencesSourceAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + public void GetTextTranslationSentencesAuto() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTextTranslationSentencesAuto + try + { + IEnumerable inputTextElements = new[] + { + "How are you? I am fine. What did you do today?" + }; + + Response> response = client.FindSentenceBoundaries(inputTextElements); + IReadOnlyList brokenSentences = response.Value; + BreakSentenceItem brokenSentence = brokenSentences.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {brokenSentence?.DetectedLanguage?.Language} with score: {brokenSentence?.DetectedLanguage?.Score}."); + Console.WriteLine($"The detected sentece boundaries: '{string.Join(",", brokenSentence?.SentLen)}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -667,7 +1196,6 @@ public async void GetTextTranslationSentencesAutoAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTextTranslationSentencesAutoAsync try { IEnumerable inputTextElements = new[] @@ -687,6 +1215,33 @@ public async void GetTextTranslationSentencesAutoAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetTransliteratedText() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTransliteratedText + try + { + string language = "zh-Hans"; + string fromScript = "Hans"; + string toScript = "Latn"; + + string inputText = "这是个测试。"; + + Response> response = client.Transliterate(language, fromScript, toScript, inputText); + IReadOnlyList transliterations = response.Value; + TransliteratedText transliteration = transliterations.FirstOrDefault(); + + Console.WriteLine($"Input text was transliterated to '{transliteration?.Script}' script. Transliterated text: '{transliteration?.Text}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -695,7 +1250,6 @@ public async void GetTransliteratedTextAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTransliteratedTextAsync try { string language = "zh-Hans"; @@ -715,14 +1269,46 @@ public async void GetTransliteratedTextAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetTranslationTextTransliterated() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetTranslationTextTransliterated + try + { + string fromScript = "Latn"; + string fromLanguage = "ar"; + string toScript = "Latn"; + IEnumerable targetLanguages = new[] { "zh-Hans" }; + IEnumerable inputTextElements = new[] + { + "hudha akhtabar." + }; + + Response> response = client.Translate(targetLanguages, inputTextElements, sourceLanguage: fromLanguage, fromScript: fromScript, toScript: toScript); + IReadOnlyList translations = response.Value; + TranslatedTextItem translation = translations.FirstOrDefault(); + + Console.WriteLine($"Source Text: {translation.SourceText.Text}"); + Console.WriteLine($"Translation: '{translation?.Translations?.FirstOrDefault()?.Text}'."); + Console.WriteLine($"Transliterated text ({translation?.Translations?.FirstOrDefault()?.Transliteration?.Script}): {translation?.Translations?.FirstOrDefault()?.Transliteration?.Text}"); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } + [Test] public async void GetTranslationTextTransliteratedAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetTranslationTextTransliteratedAsync try { string fromScript = "Latn"; @@ -747,6 +1333,30 @@ public async void GetTranslationTextTransliteratedAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void FindTextSentenceSentenceBoundaries() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:FindTextSentenceBoundaries + try + { + string inputText = "How are you? I am fine. What did you do today?"; + + Response> response = client.FindSentenceBoundaries(inputText); + IReadOnlyList brokenSentences = response.Value; + BreakSentenceItem brokenSentence = brokenSentences.FirstOrDefault(); + + Console.WriteLine($"Detected languages of the input text: {brokenSentence?.DetectedLanguage?.Language} with score: {brokenSentence?.DetectedLanguage?.Score}."); + Console.WriteLine($"The detected sentece boundaries: '{string.Join(",", brokenSentence?.SentLen)}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -755,7 +1365,6 @@ public async void FindTextSentenceSentenceBoundariesAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:FindTextSentenceBoundariesAsync try { string inputText = "How are you? I am fine. What did you do today?"; @@ -772,6 +1381,32 @@ public async void FindTextSentenceSentenceBoundariesAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void LookupDictionaryEntries() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:LookupDictionaryEntries + try + { + string sourceLanguage = "en"; + string targetLanguage = "es"; + string inputText = "fly"; + + Response> response = client.LookupDictionaryEntries(sourceLanguage, targetLanguage, inputText); + IReadOnlyList dictionaryEntries = response.Value; + DictionaryLookupItem dictionaryEntry = dictionaryEntries.FirstOrDefault(); + + Console.WriteLine($"For the given input {dictionaryEntry?.Translations?.Count} entries were found in the dictionary."); + Console.WriteLine($"First entry: '{dictionaryEntry?.Translations?.FirstOrDefault()?.DisplayTarget}', confidence: {dictionaryEntry?.Translations?.FirstOrDefault()?.Confidence}."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -780,7 +1415,6 @@ public async void LookupDictionaryEntriesAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:LookupDictionaryEntriesAsync try { string sourceLanguage = "en"; @@ -799,6 +1433,36 @@ public async void LookupDictionaryEntriesAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void GetGrammaticalStructure() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:GetGrammaticalStructure + try + { + string sourceLanguage = "en"; + string targetLanguage = "es"; + IEnumerable inputTextElements = new[] + { + new InputTextWithTranslation("fly", "volar") + }; + + Response> response = client.LookupDictionaryExamples(sourceLanguage, targetLanguage, inputTextElements); + IReadOnlyList dictionaryEntries = response.Value; + DictionaryExampleItem dictionaryEntry = dictionaryEntries.FirstOrDefault(); + + Console.WriteLine($"For the given input {dictionaryEntry?.Examples?.Count} examples were found in the dictionary."); + DictionaryExample firstExample = dictionaryEntry?.Examples?.FirstOrDefault(); + Console.WriteLine($"Example: '{string.Concat(firstExample.TargetPrefix, firstExample.TargetTerm, firstExample.TargetSuffix)}'."); + } + catch (RequestFailedException exception) + { + Console.WriteLine($"Error Code: {exception.ErrorCode}"); + Console.WriteLine($"Message: {exception.Message}"); + } #endregion } @@ -807,7 +1471,6 @@ public async void GetGrammaticalStructureAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:GetGrammaticalStructureAsync try { string sourceLanguage = "en"; @@ -830,6 +1493,22 @@ public async void GetGrammaticalStructureAsync() Console.WriteLine($"Error Code: {exception.ErrorCode}"); Console.WriteLine($"Message: {exception.Message}"); } + } + + [Test] + public void HandleBadRequest() + { + TextTranslationClient client = CreateTextTranslationClient(); + + #region Snippet:HandleBadRequest + try + { + var translation = client.Translate(Array.Empty(), new[] { "This is a Test" }); + } + catch (RequestFailedException e) + { + Console.WriteLine(e.ToString()); + } #endregion } @@ -838,7 +1517,6 @@ public async void HandleBadRequestAsync() { TextTranslationClient client = CreateTextTranslationClient(); - #region Snippet:HandleBadRequestAsync try { var translation = await client.TranslateAsync(Array.Empty(), new[] { "This is a Test" }).ConfigureAwait(false); @@ -847,7 +1525,6 @@ public async void HandleBadRequestAsync() { Console.WriteLine(e.ToString()); } - #endregion } [Test]