< Summary

Class:Azure.AI.TextAnalytics.TextAnalyticsServiceSerializer
Assembly:Azure.AI.TextAnalytics
File(s):C:\Git\azure-sdk-for-net\sdk\textanalytics\Azure.AI.TextAnalytics\src\TextAnalyticsServiceSerializer.cs
Covered lines:271
Uncovered lines:6
Coverable lines:277
Total lines:606
Line coverage:97.8% (271 of 277)
Covered branches:145
Total branches:150
Branch coverage:96.6% (145 of 150)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
SerializeDocumentInputs(...)-100%100%
ReadDocumentId(...)-66.67%50%
ReadDocumentStatistics(...)-100%100%
ReadDocumentErrors(...)-100%100%
ReadTextAnalyticsError(...)-84.21%87.5%
ReadDocumentWarnings(...)-100%100%
ReadModelVersion(...)-66.67%50%
ReadDocumentBatchStatistics(...)-100%100%
DeserializeRecognizeEntitiesResponseAsync()-100%100%
DeserializeRecognizeEntitiesResponse(...)-100%100%
ReadRecognizeEntitiesResultCollection(...)-100%100%
SortHeterogeneousCollection(...)-100%100%
ReadRecognizeEntityResult(...)-100%100%
ReadCategorizedEntity(...)-100%100%
DeserializeAnalyzeSentimentResponseAsync()-100%100%
DeserializeAnalyzeSentimentResponse(...)-100%100%
ReadSentimentResult(...)-100%100%
ReadDocumentSentimentResult(...)-100%100%
ReadDocumentSentiment(...)-100%100%
ReadSentenceSentiment(...)-100%100%
DeserializeKeyPhraseResponseAsync()-100%100%
DeserializeKeyPhraseResponse(...)-100%100%
ReadKeyPhraseResultCollection(...)-100%100%
ReadKeyPhraseResult(...)-100%100%
DeserializeLinkedEntityResponseAsync()-100%100%
DeserializeLinkedEntityResponse(...)-100%100%
ReadLinkedEntityResultCollection(...)-100%100%
ReadLinkedEntityResult(...)-100%100%
ReadLinkedEntity(...)-100%100%
ReadLinkedEntityMatches(...)-91.67%87.5%

File(s)

