1
- // <imports>
1
+ // <imports>
2
2
using System ;
3
3
using System . Collections . Generic ;
4
4
using System . Net . Http ;
@@ -13,51 +13,90 @@ namespace text_analytics_quickstart
13
13
{
14
14
class Program
15
15
{
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
+
16
34
// <main>
17
35
static void Main ( string [ ] args )
18
36
{
19
37
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
+
26
44
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 ( ) ;
31
50
}
32
51
// </main>
33
52
34
53
// <sentiment>
35
- static void SentimentAnalysisExample ( ITextAnalyticsClient client ) {
54
+ static void SentimentAnalysisExample ( ITextAnalyticsClient client )
55
+ {
36
56
var result = client . Sentiment ( "I had the best day of my life." , "en" ) ;
37
57
Console . WriteLine ( $ "Sentiment Score: { result . Score : 0.00} ") ;
38
58
}
39
59
// <sentiment>
40
60
41
61
// <language-detection>
42
- static void languageDetectionExample ( ITextAnalyticsClient client ) {
62
+ static void languageDetectionExample ( ITextAnalyticsClient client )
63
+ {
43
64
var result = client . DetectLanguage ( "This is a document written in English." ) ;
44
65
Console . WriteLine ( $ "Language: { result . DetectedLanguages [ 0 ] . Name } ") ;
45
66
}
46
67
// </language-detection>
47
68
48
69
// <entity-recognition>
49
- static void entityRecognitionExample ( ITextAnalyticsClient client ) {
50
-
70
+ static void entityRecognitionExample ( ITextAnalyticsClient client )
71
+ {
72
+
51
73
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." ) ;
52
74
Console . WriteLine ( "Entities:" ) ;
53
- foreach ( var entity in result . Entities ) {
75
+ foreach ( var entity in result . Entities )
76
+ {
54
77
Console . WriteLine ( $ "\t Name: { entity . Name } ,\t Type: { entity . Type ?? "N/A" } ,\t Sub-Type: { entity . SubType ?? "N/A" } ") ;
55
- foreach ( var match in entity . Matches ) {
78
+ foreach ( var match in entity . Matches )
79
+ {
56
80
Console . WriteLine ( $ "\t \t Offset: { match . Offset } ,\t Length: { match . Length } ,\t Score: { match . EntityTypeScore : F3} ") ;
57
81
}
58
82
}
59
83
}
60
84
// </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>
61
100
}
62
101
63
102
// <client-class>
0 commit comments