-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathSample4_RecognizeEntitiesBatchConvenience.cs
88 lines (75 loc) · 4.18 KB
/
Sample4_RecognizeEntitiesBatchConvenience.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace Azure.AI.TextAnalytics.Samples
{
public partial class TextAnalyticsSamples
{
[Test]
public void RecognizeEntitiesBatchConvenience()
{
Uri endpoint = new(TestEnvironment.Endpoint);
AzureKeyCredential credential = new(TestEnvironment.ApiKey);
TextAnalyticsClient client = new(endpoint, credential, CreateSampleOptions());
#region Snippet:Sample4_RecognizeEntitiesBatchConvenience
string documentA =
"We love this trail and make the trip every year. The views are breathtaking and well worth the hike!"
+ " Yesterday was foggy though, so we missed the spectacular views. We tried again today and it was"
+ " amazing. Everyone in my family liked the trail although it was too challenging for the less"
+ " athletic among us. Not necessarily recommended for small children. A hotel close to the trail"
+ " offers services for childcare in case you want that.";
string documentB =
"Nos hospedamos en el Hotel Foo la semana pasada por nuestro aniversario. La gerencia sabía de nuestra"
+ " celebración y me ayudaron a tenerle una sorpresa a mi pareja. La habitación estaba limpia y"
+ " decorada como yo había pedido. Una gran experiencia. El próximo año volveremos.";
string documentC =
"That was the best day of my life! We went on a 4 day trip where we stayed at Hotel Foo. They had"
+ " great amenities that included an indoor pool, a spa, and a bar. The spa offered couples massages"
+ " which were really good. The spa was clean and felt very peaceful. Overall the whole experience was"
+ " great. We will definitely come back.";
string documentD = string.Empty;
// Prepare the input of the text analysis operation. You can add multiple documents to this list and
// perform the same operation on all of them simultaneously.
List<string> batchedDocuments = new()
{
documentA,
documentB,
documentC,
documentD
};
Response<RecognizeEntitiesResultCollection> response = client.RecognizeEntitiesBatch(batchedDocuments);
RecognizeEntitiesResultCollection entititesPerDocuments = response.Value;
int i = 0;
Console.WriteLine($"Recognize Entities, model version: \"{entititesPerDocuments.ModelVersion}\"");
Console.WriteLine();
foreach (RecognizeEntitiesResult documentResult in entititesPerDocuments)
{
Console.WriteLine($"Result for document with Text = \"{batchedDocuments[i++]}\"");
if (documentResult.HasError)
{
Console.WriteLine($" Error!");
Console.WriteLine($" Document error code: {documentResult.Error.ErrorCode}");
Console.WriteLine($" Message: {documentResult.Error.Message}");
Console.WriteLine();
continue;
}
Console.WriteLine($" Recognized {documentResult.Entities.Count} entities:");
foreach (CategorizedEntity entity in documentResult.Entities)
{
Console.WriteLine($" Text: {entity.Text}");
Console.WriteLine($" Offset: {entity.Offset}");
Console.WriteLine($" Length: {entity.Length}");
Console.WriteLine($" Category: {entity.Category}");
if (!string.IsNullOrEmpty(entity.SubCategory))
Console.WriteLine($" SubCategory: {entity.SubCategory}");
Console.WriteLine($" Confidence score: {entity.ConfidenceScore}");
Console.WriteLine();
}
Console.WriteLine();
}
#endregion
}
}
}