示例#1
0
        /// <summary>
        /// Translates an array of strings from a source language to a target language.
        /// </summary>
        /// <param name="translateArraySourceTexts">The strings to translate.</param>
        /// <param name="sourceLanguage">The language code of the translation text. For example, "en" for English.</param>
        /// <param name="targetLanguage">The language code to translate the text into.</param>
        /// <returns>A task that represents the translation operation.
        /// The task result contains a list of the translated documents.</returns>
        public async Task <List <ITranslatedDocument> > TranslateArrayAsync(string[] translateArraySourceTexts, string sourceLanguage, string targetLanguage)
        {
            var translatedDocuments = new List <ITranslatedDocument>();

            for (var srcTxtIndx = 0; srcTxtIndx < translateArraySourceTexts.Length; srcTxtIndx++)
            {
                var currentTranslatedDocument = new TranslatedDocument(translateArraySourceTexts[srcTxtIndx]);
                translatedDocuments.Add(currentTranslatedDocument);

                // Check for literal tag in input user message
                _preProcessor.PreprocessMessage(currentTranslatedDocument.GetSourceMessage(), out var processedText, out var literanlNoTranslateList);
                currentTranslatedDocument.SetSourceMessage(processedText);
                translateArraySourceTexts[srcTxtIndx] = processedText;
                currentTranslatedDocument.SetLiteranlNoTranslatePhrases(literanlNoTranslateList);
            }

            // list of translation request for the service
            var payload = translateArraySourceTexts.Select(s => new TranslatorRequestModel {
                Text = s
            });

            using (var request = _requestBuilder.BuildTranslateRequest(sourceLanguage, targetLanguage, payload))
            {
                var translatedResults = await _responseGenerator.GenerateTranslateResponseAsync(request).ConfigureAwait(false);

                var sentIndex = 0;
                foreach (var translatedValue in translatedResults)
                {
                    var translation = translatedValue.Translations.First();
                    var currentTranslatedDocument = translatedDocuments[sentIndex];
                    currentTranslatedDocument.SetRawAlignment(translation.Alignment?.Projection ?? null);
                    currentTranslatedDocument.SetTranslatedMessage(translation.Text);

                    if (!string.IsNullOrEmpty(currentTranslatedDocument.GetRawAlignment()))
                    {
                        var alignments = currentTranslatedDocument.GetRawAlignment().Trim().Split(' ');
                        currentTranslatedDocument.SetSourceTokens(PostProcessingUtilities.SplitSentence(currentTranslatedDocument.GetSourceMessage(), alignments));
                        currentTranslatedDocument.SetTranslatedTokens(PostProcessingUtilities.SplitSentence(translation.Text, alignments, false));
                        currentTranslatedDocument.SetIndexedAlignment(PostProcessingUtilities.WordAlignmentParse(alignments, currentTranslatedDocument.GetSourceTokens(), currentTranslatedDocument.GetTranslatedTokens()));
                        currentTranslatedDocument.SetTranslatedMessage(PostProcessingUtilities.Join(" ", currentTranslatedDocument.GetTranslatedTokens()));
                    }
                    else
                    {
                        var translatedText = translation.Text;
                        currentTranslatedDocument.SetTranslatedMessage(translatedText);
                        currentTranslatedDocument.SetSourceTokens(new string[] { currentTranslatedDocument.GetSourceMessage() });
                        currentTranslatedDocument.SetTranslatedTokens(new string[] { currentTranslatedDocument.GetTranslatedMessage() });
                        currentTranslatedDocument.SetIndexedAlignment(new Dictionary <int, int>());
                    }

                    sentIndex++;
                }

                // post process all translated documents
                PostProcesseDocuments(translatedDocuments, sourceLanguage);
                return(translatedDocuments);
            }
        }
示例#2
0
        /// <summary>
        /// Translates an array of strings from a source language to a target language.
        /// </summary>
        /// <param name="translateArraySourceTexts">The strings to translate.</param>
        /// <param name="from">The language code of the translation text. For example, "en" for English.</param>
        /// <param name="to">The language code to translate the text into.</param>
        /// <returns>An array of the translated documents.</returns>
        public async Task <List <TranslatedDocument> > TranslateArrayAsync(string[] translateArraySourceTexts, string from, string to)
        {
            var translatedDocuments = new List <TranslatedDocument>();

            for (var srcTxtIndx = 0; srcTxtIndx < translateArraySourceTexts.Length; srcTxtIndx++)
            {
                // Check for literal tag in input user message
                var currentTranslatedDocument = new TranslatedDocument(translateArraySourceTexts[srcTxtIndx]);
                translatedDocuments.Add(currentTranslatedDocument);
                PreprocessMessage(currentTranslatedDocument.SourceMessage, out var processedText, out var literanlNoTranslateList);
                currentTranslatedDocument.SourceMessage = processedText;
                translateArraySourceTexts[srcTxtIndx]   = processedText;
                currentTranslatedDocument.LiteranlNoTranslatePhrases = literanlNoTranslateList;
            }

            // list of translation request for the service
            var payload = translateArraySourceTexts.Select(s => new TranslatorRequestModel {
                Text = s
            });

            using (var request = GetTranslateRequestMessage(from, to, payload))
            {
                using (var response = await _httpClient.SendAsync(request).ConfigureAwait(false))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        var translatedResults = JsonConvert.DeserializeObject <IEnumerable <TranslatedResult> >(responseBody);

                        var sentIndex = 0;
                        foreach (var translatedValue in translatedResults)
                        {
                            var translation = translatedValue.Translations.First();
                            var currentTranslatedDocument = translatedDocuments[sentIndex];
                            currentTranslatedDocument.RawAlignment  = translation.Alignment?.Projection ?? null;
                            currentTranslatedDocument.TargetMessage = translation.Text;

                            if (!string.IsNullOrEmpty(currentTranslatedDocument.RawAlignment))
                            {
                                var alignments = currentTranslatedDocument.RawAlignment.Trim().Split(' ');
                                currentTranslatedDocument.SourceTokens     = PostProcessingUtilities.SplitSentence(currentTranslatedDocument.SourceMessage, alignments);
                                currentTranslatedDocument.TranslatedTokens = PostProcessingUtilities.SplitSentence(translation.Text, alignments, false);
                                currentTranslatedDocument.IndexedAlignment = PostProcessingUtilities.WordAlignmentParse(alignments, currentTranslatedDocument.SourceTokens, currentTranslatedDocument.TranslatedTokens);
                                currentTranslatedDocument.TargetMessage    = PostProcessingUtilities.Join(" ", currentTranslatedDocument.TranslatedTokens);
                            }
                            else
                            {
                                var translatedText = translation.Text;
                                currentTranslatedDocument.TargetMessage    = translatedText;
                                currentTranslatedDocument.SourceTokens     = new string[] { currentTranslatedDocument.SourceMessage };
                                currentTranslatedDocument.TranslatedTokens = new string[] { currentTranslatedDocument.TargetMessage };
                                currentTranslatedDocument.IndexedAlignment = new Dictionary <int, int>();
                            }

                            sentIndex++;
                        }

                        return(translatedDocuments);
                    }
                    else
                    {
                        var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        var errorResult = JsonConvert.DeserializeObject <ErrorModel>(responseBody);
                        throw new ArgumentException(errorResult.Error.Message);
                    }
                }
            }
        }