@@ -18,26 +18,26 @@ Create a new C# console application project in Visual Studio 2019 and [install t
18
18
Let's open ` Program.cs ` and add some code that works as a skeleton for our project.
19
19
20
20
``` C#
21
- using System ;
22
- using System .Threading .Tasks ;
23
- using Microsoft .CognitiveServices .Speech ;
24
- using Microsoft .CognitiveServices .Speech .Intent ;
25
-
26
- namespace helloworld
21
+ using System ;
22
+ using System .Threading .Tasks ;
23
+ using Microsoft .CognitiveServices .Speech ;
24
+ using Microsoft .CognitiveServices .Speech .Intent ;
25
+
26
+ namespace helloworld
27
+ {
28
+ class Program
27
29
{
28
- class Program
30
+ static void Main ( string [] args )
29
31
{
30
- static void Main (string [] args )
31
- {
32
- IntentPatternMatchingWithMicrophoneAsync ().Wait ();
33
- }
34
-
35
- private static async Task IntentPatternMatchingWithMicrophoneAsync ()
36
- {
37
- var config = SpeechConfig .FromSubscription (" YOUR_SUBSCRIPTION_KEY" , " YOUR_SUBSCRIPTION_REGION" );
38
- }
32
+ IntentPatternMatchingWithMicrophoneAsync ().Wait ();
33
+ }
34
+
35
+ private static async Task IntentPatternMatchingWithMicrophoneAsync ()
36
+ {
37
+ var config = SpeechConfig .FromSubscription (" YOUR_SUBSCRIPTION_KEY" , " YOUR_SUBSCRIPTION_REGION" );
39
38
}
40
39
}
40
+ }
41
41
```
42
42
43
43
## Create a Speech configuration
@@ -67,9 +67,9 @@ We will add 2 intents with the same ID for changing floors, and another intent w
67
67
Insert this code inside the ` using ` block:
68
68
69
69
``` C#
70
- intentRecognizer .AddIntent (" Take me to floor {floorName}." , " ChangeFloors" );
71
- intentRecognizer .AddIntent (" Go to floor {floorName}." , " ChangeFloors" );
72
- intentRecognizer .AddIntent (" {action} the door." , " OpenCloseDoor" );
70
+ intentRecognizer .AddIntent (" Take me to floor {floorName}." , " ChangeFloors" );
71
+ intentRecognizer .AddIntent (" Go to floor {floorName}." , " ChangeFloors" );
72
+ intentRecognizer .AddIntent (" {action} the door." , " OpenCloseDoor" );
73
73
```
74
74
75
75
> [ !NOTE]
@@ -82,9 +82,9 @@ From the `IntentRecognizer` object, you're going to call the `RecognizeOnceAsync
82
82
Insert this code below your intents:
83
83
84
84
``` C#
85
- Console .WriteLine (" Say something..." );
85
+ Console .WriteLine (" Say something..." );
86
86
87
- var result = await recognizer .RecognizeOnceAsync ();
87
+ var result = await intentRecognizer .RecognizeOnceAsync ();
88
88
```
89
89
90
90
## Display the recognition results (or errors)
@@ -94,62 +94,64 @@ When the recognition result is returned by the Speech service, we will print the
94
94
Insert this code below ` var result = await recognizer.RecognizeOnceAsync(); ` :
95
95
96
96
``` C#
97
+ string floorName ;
97
98
switch (result .Reason )
98
99
{
99
- case ResultReason .RecognizedSpeech :
100
+ case ResultReason .RecognizedSpeech :
100
101
Console .WriteLine ($" RECOGNIZED: Text= {result .Text }" );
101
102
Console .WriteLine ($" Intent not recognized." );
102
103
break ;
103
- case ResultReason .RecognizedIntent :
104
- Console .WriteLine ($" RECOGNIZED: Text= {result .Text }" );
105
- Console .WriteLine ($" Intent Id= {result .IntentId }." );
106
- var entities = result .Entities ;
107
- if (entities .TryGetValue (" floorName" , out string floorName ))
108
- {
109
- Console .WriteLine ($" FloorName= {floorName }" );
110
- }
111
-
112
- if (entities .TryGetValue (" action" , out string floorName ))
113
- {
114
- Console .WriteLine ($" Action= {floorName }" );
115
- }
116
-
117
- break ;
118
- case ResultReason .NoMatch :
119
- {
120
- Console .WriteLine ($" NOMATCH: Speech could not be recognized." );
121
- var noMatch = NoMatchDetails .FromResult (result );
122
- switch (noMatch ->Reason )
123
- {
124
- case NoMatchReason .NotRecognized :
125
- Console .WriteLine ($" NOMATCH: Speech was detected, but not recognized." );
126
- break ;
127
- case NoMatchReason .InitialSilenceTimeout :
128
- Console .WriteLine ($" NOMATCH: The start of the audio stream contains only silence, and the service timed out waiting for speech." );
129
- break ;
130
- case NoMatchReason .InitialBabbleTimeout :
131
- Console .WriteLine ($" NOMATCH: The start of the audio stream contains only noise, and the service timed out waiting for speech." );
104
+ case ResultReason .RecognizedIntent :
105
+ Console .WriteLine ($" RECOGNIZED: Text= {result .Text }" );
106
+ Console .WriteLine ($" Intent Id= {result .IntentId }." );
107
+ var entities = result .Entities ;
108
+ if (entities .TryGetValue (" floorName" , out floorName ))
109
+ {
110
+ Console .WriteLine ($" FloorName= {floorName }" );
111
+ }
112
+
113
+ if (entities .TryGetValue (" action" , out floorName ))
114
+ {
115
+ Console .WriteLine ($" Action= {floorName }" );
116
+ }
117
+
132
118
break ;
133
- case NoMatchReason .KeywordNotRecognized :
134
- Console .WriteLine ($" NOMATCH: Keyword not recognized" );
119
+ case ResultReason .NoMatch :
120
+ {
121
+ Console .WriteLine ($" NOMATCH: Speech could not be recognized." );
122
+ var noMatch = NoMatchDetails .FromResult (result );
123
+ switch (noMatch .Reason )
124
+ {
125
+ case NoMatchReason .NotRecognized :
126
+ Console .WriteLine ($" NOMATCH: Speech was detected, but not recognized." );
127
+ break ;
128
+ case NoMatchReason .InitialSilenceTimeout :
129
+ Console .WriteLine ($" NOMATCH: The start of the audio stream contains only silence, and the service timed out waiting for speech." );
130
+ break ;
131
+ case NoMatchReason .InitialBabbleTimeout :
132
+ Console .WriteLine ($" NOMATCH: The start of the audio stream contains only noise, and the service timed out waiting for speech." );
133
+ break ;
134
+ case NoMatchReason .KeywordNotRecognized :
135
+ Console .WriteLine ($" NOMATCH: Keyword not recognized" );
136
+ break ;
137
+ }
135
138
break ;
136
139
}
137
- break ;
138
- }
139
- case ResultReason .Canceled :
140
- {
141
- var cancellation = CancellationDetails .FromResult (result );
142
- Console .WriteLine ($" CANCELED: Reason={cancellation .Reason }" );
143
-
144
- if (cancellation .Reason == CancellationReason .Error )
140
+ case ResultReason .Canceled :
145
141
{
146
- Console .WriteLine ($" CANCELED: ErrorCode={cancellation .ErrorCode }" );
147
- Console .WriteLine ($" CANCELED: ErrorDetails={cancellation .ErrorDetails }" );
148
- Console .WriteLine ($" CANCELED: Did you set the speech resource key and region values?" );
142
+ var cancellation = CancellationDetails .FromResult (result );
143
+ Console .WriteLine ($" CANCELED: Reason={cancellation .Reason }" );
144
+
145
+ if (cancellation .Reason == CancellationReason .Error )
146
+ {
147
+ Console .WriteLine ($" CANCELED: ErrorCode={cancellation .ErrorCode }" );
148
+ Console .WriteLine ($" CANCELED: ErrorDetails={cancellation .ErrorDetails }" );
149
+ Console .WriteLine ($" CANCELED: Did you set the speech resource key and region values?" );
150
+ }
151
+ break ;
149
152
}
150
- }
151
- default :
152
- break ;
153
+ default :
154
+ break ;
153
155
}
154
156
```
155
157
@@ -158,49 +160,50 @@ default:
158
160
At this point, your code should look like this:
159
161
160
162
``` C#
161
- using System ;
162
- using System .Threading .Tasks ;
163
- using Microsoft .CognitiveServices .Speech ;
164
- using Microsoft .CognitiveServices .Speech .Intent ;
165
-
166
- namespace helloworld
163
+ using System ;
164
+ using System .Threading .Tasks ;
165
+ using Microsoft .CognitiveServices .Speech ;
166
+ using Microsoft .CognitiveServices .Speech .Intent ;
167
+
168
+ namespace helloworld
169
+ {
170
+ class Program
167
171
{
168
- class Program
172
+ static void Main ( string [] args )
169
173
{
170
- static void Main (string [] args )
171
- {
172
- IntentPatternMatchingWithMicrophoneAsync ().Wait ();
173
- }
174
-
175
- private static async Task IntentPatternMatchingWithMicrophoneAsync ()
174
+ IntentPatternMatchingWithMicrophoneAsync ().Wait ();
175
+ }
176
+
177
+ private static async Task IntentPatternMatchingWithMicrophoneAsync ()
178
+ {
179
+ var config = SpeechConfig .FromSubscription (" YOUR_SUBSCRIPTION_KEY" , " YOUR_SUBSCRIPTION_REGION" );
180
+ using (var intentRecognizer = new IntentRecognizer (config ))
176
181
{
177
- var config = SpeechConfig .FromSubscription (" YOUR_SUBSCRIPTION_KEY" , " YOUR_SUBSCRIPTION_REGION" );
178
- using (var intentRecognizer = new IntentRecognizer (config ))
182
+ intentRecognizer .AddIntent (" Take me to floor {floorName}." , " ChangeFloors" );
183
+ intentRecognizer .AddIntent (" Go to floor {floorName}." , " ChangeFloors" );
184
+ intentRecognizer .AddIntent (" {action} the door." , " OpenCloseDoor" );
185
+
186
+ Console .WriteLine (" Say something..." );
187
+
188
+ var result = await intentRecognizer .RecognizeOnceAsync ();
189
+
190
+ string floorName ;
191
+ switch (result .Reason )
179
192
{
180
- intentRecognizer .AddIntent (" Take me to floor {floorName}." , " ChangeFloors" );
181
- intentRecognizer .AddIntent (" Go to floor {floorName}." , " ChangeFloors" );
182
- intentRecognizer .AddIntent (" {action} the door." , " OpenCloseDoor" );
183
-
184
- Console .WriteLine (" Say something..." );
185
-
186
- var result = await recognizer .RecognizeOnceAsync ();
187
-
188
- switch (result .Reason )
189
- {
190
193
case ResultReason .RecognizedSpeech :
191
- Console .WriteLine ($" RECOGNIZED: Text= {result .Text }" );
192
- Console .WriteLine ($" Intent not recognized." );
193
- break ;
194
+ Console .WriteLine ($" RECOGNIZED: Text= {result .Text }" );
195
+ Console .WriteLine ($" Intent not recognized." );
196
+ break ;
194
197
case ResultReason .RecognizedIntent :
195
198
Console .WriteLine ($" RECOGNIZED: Text= {result .Text }" );
196
199
Console .WriteLine ($" Intent Id= {result .IntentId }." );
197
200
var entities = result .Entities ;
198
- if (entities .TryGetValue (" floorName" , out string floorName ))
201
+ if (entities .TryGetValue (" floorName" , out floorName ))
199
202
{
200
203
Console .WriteLine ($" FloorName= {floorName }" );
201
204
}
202
205
203
- if (entities .TryGetValue (" action" , out string floorName ))
206
+ if (entities .TryGetValue (" action" , out floorName ))
204
207
{
205
208
Console .WriteLine ($" Action= {floorName }" );
206
209
}
@@ -210,20 +213,20 @@ At this point, your code should look like this:
210
213
{
211
214
Console .WriteLine ($" NOMATCH: Speech could not be recognized." );
212
215
var noMatch = NoMatchDetails .FromResult (result );
213
- switch (noMatch -> Reason )
216
+ switch (noMatch . Reason )
214
217
{
215
- case NoMatchReason .NotRecognized :
216
- Console .WriteLine ($" NOMATCH: Speech was detected, but not recognized." );
217
- break ;
218
- case NoMatchReason .InitialSilenceTimeout :
219
- Console .WriteLine ($" NOMATCH: The start of the audio stream contains only silence, and the service timed out waiting for speech." );
220
- break ;
221
- case NoMatchReason .InitialBabbleTimeout :
222
- Console .WriteLine ($" NOMATCH: The start of the audio stream contains only noise, and the service timed out waiting for speech." );
223
- break ;
224
- case NoMatchReason .KeywordNotRecognized :
225
- Console .WriteLine ($" NOMATCH: Keyword not recognized" );
226
- break ;
218
+ case NoMatchReason .NotRecognized :
219
+ Console .WriteLine ($" NOMATCH: Speech was detected, but not recognized." );
220
+ break ;
221
+ case NoMatchReason .InitialSilenceTimeout :
222
+ Console .WriteLine ($" NOMATCH: The start of the audio stream contains only silence, and the service timed out waiting for speech." );
223
+ break ;
224
+ case NoMatchReason .InitialBabbleTimeout :
225
+ Console .WriteLine ($" NOMATCH: The start of the audio stream contains only noise, and the service timed out waiting for speech." );
226
+ break ;
227
+ case NoMatchReason .KeywordNotRecognized :
228
+ Console .WriteLine ($" NOMATCH: Keyword not recognized" );
229
+ break ;
227
230
}
228
231
break ;
229
232
}
@@ -238,14 +241,15 @@ At this point, your code should look like this:
238
241
Console .WriteLine ($" CANCELED: ErrorDetails={cancellation .ErrorDetails }" );
239
242
Console .WriteLine ($" CANCELED: Did you set the speech resource key and region values?" );
240
243
}
244
+ break ;
241
245
}
242
246
default :
243
247
break ;
244
- }
245
248
}
246
249
}
247
250
}
248
251
}
252
+ }
249
253
```
250
254
## Build and run your app
251
255
0 commit comments