Skip to content

Commit b9abb98

Browse files
authored
Merge pull request #148 from v-jaswel/patch-9
[Text Analytics] Update to get endpoint and subscription key from env vars
2 parents b5958bb + 7056d24 commit b9abb98

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed

samples/TextAnalytics/synchronous/Program.cs

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <imports>
1+
// <imports>
22
using System;
33
using System.Collections.Generic;
44
using System.Net.Http;
@@ -13,51 +13,90 @@ namespace text_analytics_quickstart
1313
{
1414
class Program
1515
{
16+
private const string key_var = "TEXT_ANALYTICS_SUBSCRIPTION_KEY";
17+
private static readonly string subscriptionKey = Environment.GetEnvironmentVariable(key_var);
18+
19+
private const string endpoint_var = "TEXT_ANALYTICS_ENDPOINT";
20+
private static readonly string endpoint = Environment.GetEnvironmentVariable(endpoint_var);
21+
22+
static Program()
23+
{
24+
if (null == subscriptionKey)
25+
{
26+
throw new Exception("Please set/export the environment variable: " + key_var);
27+
}
28+
if (null == endpoint)
29+
{
30+
throw new Exception("Please set/export the environment variable: " + endpoint_var);
31+
}
32+
}
33+
1634
// <main>
1735
static void Main(string[] args)
1836
{
1937
Console.OutputEncoding = System.Text.Encoding.UTF8;
20-
string location = "westus2";
21-
string endpoint = $"https://{location}.api.cognitive.microsoft.com";
22-
//This sample assumes you have created an environment variable for your key
23-
string key = Environment.GetEnvironmentVariable("TEXTANALYTICS_SUBSCRIPTION_KEY");
24-
ITextAnalyticsClient client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials(key));
25-
38+
var credentials = new ApiKeyServiceClientCredentials(subscriptionKey);
39+
TextAnalyticsClient client = new TextAnalyticsClient(credentials)
40+
{
41+
Endpoint = endpoint
42+
};
43+
2644
SentimentAnalysisExample(client);
27-
// languageDetectionExample(client);
28-
// RecognizeEntitiesExample(client);
29-
// KeyPhraseExtractionExample(client);
30-
Console.ReadLine();
45+
languageDetectionExample(client);
46+
entityRecognitionExample(client);
47+
KeyPhraseExtractionExample(client);
48+
Console.Write("Press any key to exit.");
49+
Console.ReadKey();
3150
}
3251
// </main>
3352

3453
// <sentiment>
35-
static void SentimentAnalysisExample(ITextAnalyticsClient client){
54+
static void SentimentAnalysisExample(ITextAnalyticsClient client)
55+
{
3656
var result = client.Sentiment("I had the best day of my life.", "en");
3757
Console.WriteLine($"Sentiment Score: {result.Score:0.00}");
3858
}
3959
// <sentiment>
4060

4161
// <language-detection>
42-
static void languageDetectionExample(ITextAnalyticsClient client){
62+
static void languageDetectionExample(ITextAnalyticsClient client)
63+
{
4364
var result = client.DetectLanguage("This is a document written in English.");
4465
Console.WriteLine($"Language: {result.DetectedLanguages[0].Name}");
4566
}
4667
// </language-detection>
4768

4869
// <entity-recognition>
49-
static void entityRecognitionExample(ITextAnalyticsClient client){
50-
70+
static void entityRecognitionExample(ITextAnalyticsClient client)
71+
{
72+
5173
var result = client.Entities("Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800.");
5274
Console.WriteLine("Entities:");
53-
foreach (var entity in result.Entities){
75+
foreach (var entity in result.Entities)
76+
{
5477
Console.WriteLine($"\tName: {entity.Name},\tType: {entity.Type ?? "N/A"},\tSub-Type: {entity.SubType ?? "N/A"}");
55-
foreach (var match in entity.Matches){
78+
foreach (var match in entity.Matches)
79+
{
5680
Console.WriteLine($"\t\tOffset: {match.Offset},\tLength: {match.Length},\tScore: {match.EntityTypeScore:F3}");
5781
}
5882
}
5983
}
6084
// </entity-recognition>
85+
86+
// <key-phrase-extraction>
87+
static void KeyPhraseExtractionExample(TextAnalyticsClient client)
88+
{
89+
var result = client.KeyPhrases("My cat might need to see a veterinarian.");
90+
91+
// Printing key phrases
92+
Console.WriteLine("Key phrases:");
93+
94+
foreach (string keyphrase in result.KeyPhrases)
95+
{
96+
Console.WriteLine($"\t{keyphrase}");
97+
}
98+
}
99+
// </key-phrase-extraction>
61100
}
62101

63102
// <client-class>

0 commit comments

Comments
 (0)