Skip to content

Commit 528fd02

Browse files
authored
Merge pull request #86807 from v-jaswel/patch-1
[Cog Svcs] Update csharp.md
2 parents 38ffb4c + 69d42eb commit 528fd02

File tree

11 files changed

+1150
-1100
lines changed

11 files changed

+1150
-1100
lines changed

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: text-analytics
1111
ms.topic: quickstart
12-
ms.date: 08/05/2019
12+
ms.date: 08/28/2019
1313
ms.author: assafi
1414
---
1515
# Quickstart: Text analytics client library for .NET
@@ -84,19 +84,36 @@ using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
8484
using Microsoft.Rest;
8585
```
8686

87-
In the application's `Main` method, create variables for your resource's Azure endpoint and key. If you created the environment variable after you launched the application, you will need to close and reopen the editor, IDE, or shell running it to access the variable. You will define the methods later.
87+
In the application's `Program` class, create variables for your resource's Azure endpoint and subscription key. In a static constructor, obtain these values from the environment variables `TEXT_ANALYTICS_SUBSCRIPTION_KEY` and `TEXT_ANALYTICS_ENDPOINT`. If you created these environment variables after you began editing the application, you will need to close and reopen the editor, IDE, or shell you are using to access the variables.
88+
89+
```csharp
90+
private const string key_var = "TEXT_ANALYTICS_SUBSCRIPTION_KEY";
91+
private static readonly string subscriptionKey = Environment.GetEnvironmentVariable(key_var);
92+
93+
private const string endpoint_var = "TEXT_ANALYTICS_ENDPOINT";
94+
private static readonly string endpoint = Environment.GetEnvironmentVariable(endpoint_var);
95+
96+
static Program()
97+
{
98+
if (null == subscriptionKey)
99+
{
100+
throw new Exception("Please set/export the environment variable: " + key_var);
101+
}
102+
if (null == endpoint)
103+
{
104+
throw new Exception("Please set/export the environment variable: " + endpoint_var);
105+
}
106+
}
107+
```
108+
109+
In the application's `Main` method, create credentials to access the Text Analytics endpoint. You will define the methods called by the `Main` method later.
88110

89111
[!INCLUDE [text-analytics-find-resource-information](../includes/find-azure-resource-info.md)]
90112

91113
```csharp
92114
static void Main(string[] args)
93115
{
94-
// replace this endpoint with the correct one for your Azure resource.
95-
string endpoint = $"https://westus.api.cognitive.microsoft.com";
96-
//This sample assumes you have created an environment variable for your key
97-
string key = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_SUBSCRIPTION_KEY");
98-
99-
var credentials = new ApiKeyServiceClientCredentials(key);
116+
var credentials = new ApiKeyServiceClientCredentials(subscriptionKey);
100117
TextAnalyticsClient client = new TextAnalyticsClient(credentials)
101118
{
102119
Endpoint = endpoint
@@ -105,9 +122,11 @@ static void Main(string[] args)
105122
Console.OutputEncoding = System.Text.Encoding.UTF8;
106123
SentimentAnalysisExample(client);
107124
// languageDetectionExample(client);
108-
// RecognizeEntitiesExample(client);
125+
// entityRecognitionExample(client);
109126
// KeyPhraseExtractionExample(client);
110-
Console.ReadLine();
127+
128+
Console.Write("Press any key to exit.");
129+
Console.ReadKey();
111130
}
112131
```
113132

@@ -258,14 +277,17 @@ Entities:
258277
Create a new function called `KeyPhraseExtractionExample()` that takes the client that you created earlier and call its [KeyPhrases()](https://docs.microsoft.com/dotnet/api/microsoft.azure.cognitiveservices.language.textanalytics.textanalyticsclientextensions.keyphrases?view=azure-dotnet#Microsoft_Azure_CognitiveServices_Language_TextAnalytics_TextAnalyticsClientExtensions_KeyPhrases_Microsoft_Azure_CognitiveServices_Language_TextAnalytics_ITextAnalyticsClient_System_String_System_String_System_Nullable_System_Boolean__System_Threading_CancellationToken_) function. The result will contain the list of detected key phrases in `KeyPhrases` if successful, and an `errorMessage` if not. Print any detected key phrases.
259278

260279
```csharp
261-
var result = client.KeyPhrases("My cat might need to see a veterinarian.");
280+
static void KeyPhraseExtractionExample(TextAnalyticsClient client)
281+
{
282+
var result = client.KeyPhrases("My cat might need to see a veterinarian.");
262283

263-
// Printing key phrases
264-
Console.WriteLine("Key phrases:");
284+
// Printing key phrases
285+
Console.WriteLine("Key phrases:");
265286

266-
foreach (string keyphrase in result.KeyPhrases)
267-
{
268-
Console.WriteLine($"\t{keyphrase}");
287+
foreach (string keyphrase in result.KeyPhrases)
288+
{
289+
Console.WriteLine($"\t{keyphrase}");
290+
}
269291
}
270292
```
271293

articles/cognitive-services/text-analytics/quickstarts/go-sdk.md

Lines changed: 96 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ manager: assafi
99
ms.service: cognitive-services
1010
ms.subservice: text-analytics
1111
ms.topic: quickstart
12-
ms.date: 07/30/2019
12+
ms.date: 08/28/2019
1313
ms.author: aahi
1414
---
1515

@@ -92,6 +92,8 @@ import (
9292
"fmt"
9393
"github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.1/textanalytics"
9494
"github.com/Azure/go-autorest/autorest"
95+
"log"
96+
"os"
9597
)
9698
```
9799

@@ -109,15 +111,21 @@ func BoolPointer(v bool) *bool {
109111
}
110112
```
111113

112-
In the main function of your project, create variables for your resource's Azure endpoint and key. If you created the environment variable after you launched the application, you will need to close and reopen the editor, IDE, or shell running it to access the variable.
114+
In your application's `main` function, create variables for your resource's Azure endpoint and subscription key. Obtain these values from the environment variables TEXT_ANALYTICS_SUBSCRIPTION_KEY and TEXT_ANALYTICS_ENDPOINT. If you created these environment variables after you began editing the application, you will need to close and reopen the editor, IDE, or shell you are using to access the variables.
113115

114116
[!INCLUDE [text-analytics-find-resource-information](../includes/find-azure-resource-info.md)]
115117

116118
```golang
117-
// This sample assumes you have created an environment variable for your key
118-
subscriptionKey := os.Getenv("TEXT_ANALYTICS_SUBSCRIPTION_KEY")
119-
// replace this endpoint with the correct one for your Azure resource.
120-
endpoint := "https://eastus.api.cognitive.microsoft.com"
119+
var subscriptionKeyVar string = "TEXT_ANALYTICS_SUBSCRIPTION_KEY"
120+
if "" == os.Getenv(subscriptionKeyVar) {
121+
log.Fatal("Please set/export the environment variable " + subscriptionKeyVar + ".")
122+
}
123+
var subscriptionKey string = os.Getenv(subscriptionKeyVar)
124+
var endpointVar string = "TEXT_ANALYTICS_ENDPOINT"
125+
if "" == os.Getenv(endpointVar) {
126+
log.Fatal("Please set/export the environment variable " + endpointVar + ".")
127+
}
128+
var endpoint string = os.Getenv(endpointVar)
121129
```
122130

123131
## Object model
@@ -170,22 +178,25 @@ func SentimentAnalysis(textAnalyticsclient textanalytics.BaseClient) {
170178
In the same function, call the client's [Sentiment()](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.1/textanalytics#BaseClient.Sentiment) function and get the result. Then iterate through the results, and print each document's ID, and sentiment score. A score closer to 0 indicates a negative sentiment, while a score closer to 1 indicates a positive sentiment.
171179

172180
```golang
173-
result, _ := textAnalyticsclient.Sentiment(ctx, BoolPointer(false), &batchInput)
181+
result, err := textAnalyticsclient.Sentiment(ctx, BoolPointer(false), &batchInput)
182+
if err != nil { log.Fatal(err) }
183+
174184
batchResult := textanalytics.SentimentBatchResult{}
175185
jsonString, _ := json.Marshal(result.Value)
176186
json.Unmarshal(jsonString, &batchResult)
177187

178188
// Printing sentiment results
179189
for _,document := range *batchResult.Documents {
180-
fmt.Printf("Document ID: %s " , *document.ID)
190+
fmt.Printf("Document ID: %s\n", *document.ID)
181191
fmt.Printf("Sentiment Score: %f\n",*document.Score)
182192
}
183193

184194
// Printing document errors
185-
fmt.Println("Document Errors")
195+
fmt.Println("Document Errors:")
186196
for _,error := range *batchResult.Errors {
187197
fmt.Printf("Document ID: %s Message : %s\n" ,*error.ID, *error.Message)
188198
}
199+
fmt.Println()
189200
```
190201

191202
In the main function of your project, call `SentimentAnalysis()`.
@@ -203,22 +214,23 @@ Create a new function called `LanguageDetection()` that takes the client created
203214
```golang
204215
func LanguageDetection(textAnalyticsclient textanalytics.BaseClient) {
205216

206-
ctx := context.Background()
207-
inputDocuments := []textanalytics.LanguageInput {
208-
textanalytics.LanguageInput {
209-
ID:StringPointer("0"),
210-
Text:StringPointer("This is a document written in English."),
211-
},
212-
}
217+
ctx := context.Background()
218+
inputDocuments := []textanalytics.LanguageInput {
219+
textanalytics.LanguageInput {
220+
ID:StringPointer("0"),
221+
Text:StringPointer("This is a document written in English."),
222+
},
223+
}
213224

214-
batchInput := textanalytics.LanguageBatchInput{Documents:&inputDocuments}
225+
batchInput := textanalytics.LanguageBatchInput{Documents:&inputDocuments}
215226
}
216227
```
217228

218229
In the same function, call the client's [DetectLanguage()](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.1/textanalytics#BaseClient.DetectLanguage) and get the result. Then iterate through the results, and print each document's ID, and detected language.
219230

220231
```golang
221-
result, _ := textAnalyticsclient.DetectLanguage(ctx, BoolPointer(false), &batchInput)
232+
result, err := textAnalyticsclient.DetectLanguage(ctx, BoolPointer(false), &batchInput)
233+
if err != nil { log.Fatal(err) }
222234

223235
// Printing language detection results
224236
for _,document := range *result.Documents {
@@ -231,10 +243,11 @@ for _,document := range *result.Documents {
231243
}
232244

233245
// Printing document errors
234-
fmt.Println("Document Errors")
246+
fmt.Println("Document Errors:")
235247
for _,error := range *result.Errors {
236248
fmt.Printf("Document ID: %s Message : %s\n" ,*error.ID, *error.Message)
237249
}
250+
fmt.Println()
238251
```
239252

240253
In the main function of your project, call `LanguageDetection()`.
@@ -250,48 +263,50 @@ Document ID: 0 Detected Languages with Score: English 1.000000
250263
Create a new function called `ExtractEntities()` that takes the client created earlier. Create a list of [MultiLanguageInput](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.1/textanalytics#MultiLanguageBatchInput) objects, containing the documents you want to analyze. Each object will contain an `id`, `language`, and a `text` attribute. The `text` attribute stores the text to be analyzed, `language` is the language of the document, and the `id` can be any value.
251264

252265
```golang
253-
func ExtractKeyPhrases(textAnalyticsclient textanalytics.BaseClient) {
266+
func ExtractEntities(textAnalyticsclient textanalytics.BaseClient) {
254267

255-
ctx := context.Background()
256-
inputDocuments := []textanalytics.MultiLanguageInput {
257-
textanalytics.MultiLanguageInput {
258-
Language: StringPointer("en"),
259-
ID:StringPointer("0"),
260-
Text:StringPointer("Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800."),
261-
}
262-
}
268+
ctx := context.Background()
269+
inputDocuments := []textanalytics.MultiLanguageInput {
270+
textanalytics.MultiLanguageInput {
271+
Language: StringPointer("en"),
272+
ID:StringPointer("0"),
273+
Text:StringPointer("Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800."),
274+
},
275+
}
263276

264-
batchInput := textanalytics.MultiLanguageBatchInput{Documents:&inputDocuments}
277+
batchInput := textanalytics.MultiLanguageBatchInput{Documents:&inputDocuments}
265278
}
266279
```
267280

268281
In the same function, call the client's [Entities()](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.1/textanalytics#BaseClient.Entities) and get the result. Then iterate through the results, and print each document's ID, and extracted entities score.
269282

270283
```golang
271-
result, _ := textAnalyticsclient.Entities(ctx, BoolPointer(false), &batchInput)
272-
273-
// Printing extracted entities results
274-
for _,document := range *result.Documents {
275-
fmt.Printf("Document ID: %s\n" , *document.ID)
276-
fmt.Printf("\tExtracted Entities:\n")
277-
for _,entity := range *document.Entities{
278-
fmt.Printf("\t\tName: %s\tType: %s",*entity.Name, *entity.Type)
279-
if entity.SubType != nil{
280-
fmt.Printf("\tSub-Type: %s\n", *entity.SubType)
281-
}
282-
fmt.Println()
283-
for _,match := range *entity.Matches{
284-
fmt.Printf("\t\t\tOffset: %v\tLength: %v\tScore: %f\n", *match.Offset, *match.Length, *match.EntityTypeScore)
285-
}
286-
}
287-
fmt.Println()
288-
}
289-
290-
// Printing document errors
291-
fmt.Println("Document Errors")
292-
for _,error := range *result.Errors {
293-
fmt.Printf("Document ID: %s Message : %s\n" ,*error.ID, *error.Message)
294-
}
284+
result, err := textAnalyticsclient.Entities(ctx, BoolPointer(false), &batchInput)
285+
if err != nil { log.Fatal(err) }
286+
287+
// Printing extracted entities results
288+
for _,document := range *result.Documents {
289+
fmt.Printf("Document ID: %s\n" , *document.ID)
290+
fmt.Printf("\tExtracted Entities:\n")
291+
for _,entity := range *document.Entities{
292+
fmt.Printf("\t\tName: %s\tType: %s",*entity.Name, *entity.Type)
293+
if entity.SubType != nil{
294+
fmt.Printf("\tSub-Type: %s\n", *entity.SubType)
295+
}
296+
fmt.Println()
297+
for _,match := range *entity.Matches{
298+
fmt.Printf("\t\t\tOffset: %v\tLength: %v\tScore: %f\n", *match.Offset, *match.Length, *match.EntityTypeScore)
299+
}
300+
}
301+
fmt.Println()
302+
}
303+
304+
// Printing document errors
305+
fmt.Println("Document Errors:")
306+
for _,error := range *result.Errors {
307+
fmt.Printf("Document ID: %s Message : %s\n" ,*error.ID, *error.Message)
308+
}
309+
fmt.Println()
295310
```
296311

297312
In the main function of your project, call `ExtractEntities()`.
@@ -325,39 +340,41 @@ Create a new function called `ExtractKeyPhrases()` that takes the client created
325340
```golang
326341
func ExtractKeyPhrases(textAnalyticsclient textanalytics.BaseClient) {
327342

328-
ctx := context.Background()
329-
inputDocuments := []textanalytics.MultiLanguageInput {
330-
textanalytics.MultiLanguageInput {
331-
Language: StringPointer("en"),
332-
ID:StringPointer("0"),
333-
Text:StringPointer("My cat might need to see a veterinarian."),
334-
},
335-
}
343+
ctx := context.Background()
344+
inputDocuments := []textanalytics.MultiLanguageInput {
345+
textanalytics.MultiLanguageInput {
346+
Language: StringPointer("en"),
347+
ID:StringPointer("0"),
348+
Text:StringPointer("My cat might need to see a veterinarian."),
349+
},
350+
}
336351

337-
batchInput := textanalytics.MultiLanguageBatchInput{Documents:&inputDocuments}
352+
batchInput := textanalytics.MultiLanguageBatchInput{Documents:&inputDocuments}
338353
}
339354
```
340355

341356
In the same function, call the client's [KeyPhrases()](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.1/textanalytics#BaseClient.KeyPhrases) and get the result. Then iterate through the results, and print each document's ID, and extracted key phrases.
342357

343358
```golang
344-
result, _ := textAnalyticsclient.KeyPhrases(ctx, BoolPointer(false), &batchInput)
345-
346-
// Printing extracted key phrases results
347-
for _,document := range *result.Documents {
348-
fmt.Printf("Document ID: %s\n" , *document.ID)
349-
fmt.Printf("\tExtracted Key Phrases:\n")
350-
for _,keyPhrase := range *document.KeyPhrases{
351-
fmt.Printf("\t\t%s\n",keyPhrase)
352-
}
353-
fmt.Println()
354-
}
355-
356-
// Printing document errors
357-
fmt.Println("Document Errors")
358-
for _,error := range *result.Errors {
359-
fmt.Printf("Document ID: %s Message : %s\n" ,*error.ID, *error.Message)
360-
}
359+
result, err := textAnalyticsclient.KeyPhrases(ctx, BoolPointer(false), &batchInput)
360+
if err != nil { log.Fatal(err) }
361+
362+
// Printing extracted key phrases results
363+
for _,document := range *result.Documents {
364+
fmt.Printf("Document ID: %s\n" , *document.ID)
365+
fmt.Printf("\tExtracted Key Phrases:\n")
366+
for _,keyPhrase := range *document.KeyPhrases{
367+
fmt.Printf("\t\t%s\n",keyPhrase)
368+
}
369+
fmt.Println()
370+
}
371+
372+
// Printing document errors
373+
fmt.Println("Document Errors:")
374+
for _,error := range *result.Errors {
375+
fmt.Printf("Document ID: %s Message : %s\n" ,*error.ID, *error.Message)
376+
}
377+
fmt.Println()
361378
```
362379

363380
In the main function of your project, call `ExtractKeyPhrases()`.

0 commit comments

Comments
 (0)