| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.Core; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.IO; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Text.Json; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | |
|
| | 13 | | namespace 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 | |
|
| 2 | 21 | | private static readonly JsonEncodedText s_documents = JsonEncodedText.Encode("documents"); |
| 2 | 22 | | private static readonly JsonEncodedText s_id = JsonEncodedText.Encode("id"); |
| 2 | 23 | | private static readonly JsonEncodedText s_language = JsonEncodedText.Encode("language"); |
| 2 | 24 | | private static readonly JsonEncodedText s_text = JsonEncodedText.Encode("text"); |
| | 25 | |
|
| | 26 | | public static ReadOnlyMemory<byte> SerializeDocumentInputs(IEnumerable<TextDocumentInput> inputs, string default |
| | 27 | | { |
| 136 | 28 | | var writer = new ArrayBufferWriter<byte>(); |
| 136 | 29 | | var json = new Utf8JsonWriter(writer); |
| 136 | 30 | | json.WriteStartObject(); |
| 136 | 31 | | json.WriteStartArray(s_documents); |
| 848 | 32 | | foreach (var input in inputs) |
| | 33 | | { |
| 288 | 34 | | json.WriteStartObject(); |
| 288 | 35 | | json.WriteString(s_language, input.Language ?? defaultLanguage); |
| 288 | 36 | | json.WriteString(s_id, input.Id); |
| 288 | 37 | | json.WriteString(s_text, input.Text); |
| 288 | 38 | | json.WriteEndObject(); |
| | 39 | | } |
| 136 | 40 | | json.WriteEndArray(); |
| 136 | 41 | | json.WriteEndObject(); |
| 136 | 42 | | json.Flush(); |
| 136 | 43 | | return writer.WrittenMemory; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | #endregion Serialize Inputs |
| | 47 | |
|
| | 48 | | #region Deserialize Common |
| | 49 | |
|
| | 50 | | private static string ReadDocumentId(JsonElement documentElement) |
| | 51 | | { |
| 216 | 52 | | if (documentElement.TryGetProperty("id", out JsonElement idValue)) |
| 216 | 53 | | return idValue.ToString(); |
| | 54 | |
|
| 0 | 55 | | return default; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | private static TextDocumentStatistics ReadDocumentStatistics(JsonElement documentElement) |
| | 59 | | { |
| 216 | 60 | | if (documentElement.TryGetProperty("statistics", out JsonElement statisticsValue)) |
| | 61 | | { |
| 64 | 62 | | int characterCount = default; |
| 64 | 63 | | int transactionCount = default; |
| | 64 | |
|
| 64 | 65 | | if (statisticsValue.TryGetProperty("charactersCount", out JsonElement characterCountValue)) |
| 64 | 66 | | characterCount = characterCountValue.GetInt32(); |
| 64 | 67 | | if (statisticsValue.TryGetProperty("transactionsCount", out JsonElement transactionCountValue)) |
| 64 | 68 | | transactionCount = transactionCountValue.GetInt32(); |
| | 69 | |
|
| 64 | 70 | | return new TextDocumentStatistics(characterCount, transactionCount); |
| | 71 | | } |
| | 72 | |
|
| 152 | 73 | | return default; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | internal static IEnumerable<TextAnalyticsResult> ReadDocumentErrors(JsonElement documentElement) |
| | 77 | | { |
| 132 | 78 | | List<TextAnalyticsResult> errors = new List<TextAnalyticsResult>(); |
| | 79 | |
|
| 132 | 80 | | if (documentElement.TryGetProperty("errors", out JsonElement errorsValue)) |
| | 81 | | { |
| 320 | 82 | | foreach (JsonElement errorElement in errorsValue.EnumerateArray()) |
| | 83 | | { |
| 28 | 84 | | string id = default; |
| | 85 | |
|
| 28 | 86 | | if (errorElement.TryGetProperty("id", out JsonElement idValue)) |
| 28 | 87 | | id = idValue.ToString(); |
| 28 | 88 | | if (errorElement.TryGetProperty("error", out JsonElement errorValue)) |
| | 89 | | { |
| 28 | 90 | | errors.Add(new TextAnalyticsResult(id, ReadTextAnalyticsError(errorValue))); |
| | 91 | | } |
| | 92 | | } |
| | 93 | | } |
| | 94 | |
|
| 132 | 95 | | return errors; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | internal static TextAnalyticsError ReadTextAnalyticsError(JsonElement element) |
| | 99 | | { |
| 72 | 100 | | string errorCode = default; |
| 72 | 101 | | string message = default; |
| 72 | 102 | | string target = default; |
| 72 | 103 | | TextAnalyticsError innerError = default; |
| | 104 | |
|
| 472 | 105 | | foreach (JsonProperty property in element.EnumerateObject()) |
| | 106 | | { |
| 164 | 107 | | if (property.NameEquals("code")) |
| | 108 | | { |
| 64 | 109 | | errorCode = property.Value.GetString(); |
| 64 | 110 | | continue; |
| | 111 | | } |
| 100 | 112 | | if (property.NameEquals("message")) |
| | 113 | | { |
| 64 | 114 | | message = property.Value.GetString(); |
| 64 | 115 | | continue; |
| | 116 | | } |
| 36 | 117 | | if (property.NameEquals("target")) |
| | 118 | | { |
| 0 | 119 | | if (property.Value.ValueKind == JsonValueKind.Null) |
| | 120 | | { |
| | 121 | | continue; |
| | 122 | | } |
| 0 | 123 | | target = property.Value.GetString(); |
| 0 | 124 | | continue; |
| | 125 | | } |
| 36 | 126 | | if (property.NameEquals("innererror")) |
| | 127 | | { |
| 36 | 128 | | if (property.Value.ValueKind == JsonValueKind.Null) |
| | 129 | | { |
| | 130 | | continue; |
| | 131 | | } |
| 36 | 132 | | innerError = ReadTextAnalyticsError(property.Value); |
| | 133 | | continue; |
| | 134 | | } |
| | 135 | | } |
| | 136 | |
|
| | 137 | | // Return the innermost error, which should be only one level down. |
| 72 | 138 | | return innerError.ErrorCode == default ? new TextAnalyticsError(errorCode, message, target) : innerError; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | private static List<TextAnalyticsWarning> ReadDocumentWarnings(JsonElement documentElement) |
| | 142 | | { |
| 216 | 143 | | List<TextAnalyticsWarning> warnings = new List<TextAnalyticsWarning>(); |
| | 144 | |
|
| 440 | 145 | | foreach (JsonElement warningElement in documentElement.EnumerateArray()) |
| | 146 | | { |
| 4 | 147 | | string code = default; |
| 4 | 148 | | string message = default; |
| | 149 | |
|
| 4 | 150 | | if (warningElement.TryGetProperty("code", out JsonElement codeValue)) |
| | 151 | | { |
| 4 | 152 | | code = codeValue.ToString(); |
| | 153 | | } |
| | 154 | |
|
| 4 | 155 | | if (warningElement.TryGetProperty("message", out JsonElement messageValue)) |
| | 156 | | { |
| 4 | 157 | | message = messageValue.ToString(); |
| | 158 | | } |
| | 159 | |
|
| 4 | 160 | | warnings.Add(new TextAnalyticsWarning(code, message)); |
| | 161 | | } |
| | 162 | |
|
| 216 | 163 | | return warnings; |
| | 164 | | } |
| | 165 | |
|
| | 166 | | private static string ReadModelVersion(JsonElement documentElement) |
| | 167 | | { |
| 128 | 168 | | if (documentElement.TryGetProperty("modelVersion", out JsonElement modelVersionValue)) |
| | 169 | | { |
| 128 | 170 | | return modelVersionValue.ToString(); |
| | 171 | | } |
| | 172 | |
|
| 0 | 173 | | return default; |
| | 174 | | } |
| | 175 | |
|
| | 176 | | private static TextDocumentBatchStatistics ReadDocumentBatchStatistics(JsonElement documentElement) |
| | 177 | | { |
| 128 | 178 | | if (documentElement.TryGetProperty("statistics", out JsonElement statisticsElement)) |
| | 179 | | { |
| 32 | 180 | | int documentCount = default; |
| 32 | 181 | | int validDocumentCount = default; |
| 32 | 182 | | int invalidDocumentCount = default; |
| 32 | 183 | | long transactionCount = default; |
| | 184 | |
|
| 32 | 185 | | if (statisticsElement.TryGetProperty("documentsCount", out JsonElement documentCountValue)) |
| 32 | 186 | | documentCount = documentCountValue.GetInt32(); |
| 32 | 187 | | if (statisticsElement.TryGetProperty("validDocumentsCount", out JsonElement validDocumentCountValue)) |
| 32 | 188 | | validDocumentCount = validDocumentCountValue.GetInt32(); |
| 32 | 189 | | if (statisticsElement.TryGetProperty("erroneousDocumentsCount", out JsonElement erroneousDocumentCountVa |
| 32 | 190 | | invalidDocumentCount = erroneousDocumentCountValue.GetInt32(); |
| 32 | 191 | | if (statisticsElement.TryGetProperty("transactionsCount", out JsonElement transactionCountValue)) |
| 32 | 192 | | transactionCount = transactionCountValue.GetInt64(); |
| | 193 | |
|
| 32 | 194 | | return new TextDocumentBatchStatistics(documentCount, validDocumentCount, invalidDocumentCount, transact |
| | 195 | | } |
| | 196 | |
|
| 96 | 197 | | return default; |
| | 198 | | } |
| | 199 | |
|
| | 200 | | #endregion Deserialize Common |
| | 201 | |
|
| | 202 | | #region Recognize Entities |
| | 203 | |
|
| | 204 | | public static async Task<RecognizeEntitiesResultCollection> DeserializeRecognizeEntitiesResponseAsync(Stream con |
| | 205 | | { |
| 20 | 206 | | using JsonDocument json = await JsonDocument.ParseAsync(content, cancellationToken: cancellation).ConfigureA |
| 20 | 207 | | JsonElement root = json.RootElement; |
| 20 | 208 | | return ReadRecognizeEntitiesResultCollection(root, idToIndexMap); |
| 20 | 209 | | } |
| | 210 | |
|
| | 211 | | public static RecognizeEntitiesResultCollection DeserializeRecognizeEntitiesResponse(Stream content, IDictionary |
| | 212 | | { |
| 20 | 213 | | using JsonDocument json = JsonDocument.Parse(content, default); |
| 20 | 214 | | JsonElement root = json.RootElement; |
| 20 | 215 | | return ReadRecognizeEntitiesResultCollection(root, idToIndexMap); |
| 20 | 216 | | } |
| | 217 | |
|
| | 218 | | private static RecognizeEntitiesResultCollection ReadRecognizeEntitiesResultCollection(JsonElement root, IDictio |
| | 219 | | { |
| 40 | 220 | | var collection = new List<RecognizeEntitiesResult>(); |
| | 221 | |
|
| 40 | 222 | | TextDocumentBatchStatistics statistics = ReadDocumentBatchStatistics(root); |
| 40 | 223 | | string modelVersion = ReadModelVersion(root); |
| | 224 | |
|
| 104 | 225 | | foreach (var error in ReadDocumentErrors(root)) |
| | 226 | | { |
| 12 | 227 | | collection.Add(new RecognizeEntitiesResult(error.Id, error.Error)); |
| | 228 | | } |
| | 229 | |
|
| 40 | 230 | | if (root.TryGetProperty("documents", out JsonElement documentsValue)) |
| | 231 | | { |
| 216 | 232 | | foreach (JsonElement documentElement in documentsValue.EnumerateArray()) |
| | 233 | | { |
| 68 | 234 | | collection.Add(ReadRecognizeEntityResult(documentElement)); |
| | 235 | | } |
| | 236 | | } |
| | 237 | |
|
| 40 | 238 | | collection = SortHeterogeneousCollection(collection, idToIndexMap); |
| | 239 | |
|
| 40 | 240 | | return new RecognizeEntitiesResultCollection(collection, statistics, modelVersion); |
| | 241 | | } |
| | 242 | |
|
| | 243 | | private static List<T> SortHeterogeneousCollection<T>(List<T> collection, IDictionary<string, int> idToIndexMap) |
| | 244 | | { |
| 368 | 245 | | return collection.OrderBy(result => idToIndexMap[result.Id]).ToList(); |
| | 246 | | } |
| | 247 | |
|
| | 248 | | private static RecognizeEntitiesResult ReadRecognizeEntityResult(JsonElement documentElement) |
| | 249 | | { |
| 68 | 250 | | List<CategorizedEntity> entities = new List<CategorizedEntity>(); |
| 68 | 251 | | List<TextAnalyticsWarning> warnings = default; |
| | 252 | |
|
| 68 | 253 | | if (documentElement.TryGetProperty("entities", out JsonElement entitiesValue)) |
| | 254 | | { |
| 464 | 255 | | foreach (JsonElement entityElement in entitiesValue.EnumerateArray()) |
| | 256 | | { |
| 164 | 257 | | entities.Add(ReadCategorizedEntity(entityElement)); |
| | 258 | | } |
| | 259 | | } |
| | 260 | |
|
| 68 | 261 | | if (documentElement.TryGetProperty("warnings", out JsonElement warningsValue)) |
| | 262 | | { |
| 68 | 263 | | warnings = ReadDocumentWarnings(warningsValue); |
| | 264 | | } |
| | 265 | |
|
| 68 | 266 | | return new RecognizeEntitiesResult( |
| 68 | 267 | | ReadDocumentId(documentElement), |
| 68 | 268 | | ReadDocumentStatistics(documentElement), |
| 68 | 269 | | new CategorizedEntityCollection(entities, warnings)); |
| | 270 | | } |
| | 271 | |
|
| | 272 | | private static CategorizedEntity ReadCategorizedEntity(JsonElement entityElement) |
| | 273 | | { |
| 164 | 274 | | string text = default; |
| 164 | 275 | | string category = default; |
| 164 | 276 | | string subcategory = default; |
| 164 | 277 | | double confidenceScore = default; |
| | 278 | |
|
| 164 | 279 | | if (entityElement.TryGetProperty("text", out JsonElement textValue)) |
| 164 | 280 | | text = textValue.GetString(); |
| 164 | 281 | | if (entityElement.TryGetProperty("category", out JsonElement typeValue)) |
| 164 | 282 | | category = typeValue.ToString(); |
| 164 | 283 | | if (entityElement.TryGetProperty("subcategory", out JsonElement subTypeValue)) |
| 56 | 284 | | subcategory = subTypeValue.ToString(); |
| 164 | 285 | | if (entityElement.TryGetProperty("confidenceScore", out JsonElement scoreValue)) |
| 164 | 286 | | scoreValue.TryGetDouble(out confidenceScore); |
| | 287 | |
|
| 164 | 288 | | 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 | | { |
| 14 | 297 | | using JsonDocument json = await JsonDocument.ParseAsync(content, cancellationToken: cancellation).ConfigureA |
| 14 | 298 | | JsonElement root = json.RootElement; |
| 14 | 299 | | return ReadSentimentResult(root, idToIndexMap); |
| 14 | 300 | | } |
| | 301 | |
|
| | 302 | | public static AnalyzeSentimentResultCollection DeserializeAnalyzeSentimentResponse(Stream content, IDictionary<s |
| | 303 | | { |
| 14 | 304 | | using JsonDocument json = JsonDocument.Parse(content, default); |
| 14 | 305 | | JsonElement root = json.RootElement; |
| 14 | 306 | | return ReadSentimentResult(root, idToIndexMap); |
| 14 | 307 | | } |
| | 308 | |
|
| | 309 | | private static AnalyzeSentimentResultCollection ReadSentimentResult(JsonElement root, IDictionary<string, int> i |
| | 310 | | { |
| 28 | 311 | | var collection = new List<AnalyzeSentimentResult>(); |
| | 312 | |
|
| 28 | 313 | | TextDocumentBatchStatistics statistics = ReadDocumentBatchStatistics(root); |
| 28 | 314 | | string modelVersion = ReadModelVersion(root); |
| | 315 | |
|
| 64 | 316 | | foreach (var error in ReadDocumentErrors(root)) |
| | 317 | | { |
| 4 | 318 | | collection.Add(new AnalyzeSentimentResult(error.Id, error.Error)); |
| | 319 | | } |
| | 320 | |
|
| 28 | 321 | | if (root.TryGetProperty("documents", out JsonElement documentsValue)) |
| | 322 | | { |
| 152 | 323 | | foreach (JsonElement documentElement in documentsValue.EnumerateArray()) |
| | 324 | | { |
| 48 | 325 | | collection.Add(ReadDocumentSentimentResult(documentElement)); |
| | 326 | | } |
| | 327 | | } |
| | 328 | |
|
| 28 | 329 | | collection = SortHeterogeneousCollection(collection, idToIndexMap); |
| | 330 | |
|
| 28 | 331 | | return new AnalyzeSentimentResultCollection(collection, statistics, modelVersion); |
| | 332 | | } |
| | 333 | |
|
| | 334 | | private static AnalyzeSentimentResult ReadDocumentSentimentResult(JsonElement documentElement) |
| | 335 | | { |
| 48 | 336 | | List<TextAnalyticsWarning> warnings = default; |
| 48 | 337 | | if (documentElement.TryGetProperty("warnings", out JsonElement warningsValue)) |
| | 338 | | { |
| 48 | 339 | | warnings = ReadDocumentWarnings(warningsValue); |
| | 340 | | } |
| | 341 | |
|
| 48 | 342 | | var documentSentiment = ReadDocumentSentiment(documentElement, "confidenceScores", warnings); |
| | 343 | |
|
| 48 | 344 | | return new AnalyzeSentimentResult( |
| 48 | 345 | | ReadDocumentId(documentElement), |
| 48 | 346 | | ReadDocumentStatistics(documentElement), |
| 48 | 347 | | documentSentiment); |
| | 348 | | } |
| | 349 | |
|
| | 350 | | private static DocumentSentiment ReadDocumentSentiment(JsonElement documentElement, string scoresElementName, IL |
| | 351 | | { |
| 48 | 352 | | TextSentiment sentiment = default; |
| 48 | 353 | | double positiveScore = default; |
| 48 | 354 | | double neutralScore = default; |
| 48 | 355 | | double negativeScore = default; |
| | 356 | |
|
| 48 | 357 | | if (documentElement.TryGetProperty("sentiment", out JsonElement sentimentValue)) |
| | 358 | | { |
| 48 | 359 | | sentiment = (TextSentiment)Enum.Parse(typeof(TextSentiment), sentimentValue.ToString(), ignoreCase: true |
| | 360 | | } |
| | 361 | |
|
| 48 | 362 | | if (documentElement.TryGetProperty(scoresElementName, out JsonElement scoreValues)) |
| | 363 | | { |
| 48 | 364 | | if (scoreValues.TryGetProperty("positive", out JsonElement positiveValue)) |
| 48 | 365 | | positiveValue.TryGetDouble(out positiveScore); |
| | 366 | |
|
| 48 | 367 | | if (scoreValues.TryGetProperty("neutral", out JsonElement neutralValue)) |
| 48 | 368 | | neutralValue.TryGetDouble(out neutralScore); |
| | 369 | |
|
| 48 | 370 | | if (scoreValues.TryGetProperty("negative", out JsonElement negativeValue)) |
| 48 | 371 | | negativeValue.TryGetDouble(out negativeScore); |
| | 372 | | } |
| | 373 | |
|
| 48 | 374 | | var sentenceSentiments = new List<SentenceSentiment>(); |
| 48 | 375 | | if (documentElement.TryGetProperty("sentences", out JsonElement sentencesElement)) |
| | 376 | | { |
| 256 | 377 | | foreach (JsonElement sentenceElement in sentencesElement.EnumerateArray()) |
| | 378 | | { |
| 80 | 379 | | sentenceSentiments.Add(ReadSentenceSentiment(sentenceElement, "confidenceScores")); |
| | 380 | | } |
| | 381 | | } |
| | 382 | |
|
| 48 | 383 | | return new DocumentSentiment(sentiment, positiveScore, neutralScore, negativeScore, sentenceSentiments, warn |
| | 384 | | } |
| | 385 | |
|
| | 386 | | private static SentenceSentiment ReadSentenceSentiment(JsonElement documentElement, string scoresElementName) |
| | 387 | | { |
| 80 | 388 | | TextSentiment sentiment = default; |
| 80 | 389 | | string text = default; |
| 80 | 390 | | double positiveScore = default; |
| 80 | 391 | | double neutralScore = default; |
| 80 | 392 | | double negativeScore = default; |
| | 393 | |
|
| 80 | 394 | | if (documentElement.TryGetProperty("text", out JsonElement textValue)) |
| | 395 | | { |
| 80 | 396 | | text = textValue.ToString(); |
| | 397 | | } |
| | 398 | |
|
| 80 | 399 | | if (documentElement.TryGetProperty("sentiment", out JsonElement sentimentValue)) |
| | 400 | | { |
| 80 | 401 | | sentiment = (TextSentiment)Enum.Parse(typeof(TextSentiment), sentimentValue.ToString(), ignoreCase: true |
| | 402 | | } |
| | 403 | |
|
| 80 | 404 | | if (documentElement.TryGetProperty(scoresElementName, out JsonElement scoreValues)) |
| | 405 | | { |
| 80 | 406 | | if (scoreValues.TryGetProperty("positive", out JsonElement positiveValue)) |
| 80 | 407 | | positiveValue.TryGetDouble(out positiveScore); |
| | 408 | |
|
| 80 | 409 | | if (scoreValues.TryGetProperty("neutral", out JsonElement neutralValue)) |
| 80 | 410 | | neutralValue.TryGetDouble(out neutralScore); |
| | 411 | |
|
| 80 | 412 | | if (scoreValues.TryGetProperty("negative", out JsonElement negativeValue)) |
| 80 | 413 | | negativeValue.TryGetDouble(out negativeScore); |
| | 414 | | } |
| | 415 | |
|
| 80 | 416 | | 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 | | { |
| 16 | 425 | | using JsonDocument json = await JsonDocument.ParseAsync(content, cancellationToken: cancellation).ConfigureA |
| 16 | 426 | | JsonElement root = json.RootElement; |
| 16 | 427 | | return ReadKeyPhraseResultCollection(root, idToIndexMap); |
| 16 | 428 | | } |
| | 429 | |
|
| | 430 | | public static ExtractKeyPhrasesResultCollection DeserializeKeyPhraseResponse(Stream content, IDictionary<string, |
| | 431 | | { |
| 16 | 432 | | using JsonDocument json = JsonDocument.Parse(content, default); |
| 16 | 433 | | JsonElement root = json.RootElement; |
| 16 | 434 | | return ReadKeyPhraseResultCollection(root, idToIndexMap); |
| 16 | 435 | | } |
| | 436 | |
|
| | 437 | | private static ExtractKeyPhrasesResultCollection ReadKeyPhraseResultCollection(JsonElement root, IDictionary<str |
| | 438 | | { |
| 32 | 439 | | var collection = new List<ExtractKeyPhrasesResult>(); |
| | 440 | |
|
| 32 | 441 | | TextDocumentBatchStatistics statistics = ReadDocumentBatchStatistics(root); |
| 32 | 442 | | string modelVersion = ReadModelVersion(root); |
| | 443 | |
|
| 72 | 444 | | foreach (var error in ReadDocumentErrors(root)) |
| | 445 | | { |
| 4 | 446 | | collection.Add(new ExtractKeyPhrasesResult(error.Id, error.Error)); |
| | 447 | | } |
| | 448 | |
|
| 32 | 449 | | if (root.TryGetProperty("documents", out JsonElement documentsValue)) |
| | 450 | | { |
| 168 | 451 | | foreach (JsonElement documentElement in documentsValue.EnumerateArray()) |
| | 452 | | { |
| 52 | 453 | | collection.Add(ReadKeyPhraseResult(documentElement)); |
| | 454 | | } |
| | 455 | | } |
| | 456 | |
|
| 32 | 457 | | collection = SortHeterogeneousCollection(collection, idToIndexMap); |
| | 458 | |
|
| 32 | 459 | | return new ExtractKeyPhrasesResultCollection(collection, statistics, modelVersion); |
| | 460 | | } |
| | 461 | |
|
| | 462 | | private static ExtractKeyPhrasesResult ReadKeyPhraseResult(JsonElement documentElement) |
| | 463 | | { |
| 52 | 464 | | List<string> keyPhrases = new List<string>(); |
| 52 | 465 | | List<TextAnalyticsWarning> warnings = default; |
| | 466 | |
|
| 52 | 467 | | if (documentElement.TryGetProperty("keyPhrases", out JsonElement keyPhrasesValue)) |
| | 468 | | { |
| 392 | 469 | | foreach (JsonElement keyPhraseElement in keyPhrasesValue.EnumerateArray()) |
| | 470 | | { |
| 144 | 471 | | keyPhrases.Add(keyPhraseElement.ToString()); |
| | 472 | | } |
| | 473 | | } |
| | 474 | |
|
| 52 | 475 | | if (documentElement.TryGetProperty("warnings", out JsonElement warningsValue)) |
| | 476 | | { |
| 52 | 477 | | warnings = ReadDocumentWarnings(warningsValue); |
| | 478 | | } |
| | 479 | |
|
| 52 | 480 | | return new ExtractKeyPhrasesResult( |
| 52 | 481 | | ReadDocumentId(documentElement), |
| 52 | 482 | | ReadDocumentStatistics(documentElement), |
| 52 | 483 | | 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 | | { |
| 14 | 492 | | using JsonDocument json = await JsonDocument.ParseAsync(content, cancellationToken: cancellation).ConfigureA |
| 14 | 493 | | JsonElement root = json.RootElement; |
| 14 | 494 | | return ReadLinkedEntityResultCollection(root, idToIndexMap); |
| 14 | 495 | | } |
| | 496 | |
|
| | 497 | | public static RecognizeLinkedEntitiesResultCollection DeserializeLinkedEntityResponse(Stream content, IDictionar |
| | 498 | | { |
| 14 | 499 | | using JsonDocument json = JsonDocument.Parse(content, default); |
| 14 | 500 | | JsonElement root = json.RootElement; |
| 14 | 501 | | return ReadLinkedEntityResultCollection(root, idToIndexMap); |
| 14 | 502 | | } |
| | 503 | |
|
| | 504 | | private static RecognizeLinkedEntitiesResultCollection ReadLinkedEntityResultCollection(JsonElement root, IDicti |
| | 505 | | { |
| 28 | 506 | | var collection = new List<RecognizeLinkedEntitiesResult>(); |
| | 507 | |
|
| 28 | 508 | | TextDocumentBatchStatistics statistics = ReadDocumentBatchStatistics(root); |
| 28 | 509 | | string modelVersion = ReadModelVersion(root); |
| | 510 | |
|
| 64 | 511 | | foreach (var error in ReadDocumentErrors(root)) |
| | 512 | | { |
| 4 | 513 | | collection.Add(new RecognizeLinkedEntitiesResult(error.Id, error.Error)); |
| | 514 | | } |
| | 515 | |
|
| 28 | 516 | | if (root.TryGetProperty("documents", out JsonElement documentsValue)) |
| | 517 | | { |
| 152 | 518 | | foreach (JsonElement documentElement in documentsValue.EnumerateArray()) |
| | 519 | | { |
| 48 | 520 | | collection.Add(ReadLinkedEntityResult(documentElement)); |
| | 521 | | } |
| | 522 | | } |
| | 523 | |
|
| 28 | 524 | | collection = SortHeterogeneousCollection(collection, idToIndexMap); |
| | 525 | |
|
| 28 | 526 | | return new RecognizeLinkedEntitiesResultCollection(collection, statistics, modelVersion); |
| | 527 | | } |
| | 528 | |
|
| | 529 | | private static RecognizeLinkedEntitiesResult ReadLinkedEntityResult(JsonElement documentElement) |
| | 530 | | { |
| 48 | 531 | | List<LinkedEntity> entities = new List<LinkedEntity>(); |
| 48 | 532 | | List<TextAnalyticsWarning> warnings = default; |
| | 533 | |
|
| 48 | 534 | | if (documentElement.TryGetProperty("entities", out JsonElement entitiesValue)) |
| | 535 | | { |
| 344 | 536 | | foreach (JsonElement entityElement in entitiesValue.EnumerateArray()) |
| | 537 | | { |
| 124 | 538 | | entities.Add(ReadLinkedEntity(entityElement)); |
| | 539 | | } |
| | 540 | | } |
| | 541 | |
|
| 48 | 542 | | if (documentElement.TryGetProperty("warnings", out JsonElement warningsValue)) |
| | 543 | | { |
| 48 | 544 | | warnings = ReadDocumentWarnings(warningsValue); |
| | 545 | | } |
| | 546 | |
|
| 48 | 547 | | return new RecognizeLinkedEntitiesResult( |
| 48 | 548 | | ReadDocumentId(documentElement), |
| 48 | 549 | | ReadDocumentStatistics(documentElement), |
| 48 | 550 | | new LinkedEntityCollection(entities, warnings)); |
| | 551 | | } |
| | 552 | |
|
| | 553 | | private static LinkedEntity ReadLinkedEntity(JsonElement entityElement) |
| | 554 | | { |
| 124 | 555 | | string name = default; |
| 124 | 556 | | string id = default; |
| 124 | 557 | | string language = default; |
| 124 | 558 | | string dataSource = default; |
| 124 | 559 | | Uri url = default; |
| | 560 | |
|
| 124 | 561 | | if (entityElement.TryGetProperty("name", out JsonElement nameElement)) |
| 124 | 562 | | name = nameElement.ToString(); |
| 124 | 563 | | if (entityElement.TryGetProperty("id", out JsonElement idElement)) |
| 124 | 564 | | id = idElement.ToString(); |
| 124 | 565 | | if (entityElement.TryGetProperty("language", out JsonElement languageElement)) |
| 124 | 566 | | language = languageElement.ToString(); |
| 124 | 567 | | if (entityElement.TryGetProperty("dataSource", out JsonElement dataSourceValue)) |
| 124 | 568 | | dataSource = dataSourceValue.ToString(); |
| 124 | 569 | | if (entityElement.TryGetProperty("url", out JsonElement urlValue)) |
| 124 | 570 | | url = new Uri(urlValue.ToString()); |
| | 571 | |
|
| 124 | 572 | | IEnumerable<LinkedEntityMatch> matches = ReadLinkedEntityMatches(entityElement); |
| | 573 | |
|
| 124 | 574 | | return new LinkedEntity(name, id, language, dataSource, url, matches); |
| | 575 | | } |
| | 576 | |
|
| | 577 | | private static IEnumerable<LinkedEntityMatch> ReadLinkedEntityMatches(JsonElement entityElement) |
| | 578 | | { |
| 124 | 579 | | if (entityElement.TryGetProperty("matches", out JsonElement matchesElement)) |
| | 580 | | { |
| 124 | 581 | | List<LinkedEntityMatch> matches = new List<LinkedEntityMatch>(); |
| | 582 | |
|
| 496 | 583 | | foreach (JsonElement matchElement in matchesElement.EnumerateArray()) |
| | 584 | | { |
| 124 | 585 | | string text = default; |
| 124 | 586 | | double confidenceScore = default; |
| | 587 | |
|
| 124 | 588 | | if (matchElement.TryGetProperty("text", out JsonElement textValue)) |
| 124 | 589 | | text = textValue.ToString(); |
| | 590 | |
|
| 124 | 591 | | if (matchElement.TryGetProperty("confidenceScore", out JsonElement scoreValue)) |
| 124 | 592 | | scoreValue.TryGetDouble(out confidenceScore); |
| | 593 | |
|
| 124 | 594 | | matches.Add(new LinkedEntityMatch(text, confidenceScore)); |
| | 595 | | } |
| | 596 | |
|
| 124 | 597 | | return matches; |
| | 598 | | } |
| | 599 | |
|
| 0 | 600 | | return default; |
| | 601 | | } |
| | 602 | |
|
| | 603 | | #endregion Entity Linking |
| | 604 | |
|
| | 605 | | } |
| | 606 | | } |