Exemplo n.º 1
0
        static async Task CustomRecognizeAsync()
        {
            //custom speech recognition with continuous recognition
            var config = SpeechConfig.FromSubscription(customSpeechKey, customSpeechRegion);

            config.EndpointId = customSpeechId;

            var stopRecognition = new TaskCompletionSource <int>();

            using (var recognizer = new SpeechRecognizer(config))
            {
                recognizer.Recognizing += (s, e) =>
                {
                    Console.WriteLine($"Recognizing: Text = {e.Result.Text}");
                };

                recognizer.Recognized += (s, e) =>
                {
                    if (e.Result.Reason == ResultReason.RecognizedSpeech)
                    {
                        Console.WriteLine($"Recognized: Text = {e.Result.Text}");
                        //get the full text, send the request to luis to identify intent and get response
                        LUIS.SendToLuis(e.Result.Text, luisAppId, luisKey).Wait();
                    }
                };

                recognizer.Canceled += (s, e) =>
                {
                    Console.WriteLine($"Canceled: Reason={e.Reason}");
                    if (e.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                    stopRecognition.TrySetResult(0);
                };

                recognizer.SessionStarted += (s, e) =>
                {
                    Console.WriteLine("\n Session started event.");
                };

                recognizer.SessionStopped += (s, e) =>
                {
                    Console.WriteLine("\n Session stopped.");
                    stopRecognition.TrySetResult(0);
                };

                Console.WriteLine("[Custom Speech] Say something...");
                await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                do
                {
                    Console.WriteLine("Press Enter to stop");
                } while (Console.ReadKey().Key != ConsoleKey.Enter);

                await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
            }
        }
Exemplo n.º 2
0
        static async Task ContinuousRecognizeIntentAsync()
        {
            //continuous speech recognition and detect intent
            var config = SpeechConfig.FromSubscription(luisKey, luisRegion);

            using (var recognizer = new IntentRecognizer(config))
            {
                var stopRecognition = new TaskCompletionSource <int>();

                var model = LanguageUnderstandingModel.FromAppId(luisAppId);
                recognizer.AddAllIntents(model);

                recognizer.Recognizing += (s, e) =>
                {
                    Console.WriteLine($"Recognizing: Text = {e.Result.Text}");
                };

                recognizer.Recognized += (s, e) =>
                {
                    if (e.Result.Reason == ResultReason.RecognizedIntent)
                    {
                        Console.WriteLine($"Recognized: Text = {e.Result.Text}");
                        LUIS.GetLuisResult(e.Result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult));
                        string intent = LUIS.GetLuisIntent(e.Result.Properties.GetProperty(PropertyId.LanguageUnderstandingServiceResponse_JsonResult));
                        LUIS.ResponseToLuisIntent(intent, e.Result.Text);
                    }

                    else if (e.Result.Reason == ResultReason.RecognizedSpeech)
                    {
                        Console.WriteLine($"Recognized: Text = {e.Result.Text}");
                        Console.WriteLine("Intent not recognized.");
                    }
                };

                recognizer.Canceled += (s, e) =>
                {
                    Console.WriteLine($"Canceled: Reason={e.Reason}");
                    if (e.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={e.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                    stopRecognition.TrySetResult(0);
                };

                recognizer.SessionStarted += (s, e) =>
                {
                    Console.WriteLine("\n Session started event.");
                };

                recognizer.SessionStopped += (s, e) =>
                {
                    Console.WriteLine("\n Session stopped.");
                    stopRecognition.TrySetResult(0);
                };

                Console.WriteLine("[Continous Recognition] Say something...");
                await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                do
                {
                    Console.WriteLine("Press Enter to stop");
                } while (Console.ReadKey().Key != ConsoleKey.Enter);

                await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
            }
        }