| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using Azure.AI.TextAnalytics.Models; |
| | 7 | |
|
| | 8 | | namespace Azure.AI.TextAnalytics |
| | 9 | | { |
| | 10 | | internal static class Transforms |
| | 11 | | { |
| | 12 | | internal static TextAnalyticsError ConvertToError(TextAnalyticsError_internal error) |
| | 13 | | { |
| 8 | 14 | | string errorCode = error.Code; |
| 8 | 15 | | string message = error.Message; |
| 8 | 16 | | string target = error.Target; |
| 8 | 17 | | InnerError innerError = error.Innererror; |
| | 18 | |
|
| 8 | 19 | | if (innerError != null) |
| | 20 | | { |
| | 21 | | // Return the innermost error, which should be only one level down. |
| 8 | 22 | | return new TextAnalyticsError(innerError.Code, innerError.Message, innerError.Target); |
| | 23 | | } |
| | 24 | |
|
| 0 | 25 | | return new TextAnalyticsError(errorCode, message, target); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | internal static DetectedLanguage ConvertToDetectedLanguage(DocumentLanguage documentLanguage) |
| | 29 | | { |
| 84 | 30 | | var warnings = new List<TextAnalyticsWarning>(); |
| 0 | 31 | | foreach (TextAnalyticsWarning_internal warning in documentLanguage.Warnings) |
| | 32 | | { |
| 0 | 33 | | warnings.Add(new TextAnalyticsWarning(warning)); |
| | 34 | | } |
| | 35 | |
|
| 84 | 36 | | return new DetectedLanguage(documentLanguage.DetectedLanguage, warnings); |
| | 37 | | } |
| | 38 | |
|
| | 39 | | internal static DetectLanguageResultCollection ConvertLanguageResult(LanguageResult results, IDictionary<string, |
| | 40 | | { |
| 20 | 41 | | var detectedLanguages = new List<DetectLanguageResult>(); |
| | 42 | |
|
| | 43 | | //Read errors |
| 48 | 44 | | foreach (DocumentError error in results.Errors) |
| | 45 | | { |
| 4 | 46 | | detectedLanguages.Add(new DetectLanguageResult(error.Id, ConvertToError(error.Error))); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | //Read languages |
| 160 | 50 | | foreach (DocumentLanguage language in results.Documents) |
| | 51 | | { |
| 60 | 52 | | detectedLanguages.Add(new DetectLanguageResult(language.Id, language.Statistics ?? default, ConvertToDet |
| | 53 | | } |
| | 54 | |
|
| 20 | 55 | | detectedLanguages = SortHeterogeneousCollection(detectedLanguages, idToIndexMap); |
| | 56 | |
|
| 20 | 57 | | return new DetectLanguageResultCollection(detectedLanguages, results.Statistics, results.ModelVersion); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | private static List<T> SortHeterogeneousCollection<T>(List<T> collection, IDictionary<string, int> idToIndexMap) |
| | 61 | | { |
| 84 | 62 | | return collection.OrderBy(result => idToIndexMap[result.Id]).ToList(); |
| | 63 | | } |
| | 64 | | } |
| | 65 | | } |