C:\Git\azure-sdk-for-net\sdk\textanalytics\Azure.AI.TextAnalytics\src\TextAnalyticsServiceSerializer.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using Azure.Core;
 5using System;
 6using System.Collections.Generic;
 7using System.IO;
 8using System.Linq;
 9using System.Text.Json;
 10using System.Threading;
 11using System.Threading.Tasks;
 12
 13namespace Azure.AI.TextAnalytics
 14{
 15    internal static class TextAnalyticsServiceSerializer
 16    {
 17        // TODO (pri 2): make the deserializer version resilient
 18
 19        #region Serialize Inputs
 20
 221        private static readonly JsonEncodedText s_documents = JsonEncodedText.Encode("documents");
 222        private static readonly JsonEncodedText s_id = JsonEncodedText.Encode("id");
 223        private static readonly JsonEncodedText s_language = JsonEncodedText.Encode("language");
 224        private static readonly JsonEncodedText s_text = JsonEncodedText.Encode("text");
 25
 26        public static ReadOnlyMemory<byte> SerializeDocumentInputs(IEnumerable<TextDocumentInput> inputs, string default
 27        {
 13628            var writer = new ArrayBufferWriter<byte>();
 13629            var json = new Utf8JsonWriter(writer);
 13630            json.WriteStartObject();
 13631            json.WriteStartArray(s_documents);
 84832            foreach (var input in inputs)
 33            {
 28834                json.WriteStartObject();
 28835                json.WriteString(s_language, input.Language ?? defaultLanguage);
 28836                json.WriteString(s_id, input.Id);
 28837                json.WriteString(s_text, input.Text);
 28838                json.WriteEndObject();
 39            }
 13640            json.WriteEndArray();
 13641            json.WriteEndObject();
 13642            json.Flush();
 13643            return writer.WrittenMemory;
 44        }
 45
 46        #endregion Serialize Inputs
 47
 48        #region Deserialize Common
 49
 50        private static string ReadDocumentId(JsonElement documentElement)
 51        {
 21652            if (documentElement.TryGetProperty("id", out JsonElement idValue))
 21653                return idValue.ToString();
 54
 055            return default;
 56        }
 57
 58        private static TextDocumentStatistics ReadDocumentStatistics(JsonElement documentElement)
 59        {
 21660            if (documentElement.TryGetProperty("statistics", out JsonElement statisticsValue))
 61            {
 6462                int characterCount = default;
 6463                int transactionCount = default;
 64
 6465                if (statisticsValue.TryGetProperty("charactersCount", out JsonElement characterCountValue))
 6466                    characterCount = characterCountValue.GetInt32();
 6467                if (statisticsValue.TryGetProperty("transactionsCount", out JsonElement transactionCountValue))
 6468                    transactionCount = transactionCountValue.GetInt32();
 69
 6470                return new TextDocumentStatistics(characterCount, transactionCount);
 71            }
 72
 15273            return default;
 74        }
 75
 76        internal static IEnumerable<TextAnalyticsResult> ReadDocumentErrors(JsonElement documentElement)
 77        {
 13278            List<TextAnalyticsResult> errors = new List<TextAnalyticsResult>();
 79
 13280            if (documentElement.TryGetProperty("errors", out JsonElement errorsValue))
 81            {
 32082                foreach (JsonElement errorElement in errorsValue.EnumerateArray())
 83                {
 2884                    string id = default;
 85
 2886                    if (errorElement.TryGetProperty("id", out JsonElement idValue))
 2887                        id = idValue.ToString();
 2888                    if (errorElement.TryGetProperty("error", out JsonElement errorValue))
 89                    {
 2890                        errors.Add(new TextAnalyticsResult(id, ReadTextAnalyticsError(errorValue)));
 91                    }
 92                }
 93            }
 94
 13295            return errors;
 96        }
 97
 98        internal static TextAnalyticsError ReadTextAnalyticsError(JsonElement element)
 99        {
 72100            string errorCode = default;
 72101            string message = default;
 72102            string target = default;
 72103            TextAnalyticsError innerError = default;
 104
 472105            foreach (JsonProperty property in element.EnumerateObject())
 106            {
 164107                if (property.NameEquals("code"))
 108                {
 64109                    errorCode = property.Value.GetString();
 64110                    continue;
 111                }
 100112                if (property.NameEquals("message"))
 113                {
 64114                    message = property.Value.GetString();
 64115                    continue;
 116                }
 36117                if (property.NameEquals("target"))
 118                {
 0119                    if (property.Value.ValueKind == JsonValueKind.Null)
 120                    {
 121                        continue;
 122                    }
 0123                    target = property.Value.GetString();
 0124                    continue;
 125                }
 36126                if (property.NameEquals("innererror"))
 127                {
 36128                    if (property.Value.ValueKind == JsonValueKind.Null)
 129                    {
 130                        continue;
 131                    }
 36132                    innerError = ReadTextAnalyticsError(property.Value);
 133                    continue;
 134                }
 135            }
 136
 137            // Return the innermost error, which should be only one level down.
 72138            return innerError.ErrorCode == default ? new TextAnalyticsError(errorCode, message, target) : innerError;
 139        }
 140
 141        private static List<TextAnalyticsWarning> ReadDocumentWarnings(JsonElement documentElement)
 142        {
 216143            List<TextAnalyticsWarning> warnings = new List<TextAnalyticsWarning>();
 144
 440145            foreach (JsonElement warningElement in documentElement.EnumerateArray())
 146            {
 4147                string code = default;
 4148                string message = default;
 149
 4150                if (warningElement.TryGetProperty("code", out JsonElement codeValue))
 151                {
 4152                    code = codeValue.ToString();
 153                }
 154
 4155                if (warningElement.TryGetProperty("message", out JsonElement messageValue))
 156                {
 4157                    message = messageValue.ToString();
 158                }
 159
 4160                warnings.Add(new TextAnalyticsWarning(code, message));
 161            }
 162
 216163            return warnings;
 164        }
 165
 166        private static string ReadModelVersion(JsonElement documentElement)
 167        {
 128168            if (documentElement.TryGetProperty("modelVersion", out JsonElement modelVersionValue))
 169            {
 128170                return modelVersionValue.ToString();
 171            }
 172
 0173            return default;
 174        }
 175
 176        private static TextDocumentBatchStatistics ReadDocumentBatchStatistics(JsonElement documentElement)
 177        {
 128178            if (documentElement.TryGetProperty("statistics", out JsonElement statisticsElement))
 179            {
 32180                int documentCount = default;
 32181                int validDocumentCount = default;
 32182                int invalidDocumentCount = default;
 32183                long transactionCount = default;
 184
 32185                if (statisticsElement.TryGetProperty("documentsCount", out JsonElement documentCountValue))
 32186                    documentCount = documentCountValue.GetInt32();
 32187                if (statisticsElement.TryGetProperty("validDocumentsCount", out JsonElement validDocumentCountValue))
 32188                    validDocumentCount = validDocumentCountValue.GetInt32();
 32189                if (statisticsElement.TryGetProperty("erroneousDocumentsCount", out JsonElement erroneousDocumentCountVa
 32190                    invalidDocumentCount = erroneousDocumentCountValue.GetInt32();
 32191                if (statisticsElement.TryGetProperty("transactionsCount", out JsonElement transactionCountValue))
 32192                    transactionCount = transactionCountValue.GetInt64();
 193
 32194                return new TextDocumentBatchStatistics(documentCount, validDocumentCount, invalidDocumentCount, transact
 195            }
 196
 96197            return default;
 198        }
 199
 200        #endregion Deserialize Common
 201
 202        #region Recognize Entities
 203
 204        public static async Task<RecognizeEntitiesResultCollection> DeserializeRecognizeEntitiesResponseAsync(Stream con
 205        {
 20206            using JsonDocument json = await JsonDocument.ParseAsync(content, cancellationToken: cancellation).ConfigureA
 20207            JsonElement root = json.RootElement;
 20208            return ReadRecognizeEntitiesResultCollection(root, idToIndexMap);
 20209        }
 210
 211        public static RecognizeEntitiesResultCollection DeserializeRecognizeEntitiesResponse(Stream content, IDictionary
 212        {
 20213            using JsonDocument json = JsonDocument.Parse(content, default);
 20214            JsonElement root = json.RootElement;
 20215            return ReadRecognizeEntitiesResultCollection(root, idToIndexMap);
 20216        }
 217
 218        private static RecognizeEntitiesResultCollection ReadRecognizeEntitiesResultCollection(JsonElement root, IDictio
 219        {
 40220            var collection = new List<RecognizeEntitiesResult>();
 221
 40222            TextDocumentBatchStatistics statistics = ReadDocumentBatchStatistics(root);
 40223            string modelVersion = ReadModelVersion(root);
 224
 104225            foreach (var error in ReadDocumentErrors(root))
 226            {
 12227                collection.Add(new RecognizeEntitiesResult(error.Id, error.Error));
 228            }
 229
 40230            if (root.TryGetProperty("documents", out JsonElement documentsValue))
 231            {
 216232                foreach (JsonElement documentElement in documentsValue.EnumerateArray())
 233                {
 68234                    collection.Add(ReadRecognizeEntityResult(documentElement));
 235                }
 236            }
 237
 40238            collection = SortHeterogeneousCollection(collection, idToIndexMap);
 239
 40240            return new RecognizeEntitiesResultCollection(collection, statistics, modelVersion);
 241        }
 242
 243        private static List<T> SortHeterogeneousCollection<T>(List<T> collection, IDictionary<string, int> idToIndexMap)
 244        {
 368245            return collection.OrderBy(result => idToIndexMap[result.Id]).ToList();
 246        }
 247
 248        private static RecognizeEntitiesResult ReadRecognizeEntityResult(JsonElement documentElement)
 249        {
 68250            List<CategorizedEntity> entities = new List<CategorizedEntity>();
 68251            List<TextAnalyticsWarning> warnings = default;
 252
 68253            if (documentElement.TryGetProperty("entities", out JsonElement entitiesValue))
 254            {
 464255                foreach (JsonElement entityElement in entitiesValue.EnumerateArray())
 256                {
 164257                    entities.Add(ReadCategorizedEntity(entityElement));
 258                }
 259            }
 260
 68261            if (documentElement.TryGetProperty("warnings", out JsonElement warningsValue))
 262            {
 68263                warnings = ReadDocumentWarnings(warningsValue);
 264            }
 265
 68266            return new RecognizeEntitiesResult(
 68267                ReadDocumentId(documentElement),
 68268                ReadDocumentStatistics(documentElement),
 68269                new CategorizedEntityCollection(entities, warnings));
 270        }
 271
 272        private static CategorizedEntity ReadCategorizedEntity(JsonElement entityElement)
 273        {
 164274            string text = default;
 164275            string category = default;
 164276            string subcategory = default;
 164277            double confidenceScore = default;
 278
 164279            if (entityElement.TryGetProperty("text", out JsonElement textValue))
 164280                text = textValue.GetString();
 164281            if (entityElement.TryGetProperty("category", out JsonElement typeValue))
 164282                category = typeValue.ToString();
 164283            if (entityElement.TryGetProperty("subcategory", out JsonElement subTypeValue))
 56284                subcategory = subTypeValue.ToString();
 164285            if (entityElement.TryGetProperty("confidenceScore", out JsonElement scoreValue))
 164286                scoreValue.TryGetDouble(out confidenceScore);
 287
 164288            return new CategorizedEntity(text, category, subcategory, confidenceScore);
 289        }
 290
 291        #endregion Recognize Entities
 292
 293        #region Analyze Sentiment
 294
 295        public static async Task<AnalyzeSentimentResultCollection> DeserializeAnalyzeSentimentResponseAsync(Stream conte
 296        {
 14297            using JsonDocument json = await JsonDocument.ParseAsync(content, cancellationToken: cancellation).ConfigureA
 14298            JsonElement root = json.RootElement;
 14299            return ReadSentimentResult(root, idToIndexMap);
 14300        }
 301
 302        public static AnalyzeSentimentResultCollection DeserializeAnalyzeSentimentResponse(Stream content, IDictionary<s
 303        {
 14304            using JsonDocument json = JsonDocument.Parse(content, default);
 14305            JsonElement root = json.RootElement;
 14306            return ReadSentimentResult(root, idToIndexMap);
 14307        }
 308
 309        private static AnalyzeSentimentResultCollection ReadSentimentResult(JsonElement root, IDictionary<string, int> i
 310        {
 28311            var collection = new List<AnalyzeSentimentResult>();
 312
 28313            TextDocumentBatchStatistics statistics = ReadDocumentBatchStatistics(root);
 28314            string modelVersion = ReadModelVersion(root);
 315
 64316            foreach (var error in ReadDocumentErrors(root))
 317            {
 4318                collection.Add(new AnalyzeSentimentResult(error.Id, error.Error));
 319            }
 320
 28321            if (root.TryGetProperty("documents", out JsonElement documentsValue))
 322            {
 152323                foreach (JsonElement documentElement in documentsValue.EnumerateArray())
 324                {
 48325                    collection.Add(ReadDocumentSentimentResult(documentElement));
 326                }
 327            }
 328
 28329            collection = SortHeterogeneousCollection(collection, idToIndexMap);
 330
 28331            return new AnalyzeSentimentResultCollection(collection, statistics, modelVersion);
 332        }
 333
 334        private static AnalyzeSentimentResult ReadDocumentSentimentResult(JsonElement documentElement)
 335        {
 48336            List<TextAnalyticsWarning> warnings = default;
 48337            if (documentElement.TryGetProperty("warnings", out JsonElement warningsValue))
 338            {
 48339                warnings = ReadDocumentWarnings(warningsValue);
 340            }
 341
 48342            var documentSentiment = ReadDocumentSentiment(documentElement, "confidenceScores", warnings);
 343
 48344            return new AnalyzeSentimentResult(
 48345                    ReadDocumentId(documentElement),
 48346                    ReadDocumentStatistics(documentElement),
 48347                    documentSentiment);
 348        }
 349
 350        private static DocumentSentiment ReadDocumentSentiment(JsonElement documentElement, string scoresElementName, IL
 351        {
 48352            TextSentiment sentiment = default;
 48353            double positiveScore = default;
 48354            double neutralScore = default;
 48355            double negativeScore = default;
 356
 48357            if (documentElement.TryGetProperty("sentiment", out JsonElement sentimentValue))
 358            {
 48359                sentiment = (TextSentiment)Enum.Parse(typeof(TextSentiment), sentimentValue.ToString(), ignoreCase: true
 360            }
 361
 48362            if (documentElement.TryGetProperty(scoresElementName, out JsonElement scoreValues))
 363            {
 48364                if (scoreValues.TryGetProperty("positive", out JsonElement positiveValue))
 48365                    positiveValue.TryGetDouble(out positiveScore);
 366
 48367                if (scoreValues.TryGetProperty("neutral", out JsonElement neutralValue))
 48368                    neutralValue.TryGetDouble(out neutralScore);
 369
 48370                if (scoreValues.TryGetProperty("negative", out JsonElement negativeValue))
 48371                    negativeValue.TryGetDouble(out negativeScore);
 372            }
 373
 48374            var sentenceSentiments = new List<SentenceSentiment>();
 48375            if (documentElement.TryGetProperty("sentences", out JsonElement sentencesElement))
 376            {
 256377                foreach (JsonElement sentenceElement in sentencesElement.EnumerateArray())
 378                {
 80379                    sentenceSentiments.Add(ReadSentenceSentiment(sentenceElement, "confidenceScores"));
 380                }
 381            }
 382
 48383            return new DocumentSentiment(sentiment, positiveScore, neutralScore, negativeScore, sentenceSentiments, warn
 384        }
 385
 386        private static SentenceSentiment ReadSentenceSentiment(JsonElement documentElement, string scoresElementName)
 387        {
 80388            TextSentiment sentiment = default;
 80389            string text = default;
 80390            double positiveScore = default;
 80391            double neutralScore = default;
 80392            double negativeScore = default;
 393
 80394            if (documentElement.TryGetProperty("text", out JsonElement textValue))
 395            {
 80396                text = textValue.ToString();
 397            }
 398
 80399            if (documentElement.TryGetProperty("sentiment", out JsonElement sentimentValue))
 400            {
 80401                sentiment = (TextSentiment)Enum.Parse(typeof(TextSentiment), sentimentValue.ToString(), ignoreCase: true
 402            }
 403
 80404            if (documentElement.TryGetProperty(scoresElementName, out JsonElement scoreValues))
 405            {
 80406                if (scoreValues.TryGetProperty("positive", out JsonElement positiveValue))
 80407                    positiveValue.TryGetDouble(out positiveScore);
 408
 80409                if (scoreValues.TryGetProperty("neutral", out JsonElement neutralValue))
 80410                    neutralValue.TryGetDouble(out neutralScore);
 411
 80412                if (scoreValues.TryGetProperty("negative", out JsonElement negativeValue))
 80413                    negativeValue.TryGetDouble(out negativeScore);
 414            }
 415
 80416            return new SentenceSentiment(sentiment, text, positiveScore, neutralScore, negativeScore);
 417        }
 418
 419        #endregion
 420
 421        #region Extract Key Phrases
 422
 423        public static async Task<ExtractKeyPhrasesResultCollection> DeserializeKeyPhraseResponseAsync(Stream content, ID
 424        {
 16425            using JsonDocument json = await JsonDocument.ParseAsync(content, cancellationToken: cancellation).ConfigureA
 16426            JsonElement root = json.RootElement;
 16427            return ReadKeyPhraseResultCollection(root, idToIndexMap);
 16428        }
 429
 430        public static ExtractKeyPhrasesResultCollection DeserializeKeyPhraseResponse(Stream content, IDictionary<string,
 431        {
 16432            using JsonDocument json = JsonDocument.Parse(content, default);
 16433            JsonElement root = json.RootElement;
 16434            return ReadKeyPhraseResultCollection(root, idToIndexMap);
 16435        }
 436
 437        private static ExtractKeyPhrasesResultCollection ReadKeyPhraseResultCollection(JsonElement root, IDictionary<str
 438        {
 32439            var collection = new List<ExtractKeyPhrasesResult>();
 440
 32441            TextDocumentBatchStatistics statistics = ReadDocumentBatchStatistics(root);
 32442            string modelVersion = ReadModelVersion(root);
 443
 72444            foreach (var error in ReadDocumentErrors(root))
 445            {
 4446                collection.Add(new ExtractKeyPhrasesResult(error.Id, error.Error));
 447            }
 448
 32449            if (root.TryGetProperty("documents", out JsonElement documentsValue))
 450            {
 168451                foreach (JsonElement documentElement in documentsValue.EnumerateArray())
 452                {
 52453                    collection.Add(ReadKeyPhraseResult(documentElement));
 454                }
 455            }
 456
 32457            collection = SortHeterogeneousCollection(collection, idToIndexMap);
 458
 32459            return new ExtractKeyPhrasesResultCollection(collection, statistics, modelVersion);
 460        }
 461
 462        private static ExtractKeyPhrasesResult ReadKeyPhraseResult(JsonElement documentElement)
 463        {
 52464            List<string> keyPhrases = new List<string>();
 52465            List<TextAnalyticsWarning> warnings = default;
 466
 52467            if (documentElement.TryGetProperty("keyPhrases", out JsonElement keyPhrasesValue))
 468            {
 392469                foreach (JsonElement keyPhraseElement in keyPhrasesValue.EnumerateArray())
 470                {
 144471                    keyPhrases.Add(keyPhraseElement.ToString());
 472                }
 473            }
 474
 52475            if (documentElement.TryGetProperty("warnings", out JsonElement warningsValue))
 476            {
 52477                warnings = ReadDocumentWarnings(warningsValue);
 478            }
 479
 52480            return new ExtractKeyPhrasesResult(
 52481                ReadDocumentId(documentElement),
 52482                ReadDocumentStatistics(documentElement),
 52483                new KeyPhraseCollection(keyPhrases, warnings));
 484        }
 485
 486        #endregion Extract Key Phrases
 487
 488        #region Linked Entities
 489
 490        public static async Task<RecognizeLinkedEntitiesResultCollection> DeserializeLinkedEntityResponseAsync(Stream co
 491        {
 14492            using JsonDocument json = await JsonDocument.ParseAsync(content, cancellationToken: cancellation).ConfigureA
 14493            JsonElement root = json.RootElement;
 14494            return ReadLinkedEntityResultCollection(root, idToIndexMap);
 14495        }
 496
 497        public static RecognizeLinkedEntitiesResultCollection DeserializeLinkedEntityResponse(Stream content, IDictionar
 498        {
 14499            using JsonDocument json = JsonDocument.Parse(content, default);
 14500            JsonElement root = json.RootElement;
 14501            return ReadLinkedEntityResultCollection(root, idToIndexMap);
 14502        }
 503
 504        private static RecognizeLinkedEntitiesResultCollection ReadLinkedEntityResultCollection(JsonElement root, IDicti
 505        {
 28506            var collection = new List<RecognizeLinkedEntitiesResult>();
 507
 28508            TextDocumentBatchStatistics statistics = ReadDocumentBatchStatistics(root);
 28509            string modelVersion = ReadModelVersion(root);
 510
 64511            foreach (var error in ReadDocumentErrors(root))
 512            {
 4513                collection.Add(new RecognizeLinkedEntitiesResult(error.Id, error.Error));
 514            }
 515
 28516            if (root.TryGetProperty("documents", out JsonElement documentsValue))
 517            {
 152518                foreach (JsonElement documentElement in documentsValue.EnumerateArray())
 519                {
 48520                    collection.Add(ReadLinkedEntityResult(documentElement));
 521                }
 522            }
 523
 28524            collection = SortHeterogeneousCollection(collection, idToIndexMap);
 525
 28526            return new RecognizeLinkedEntitiesResultCollection(collection, statistics, modelVersion);
 527        }
 528
 529        private static RecognizeLinkedEntitiesResult ReadLinkedEntityResult(JsonElement documentElement)
 530        {
 48531            List<LinkedEntity> entities = new List<LinkedEntity>();
 48532            List<TextAnalyticsWarning> warnings = default;
 533
 48534            if (documentElement.TryGetProperty("entities", out JsonElement entitiesValue))
 535            {
 344536                foreach (JsonElement entityElement in entitiesValue.EnumerateArray())
 537                {
 124538                    entities.Add(ReadLinkedEntity(entityElement));
 539                }
 540            }
 541
 48542            if (documentElement.TryGetProperty("warnings", out JsonElement warningsValue))
 543            {
 48544                warnings = ReadDocumentWarnings(warningsValue);
 545            }
 546
 48547            return new RecognizeLinkedEntitiesResult(
 48548                ReadDocumentId(documentElement),
 48549                ReadDocumentStatistics(documentElement),
 48550                new LinkedEntityCollection(entities, warnings));
 551        }
 552
 553        private static LinkedEntity ReadLinkedEntity(JsonElement entityElement)
 554        {
 124555            string name = default;
 124556            string id = default;
 124557            string language = default;
 124558            string dataSource = default;
 124559            Uri url = default;
 560
 124561            if (entityElement.TryGetProperty("name", out JsonElement nameElement))
 124562                name = nameElement.ToString();
 124563            if (entityElement.TryGetProperty("id", out JsonElement idElement))
 124564                id = idElement.ToString();
 124565            if (entityElement.TryGetProperty("language", out JsonElement languageElement))
 124566                language = languageElement.ToString();
 124567            if (entityElement.TryGetProperty("dataSource", out JsonElement dataSourceValue))
 124568                dataSource = dataSourceValue.ToString();
 124569            if (entityElement.TryGetProperty("url", out JsonElement urlValue))
 124570                url = new Uri(urlValue.ToString());
 571
 124572            IEnumerable<LinkedEntityMatch> matches = ReadLinkedEntityMatches(entityElement);
 573
 124574            return new LinkedEntity(name, id, language, dataSource, url, matches);
 575        }
 576
 577        private static IEnumerable<LinkedEntityMatch> ReadLinkedEntityMatches(JsonElement entityElement)
 578        {
 124579            if (entityElement.TryGetProperty("matches", out JsonElement matchesElement))
 580            {
 124581                List<LinkedEntityMatch> matches = new List<LinkedEntityMatch>();
 582
 496583                foreach (JsonElement matchElement in matchesElement.EnumerateArray())
 584                {
 124585                    string text = default;
 124586                    double confidenceScore = default;
 587
 124588                    if (matchElement.TryGetProperty("text", out JsonElement textValue))
 124589                        text = textValue.ToString();
 590
 124591                    if (matchElement.TryGetProperty("confidenceScore", out JsonElement scoreValue))
 124592                        scoreValue.TryGetDouble(out confidenceScore);
 593
 124594                    matches.Add(new LinkedEntityMatch(text, confidenceScore));
 595                }
 596
 124597                return matches;
 598            }
 599
 0600            return default;
 601        }
 602
 603        #endregion  Entity Linking
 604
 605    }
 606}