示例#1
0
        private async void btnName_Click(object sender, EventArgs e)
        {
            if (btnName.Text == "Start")
            {
                btnName.Text            = "Stop";
                recognizer.Recognizing += (s, e) =>
                {
                    //Log($"{e.Result.Text}{Environment.NewLine}");
                };

                recognizer.Recognized += async(s, e) =>
                {
                    if (e.Result.Reason == ResultReason.RecognizedSpeech)
                    {
                        if (!string.IsNullOrEmpty(e.Result.Text))
                        {
                            var result = await textAnalyticsHelper.AnalyzeSentitmentAsync(e.Result.Text);

                            var phrases = await textAnalyticsHelper.ExtractKeyPhrasesAsync(e.Result.Text);

                            string message = $"{result.Sentences.First().Text}{Environment.NewLine}";
                            message += $"Happiness:{result.Sentences.First().ConfidenceScores.Positive}{Environment.NewLine}";
                            message += $"Sadness:{result.Sentences.First().ConfidenceScores.Negative}{Environment.NewLine}";
                            message += $"Neutral:{result.Sentences.First().ConfidenceScores.Neutral}{Environment.NewLine}";
                            message += $"Key phrases:{ JsonConvert.SerializeObject( phrases.ToList()) } {Environment.NewLine}";
                            message += $"------------------------------------------------{Environment.NewLine}";
                            Log(message);
                        }
                    }
                    else if (e.Result.Reason == ResultReason.NoMatch)
                    {
                        Log($"NOMATCH: Speech could not be recognized.{Environment.NewLine}");
                    }
                };

                recognizer.Canceled += (s, e) =>
                {
                    Log($"CANCELED: Reason={e.Reason}{Environment.NewLine}");

                    if (e.Reason == CancellationReason.Error)
                    {
                        Log($"CANCELED: ErrorCode={e.ErrorCode}{Environment.NewLine}");
                        Log($"CANCELED: ErrorDetails={e.ErrorDetails}{Environment.NewLine}");
                        Log($"CANCELED: Did you update the subscription info?{Environment.NewLine}");
                    }

                    stopRecognition.TrySetResult(0);
                };

                recognizer.SessionStopped += (s, e) =>
                {
                    Log($"Session stopped event.{Environment.NewLine}");
                    stopRecognition.TrySetResult(0);
                };
                await recognizer.StartContinuousRecognitionAsync();

                await Task.WhenAny(stopRecognition.Task);
            }
            else
            {
                btnName.Text = "Start";

                await recognizer.StopContinuousRecognitionAsync();
            }
        }