Skip to content

Commit 5268739

Browse files
author
Ryan Hill
committed
(Azure CXP) code errors
fixes MicrosoftDocs/azure-docs#97390
1 parent 29c3730 commit 5268739

File tree

1 file changed

+115
-111
lines changed

1 file changed

+115
-111
lines changed

articles/cognitive-services/Speech-Service/includes/how-to/intent-recognition/csharp/simple-pattern-matching.md

Lines changed: 115 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ Create a new C# console application project in Visual Studio 2019 and [install t
1818
Let's open `Program.cs` and add some code that works as a skeleton for our project.
1919

2020
```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
2729
{
28-
class Program
30+
static void Main(string[] args)
2931
{
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");
3938
}
4039
}
40+
}
4141
```
4242

4343
## Create a Speech configuration
@@ -67,9 +67,9 @@ We will add 2 intents with the same ID for changing floors, and another intent w
6767
Insert this code inside the `using` block:
6868

6969
```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");
7373
```
7474

7575
> [!NOTE]
@@ -82,9 +82,9 @@ From the `IntentRecognizer` object, you're going to call the `RecognizeOnceAsync
8282
Insert this code below your intents:
8383

8484
```C#
85-
Console.WriteLine("Say something...");
85+
Console.WriteLine("Say something...");
8686

87-
var result = await recognizer.RecognizeOnceAsync();
87+
var result = await intentRecognizer.RecognizeOnceAsync();
8888
```
8989

9090
## Display the recognition results (or errors)
@@ -94,62 +94,64 @@ When the recognition result is returned by the Speech service, we will print the
9494
Insert this code below `var result = await recognizer.RecognizeOnceAsync();`:
9595

9696
```C#
97+
string floorName;
9798
switch (result.Reason)
9899
{
99-
case ResultReason.RecognizedSpeech:
100+
case ResultReason.RecognizedSpeech:
100101
Console.WriteLine($"RECOGNIZED: Text= {result.Text}");
101102
Console.WriteLine($" Intent not recognized.");
102103
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+
132118
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+
}
135138
break;
136139
}
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:
145141
{
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;
149152
}
150-
}
151-
default:
152-
break;
153+
default:
154+
break;
153155
}
154156
```
155157

@@ -158,49 +160,50 @@ default:
158160
At this point, your code should look like this:
159161

160162
```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
167171
{
168-
class Program
172+
static void Main(string[] args)
169173
{
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))
176181
{
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)
179192
{
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-
{
190193
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;
194197
case ResultReason.RecognizedIntent:
195198
Console.WriteLine($"RECOGNIZED: Text= {result.Text}");
196199
Console.WriteLine($" Intent Id= {result.IntentId}.");
197200
var entities = result.Entities;
198-
if (entities.TryGetValue("floorName", out string floorName))
201+
if (entities.TryGetValue("floorName", out floorName))
199202
{
200203
Console.WriteLine($" FloorName= {floorName}");
201204
}
202205

203-
if (entities.TryGetValue("action", out string floorName))
206+
if (entities.TryGetValue("action", out floorName))
204207
{
205208
Console.WriteLine($" Action= {floorName}");
206209
}
@@ -210,20 +213,20 @@ At this point, your code should look like this:
210213
{
211214
Console.WriteLine($"NOMATCH: Speech could not be recognized.");
212215
var noMatch = NoMatchDetails.FromResult(result);
213-
switch (noMatch->Reason)
216+
switch (noMatch.Reason)
214217
{
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;
227230
}
228231
break;
229232
}
@@ -238,14 +241,15 @@ At this point, your code should look like this:
238241
Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
239242
Console.WriteLine($"CANCELED: Did you set the speech resource key and region values?");
240243
}
244+
break;
241245
}
242246
default:
243247
break;
244-
}
245248
}
246249
}
247250
}
248251
}
252+
}
249253
```
250254
## Build and run your app
251255

0 commit comments

Comments
 (0)