Skip to content

Commit ec4cf55

Browse files
authored
Merge pull request #103909 from assafi/assafi/cs_sdk_preview_2
[Cog Svcs] Update Text Analytics C# Quickstart to SDK v1.0.0-preview.2
2 parents f5cfb62 + a8d421e commit ec4cf55

File tree

1 file changed

+46
-41
lines changed
  • articles/cognitive-services/text-analytics/includes/quickstarts

1 file changed

+46
-41
lines changed

articles/cognitive-services/text-analytics/includes/quickstarts/csharp-sdk.md

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Using the Visual Studio IDE, create a new .NET Core console app. This will creat
3838

3939
#### [Version 3.0-preview](#tab/version-3)
4040

41-
Install the client library by right-clicking on the solution in the **Solution Explorer** and selecting **Manage NuGet Packages**. In the package manager that opens select **Browse**, check **Include prerelease**, and search for `Azure.AI.TextAnalytics`. Click on it, and then **Install**. You can also use the [Package Manager Console](https://docs.microsoft.com/nuget/consume-packages/install-use-packages-powershell#find-and-install-a-package).
41+
Install the client library by right-clicking on the solution in the **Solution Explorer** and selecting **Manage NuGet Packages**. In the package manager that opens select **Browse**, check **Include prerelease**, and search for `Azure.AI.TextAnalytics`. Select version `1.0.0-preview.2`, and then **Install**. You can also use the [Package Manager Console](https://docs.microsoft.com/nuget/consume-packages/install-use-packages-powershell#find-and-install-a-package).
4242

4343
Open the *program.cs* file and add the following `using` directives:
4444

@@ -52,7 +52,7 @@ In the application's `Program` class, create variables for your resource's key a
5252
[!INCLUDE [text-analytics-find-resource-information](../find-azure-resource-info.md)]
5353

5454
```csharp
55-
private static readonly string key = "<replace-with-your-text-analytics-key-here>";
55+
private static readonly TextAnalyticsApiKeyCredential credentials = new TextAnalyticsApiKeyCredential("<replace-with-your-text-analytics-key-here>");
5656
private static readonly Uri endpoint = new Uri("<replace-with-your-text-analytics-endpoint-here>");
5757
```
5858

@@ -61,7 +61,7 @@ Replace the application's `Main` method. You will define the methods called here
6161
```csharp
6262
static void Main(string[] args)
6363
{
64-
var client = new TextAnalyticsClient(endpoint, key);
64+
var client = new TextAnalyticsClient(endpoint, credentials);
6565
// You will implement these methods later in the quickstart.
6666
SentimentAnalysisExample(client);
6767
LanguageDetectionExample(client);
@@ -109,17 +109,18 @@ If you're using version `3.0-preview`, you can use an optional `TextAnalyticsCli
109109
* [Sentiment analysis](#sentiment-analysis)
110110
* [Language detection](#language-detection)
111111
* [Named Entity Recognition](#named-entity-recognition-ner)
112+
* [Detect personal information](#detect-personal-information)
112113
* [Entity linking](#entity-linking)
113114
* [Key phrase extraction](#key-phrase-extraction)
114115

115116
## Authenticate the client
116117

117118
#### [Version 3.0-preview](#tab/version-3)
118119

119-
Make sure your main method from earlier creates a new client object with your endpoint and key.
120+
Make sure your main method from earlier creates a new client object with your endpoint and credentials.
120121

121122
```csharp
122-
var client = new TextAnalyticsClient(endpoint, key);
123+
var client = new TextAnalyticsClient(endpoint, credentials);
123124
```
124125

125126
#### [Version 2.1](#tab/version-2)
@@ -138,20 +139,24 @@ Create a method to instantiate the [TextAnalyticsClient](https://docs.microsoft.
138139

139140
#### [Version 3.0-preview](#tab/version-3)
140141

141-
Create a new function called `SentimentAnalysisExample()` that takes the client that you created earlier, and call its `AnalyzeSentiment()` function. The returned `Response<AnalyzeSentimentResult>` object will contain the sentiment label and score of the entire input document, as well as a sentiment analysis for each sentence if successful, and a `Value.ErrorMessage` if not.
142+
Create a new function called `SentimentAnalysisExample()` that takes the client that you created earlier, and call its `AnalyzeSentiment()` function. The returned `Response<DocumentSentiment>` object will contain the sentiment label and score of the entire input document, as well as a sentiment analysis for each sentence if successful. If there was an error, it will throw a `RequestFailedException`.
142143

143144
```csharp
144145
static void SentimentAnalysisExample(TextAnalyticsClient client)
145146
{
146-
var response = client.AnalyzeSentiment("I had the best day of my life. I wish you were there with me.");
147-
Console.WriteLine($"Document sentiment: {response.Value.DocumentSentiment.SentimentClass}\n");
148-
foreach (var sentence in response.Value.SentenceSentiments)
147+
string inputText = "I had the best day of my life. I wish you were there with me.";
148+
DocumentSentiment documentSentiment = client.AnalyzeSentiment(inputText);
149+
Console.WriteLine($"Document sentiment: {documentSentiment.Sentiment}\n");
150+
151+
var si = new StringInfo(inputText);
152+
foreach (var sentence in documentSentiment.Sentences)
149153
{
150154
Console.WriteLine($"\tSentence [offset {sentence.Offset}, length {sentence.Length}]");
151-
Console.WriteLine($"\tSentence sentiment: {sentence.SentimentClass}");
152-
Console.WriteLine($"\tPositive score: {sentence.PositiveScore:0.00}");
153-
Console.WriteLine($"\tNegative score: {sentence.NegativeScore:0.00}");
154-
Console.WriteLine($"\tNeutral score: {sentence.NeutralScore:0.00}\n");
155+
Console.WriteLine($"\tText: \"{si.SubstringByTextElements(sentence.Offset, sentence.Length)}\"");
156+
Console.WriteLine($"\tSentence sentiment: {sentence.Sentiment}");
157+
Console.WriteLine($"\tPositive score: {sentence.SentimentScores.Positive:0.00}");
158+
Console.WriteLine($"\tNegative score: {sentence.SentimentScores.Negative:0.00}");
159+
Console.WriteLine($"\tNeutral score: {sentence.SentimentScores.Neutral:0.00}\n");
155160
}
156161
}
157162
```
@@ -162,12 +167,14 @@ static void SentimentAnalysisExample(TextAnalyticsClient client)
162167
Document sentiment: Positive
163168

164169
Sentence [offset 0, length 30]
170+
Text: "I had the best day of my life."
165171
Sentence sentiment: Positive
166172
Positive score: 1.00
167173
Negative score: 0.00
168174
Neutral score: 0.00
169175

170176
Sentence [offset 31, length 30]
177+
Text: "I wish you were there with me."
171178
Sentence sentiment: Neutral
172179
Positive score: 0.21
173180
Negative score: 0.02
@@ -193,16 +200,15 @@ Sentiment Score: 0.87
193200
#### [Version 3.0-preview](#tab/version-3)
194201

195202

196-
Create a new function called `LanguageDetectionExample()` that takes the client that you created earlier, and call its `DetectLanguage()` function. The returned `Response<DetectLanguageResult>` object will contain the detected language in `Value.PrimaryLanguage` if successful, and a `Value.ErrorMessage` if not.
203+
Create a new function called `LanguageDetectionExample()` that takes the client that you created earlier, and call its `DetectLanguage()` function. The returned `Response<DetectedLanguage>` object will contain the detected language along with its name and ISO-6391 code. If there was an error, it will throw a `RequestFailedException`.
197204

198205
> [!Tip]
199206
> In some cases it may be hard to disambiguate languages based on the input. You can use the `countryHint` parameter to specify a 2-letter country code. By default the API is using the "US" as the default countryHint, to remove this behavior you can reset this parameter by setting this value to empty string `countryHint = ""`. To set a different default, set the `TextAnalyticsClientOptions.DefaultCountryHint` property and pass it during the client's initialization.
200207
201208
```csharp
202209
static void LanguageDetectionExample(TextAnalyticsClient client)
203210
{
204-
var response = client.DetectLanguage("Ce document est rédigé en Français.");
205-
var detectedLanguage = response.Value.PrimaryLanguage;
211+
DetectedLanguage detectedLanguage = client.DetectLanguage("Ce document est rédigé en Français.");
206212
Console.WriteLine("Language:");
207213
Console.WriteLine($"\t{detectedLanguage.Name},\tISO-6391: {detectedLanguage.Iso6391Name}\n");
208214
}
@@ -217,7 +223,7 @@ Language:
217223

218224
#### [Version 2.1](#tab/version-2)
219225

220-
Create a new function called `languageDetectionExample()` that takes the client that you created earlier, and call its [DetectLanguage()](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.textanalytics.textanalyticsclientextensions.detectlanguage?view=azure-dotnet#Microsoft_Azure_CognitiveServices_Language_TextAnalytics_TextAnalyticsClientExtensions_DetectLanguage_Microsoft_Azure_CognitiveServices_Language_TextAnalytics_ITextAnalyticsClient_System_String_System_String_System_Nullable_System_Boolean__System_Threading_CancellationToken_) function. The returned [LanguageResult](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.textanalytics.models.languageresult?view=azure-dotnet) object will contain the list of detected languages in `DetectedLanguages` if successful, and an `errorMessage` if not. Print the first returned language.
226+
Create a new function called `languageDetectionExample()` that takes the client that you created earlier, and call its [DetectLanguage()](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.textanalytics.textanalyticsclientextensions.detectlanguage?view=azure-dotnet#Microsoft_Azure_CognitiveServices_Language_TextAnalytics_TextAnalyticsClientExtensions_DetectLanguage_Microsoft_Azure_CognitiveServices_Language_TextAnalytics_ITextAnalyticsClient_System_String_System_String_System_Nullable_System_Boolean__System_Threading_CancellationToken_) function. The returned [LanguageResult](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.textanalytics.models.languageresult?view=azure-dotnet) object will contain the list of detected languages in `DetectedLanguages` if successful, and an `errorMessage` if not. Print the first returned language.
221227

222228
> [!Tip]
223229
> In some cases it may be hard to disambiguate languages based on the input. You can use the `countryHint` parameter to specify a 2-letter country code. By default the API is using the "US" as the default countryHint, to remove this behavior you can reset this parameter by setting this value to empty string `countryHint = ""` .
@@ -232,29 +238,27 @@ Language: English
232238

233239
---
234240

235-
## Named Entity recognition (NER)
241+
## Named Entity Recognition (NER)
236242

237243
#### [Version 3.0-preview](#tab/version-3)
238244

239245

240246
> [!NOTE]
241-
> In version `3.0-preview`:
242-
> * NER includes separate methods for detecting personal information.
243-
> * Entity linking is a separate request than NER.
247+
> New in version `3.0-preview`:
248+
> * Entity recognition now includes the ability to detect personal information in text.
249+
> * Entity linking is now a separated from entity recognition.
244250
245-
The below code is for detecting personal information using NER v3, which is in public preview.
246251

247-
248-
Create a new function called `EntityRecognitionExample()` that takes the client that you created earlier, call its `RecognizeEntities()` function and iterate through the results. The returned `Response<RecognizeEntitiesResult>` object will contain the list of detected entities in `Value.NamedEntities` if successful, and a `Value.ErrorMessage` if not. For each detected entity, print its Type and Sub-Type if exists.
252+
Create a new function called `EntityRecognitionExample()` that takes the client that you created earlier, call its `RecognizeEntities()` function and iterate through the results. The returned `Response<IReadOnlyCollection<CategorizedEntity>>` object will contain the list of detected entities. If there was an error, it will throw a `RequestFailedException`.
249253

250254
```csharp
251255
static void EntityRecognitionExample(TextAnalyticsClient client)
252256
{
253257
var response = client.RecognizeEntities("I had a wonderful trip to Seattle last week.");
254258
Console.WriteLine("Named Entities:");
255-
foreach(var entity in response.Value.NamedEntities)
259+
foreach(var entity in response.Value)
256260
{
257-
Console.WriteLine($"\tText: {entity.Text},\tType: {entity.Type},\tSub-Type: {entity.SubType ?? "N/A"}");
261+
Console.WriteLine($"\tText: {entity.Text},\tCategory: {entity.Category},\tSub-Category: {entity.SubCategory}");
258262
Console.WriteLine($"\t\tOffset: {entity.Offset},\tLength: {entity.Length},\tScore: {entity.Score:F3}\n");
259263
}
260264
}
@@ -264,25 +268,26 @@ static void EntityRecognitionExample(TextAnalyticsClient client)
264268

265269
```console
266270
Named Entities:
267-
Text: Seattle, Type: Location, Sub-Type: N/A
268-
Offset: 26, Length: 7, Score: 0.806
271+
Text: Seattle, Category: Location, Sub-Category: GPE
272+
Offset: 26, Length: 7, Score: 0.920
269273

270-
Text: last week, Type: DateTime, Sub-Type: N/A
274+
Text: last week, Category: DateTime, Sub-Category: DateRange
271275
Offset: 34, Length: 9, Score: 0.800
272276
```
273277

274-
## Using NER to detect personal information
278+
## Detect personal information
275279

276-
Create a new function called `EntityPIIExample()` that takes the client that you created earlier, call its `RecognizePiiEntities()` function and iterate through the results. Similar to the previous function the returned `Response<RecognizeEntitiesResult>` object will contain the list of detected entities in `Value.NamedEntities` if successful, and a `Value.ErrorMessage` if not.
280+
Create a new function called `EntityPIIExample()` that takes the client that you created earlier, call its `RecognizePiiEntities()` function and iterate through the results. Similar to the previous function the returned `Response<IReadOnlyCollection<CategorizedEntity>>` object will contain the list of detected entities. If there was an error, it will throw a `RequestFailedException`.
277281

278282
```csharp
279283
static void EntityPIIExample(TextAnalyticsClient client)
280284
{
281-
var response = client.RecognizePiiEntities("Insurance policy for SSN on file 123-12-1234 is here by approved.");
285+
string inputText = "Insurance policy for SSN on file 123-12-1234 is here by approved.";
286+
var response = client.RecognizePiiEntities(inputText);
282287
Console.WriteLine("Personally Identifiable Information Entities:");
283-
foreach(var entity in response.Value.NamedEntities)
288+
foreach(var entity in response.Value)
284289
{
285-
Console.WriteLine($"\tText: {entity.Text},\tType: {entity.Type},\tSub-Type: {entity.SubType ?? "N/A"}");
290+
Console.WriteLine($"\tText: {entity.Text},\tCategory: {entity.Category},\tSub-Category: {entity.SubCategory}");
286291
Console.WriteLine($"\t\tOffset: {entity.Offset},\tLength: {entity.Length},\tScore: {entity.Score:F3}\n");
287292
}
288293
}
@@ -292,14 +297,14 @@ static void EntityPIIExample(TextAnalyticsClient client)
292297

293298
```console
294299
Personally Identifiable Information Entities:
295-
Text: 123-12-1234, Type: U.S. Social Security Number (SSN), Sub-Type: N/A
300+
Text: 123-12-1234, Category: U.S. Social Security Number (SSN), Sub-Category: None
296301
Offset: 33, Length: 11, Score: 0.850
297302
```
298303

299304

300305
## Entity linking
301306

302-
Create a new function called `EntityLinkingExample()` that takes the client that you created earlier, call its `RecognizeLinkedEntities()` function and iterate through the results. The returned `Response<RecognizeLinkedEntitiesResult>` object will contain the list of detected entities in `Value.LinkedEntities` if successful, and a `Value.ErrorMessage` if not. Since linked entities are uniquely identified, occurrences of the same entity are grouped under a `LinkedEntity` object as a list of `LinkedEntityMatch` objects.
307+
Create a new function called `EntityLinkingExample()` that takes the client that you created earlier, call its `RecognizeLinkedEntities()` function and iterate through the results. The returned `Response<IReadOnlyCollection<LinkedEntity>>` represents the list of detected entities. If there was an error, it will throw a `RequestFailedException`. Since linked entities are uniquely identified, occurrences of the same entity are grouped under a `LinkedEntity` object as a list of `LinkedEntityMatch` objects.
303308

304309
```csharp
305310
static void EntityLinkingExample(TextAnalyticsClient client)
@@ -311,9 +316,9 @@ static void EntityLinkingExample(TextAnalyticsClient client)
311316
"chief executive officer, president and chief software architect, " +
312317
"while also being the largest individual shareholder until May 2014.");
313318
Console.WriteLine("Linked Entities:");
314-
foreach (var entity in response.Value.LinkedEntities)
319+
foreach (var entity in response.Value)
315320
{
316-
Console.WriteLine($"\tName: {entity.Name},\tID: {entity.Id},\tURL: {entity.Uri}\tData Source: {entity.DataSource}");
321+
Console.WriteLine($"\tName: {entity.Name},\tID: {entity.Id},\tURL: {entity.Url}\tData Source: {entity.DataSource}");
317322
Console.WriteLine("\tMatches:");
318323
foreach (var match in entity.Matches)
319324
{
@@ -381,7 +386,7 @@ Create a new function called `RecognizeEntitiesExample()` that takes the client
381386

382387
#### [Version 3.0-preview](#tab/version-3)
383388

384-
Create a new function called `KeyPhraseExtractionExample()` that takes the client that you created earlier, and call its `ExtractKeyPhrases()` function. The result will contain the list of detected key phrases in `Value.KeyPhrases` if successful, and a `Value.ErrorMessage` if not. Print any detected key phrases.
389+
Create a new function called `KeyPhraseExtractionExample()` that takes the client that you created earlier, and call its `ExtractKeyPhrases()` function. The returned `<Response<IReadOnlyCollection<string>>` object will contain the list of detected key phrases. If there was an error, it will throw a `RequestFailedException`.
385390

386391
```csharp
387392
static void KeyPhraseExtractionExample(TextAnalyticsClient client)
@@ -391,7 +396,7 @@ static void KeyPhraseExtractionExample(TextAnalyticsClient client)
391396
// Printing key phrases
392397
Console.WriteLine("Key phrases:");
393398

394-
foreach (string keyphrase in response.Value.KeyPhrases)
399+
foreach (string keyphrase in response.Value)
395400
{
396401
Console.WriteLine($"\t{keyphrase}");
397402
}
@@ -421,4 +426,4 @@ Key phrases:
421426
veterinarian
422427
```
423428

424-
---
429+
---

0 commit comments

Comments
 (0)