예제 #1
0
        private async Task <DialogTurnResult> ArtistStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var songRequest = (SongRequest)stepContext.Options;

            if (songRequest.Intent == UserCommand.Intent.PlayByArtist || songRequest.Intent == UserCommand.Intent.PlayByTitleArtist)
            {
                var(bestMatchArtist, _) = _songChooser.GetClosestKnownArtist(songRequest.Artist, _songChooser.ThresholdSimilarityRatio);
                Console.WriteLine(songRequest.Artist + " => " + bestMatchArtist);

                if (bestMatchArtist is null)
                {
                    (bestMatchArtist, _) = _songChooser.GetClosestKnownArtist(songRequest.Artist);
                    Console.WriteLine(songRequest.Artist + " => " + bestMatchArtist);
                    var(messageText, spokenMessageText) = SentenceGenerator.AskArtistName(bestMatchArtist);
                    var promptMessage = MessageFactory.Text(messageText, spokenMessageText, InputHints.ExpectingInput);
                    return(await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = promptMessage }, cancellationToken));
                }
            }

            return(await stepContext.NextAsync(songRequest.Artist, cancellationToken));
        }
예제 #2
0
        private async Task <DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var         userInput = stepContext.Context.Activity.Text;
            SongRequest songRequest;

            if (!_luisCommandRecognizer.IsConfigured)
            {
                songRequest = new SongRequest()
                {
                    Artist = _songChooser.GetKnownArtistFromSentence(userInput),
                    Title  = _songChooser.GetKnownSongTitleFromSentence(userInput)
                };
                // LUIS is not configured, we just run the RequestSongDialog path with an empty SongDetailsInstance.
                return(await stepContext.BeginDialogAsync(nameof(RequestSongDialog), songRequest, cancellationToken));
            }

            if (_luisQuestionRecognizer.IsConfigured)
            {
                var luisQuestionResult = await _luisQuestionRecognizer.RecognizeAsync <UserQuestion>(stepContext.Context, cancellationToken);

                var(intent, score) = luisQuestionResult.TopIntent();
                if (!(intent is UserQuestion.Intent.None))
                {
                    var    feedbackScoreThreshold = 0.8;
                    Answer answer = new Answer()
                    {
                        Text  = "",
                        Speak = ""
                    };
                    switch (intent)
                    {
                    case UserQuestion.Intent.CurrentSongInformationRequest:
                        var question = new Question()
                        {
                            Text   = userInput,
                            Intent = intent
                        };
                        answer = new Answer(question);

                        // Which song

                        /*switch (luisQuestionResult.SongDescription)
                         * {
                         *  case "current song":
                         *      break;
                         *  case "next song":
                         *  case "last song":
                         *  default:
                         *      break;
                         * }*/

                        // Which information requested
                        var song = _musicPlayer.GetPlayerStatus().GetCurrentSong();
                        (answer.Text, answer.Speak) = GenerateResponseToInformationRequest(luisQuestionResult.SongInformation, song);
                        break;

                    case UserQuestion.Intent.NegativeFeedback:
                        // check certainty percentage to avoid false positives
                        if (score > feedbackScoreThreshold)
                        {
                            (answer.Text, answer.Speak) = ("I don't like it either.", "I don't like it either.");
                        }
                        break;

                    case UserQuestion.Intent.PositiveFeedback:
                        // check certainty percentage to avoid false positives
                        if (score > feedbackScoreThreshold)
                        {
                            (answer.Text, answer.Speak) = ("It's nice indeed.", "It's nice indeed.");
                        }
                        break;

                    default:
                        break;
                    }
                    return(await stepContext.NextAsync(answer, cancellationToken));
                }
            }

            // Call LUIS and gather any potential song details. (Note the TurnContext has the response to the prompt.)
            var luisCommandResult = await _luisCommandRecognizer.RecognizeAsync <UserCommand>(stepContext.Context, cancellationToken);

            songRequest = new SongRequest()
            {
                Intent = luisCommandResult.TopIntent().intent
            };

            var    inputHint            = InputHints.IgnoringInput;
            string messageText          = "";
            string spokenMessageText    = "";
            Song   songToBePlayed       = null;
            var    intentScoreThreshold = 0.5;

            switch (luisCommandResult.TopIntent().intent)
            {
            case UserCommand.Intent.PlayByTitleArtist:

                // Initialize SongRequest with any entities we may have found in the response.
                songRequest.Title  = luisCommandResult.SongTitle;
                songRequest.Artist = luisCommandResult.SongArtist;
                songToBePlayed     = SelectSongToBePlayedByTitleArtist(songRequest, userInput);

                if (songToBePlayed is null)
                {
                    // Reset song request if both known, but not a pair
                    if (!(songRequest.Title is null || songRequest.Artist is null))
                    {
                        if (songRequest.Title.Length > songRequest.Artist.Length)
                        {
                            songRequest.Artist = null;
                        }
                        else
                        {
                            songRequest.Title = null;
                        }
                    }
                    // Run the RequestSongDialog giving it whatever details we have from the LUIS call, it will fill out the remainder.
                    return(await stepContext.BeginDialogAsync(nameof(RequestSongDialog), songRequest, cancellationToken));
                }
                break;

            case UserCommand.Intent.PlayByArtist:

                // Initialize SongRequest with any entities we may have found in the response.
                songRequest.Artist = luisCommandResult.SongArtist;
                if (songRequest.Artist is null)
                {
                    songRequest.Artist = _songChooser.GetKnownArtistFromSentence(userInput);
                }

                var(bestMatchArtist, similarityRatioArtist) = _songChooser.GetClosestKnownArtist(luisCommandResult.SongArtist, _songChooser.ThresholdSimilarityRatio);
                Console.WriteLine(songRequest.Artist + " => " + bestMatchArtist);

                if (bestMatchArtist is null)
                {
                    songRequest.Artist = _songChooser.ExpandSentenceToKnownArtist(userInput);

                    if (songRequest.Artist is null)
                    {
                        // Run the RequestSongDialog giving it whatever details we have from the LUIS call, it will fill out the remainder.
                        return(await stepContext.BeginDialogAsync(nameof(RequestSongDialog), songRequest, cancellationToken));
                    }
                    bestMatchArtist = songRequest.Artist;
                }
                songRequest.Artist = bestMatchArtist;
                songToBePlayed     = _songChooser.ChooseRandomSongByArtist(songRequest.Artist);
                messageText        = "Random song by artist.";
                break;

            case UserCommand.Intent.PlayByTitle:

                // Initialize SongRequest with any entities we may have found in the response.
                songRequest.Title = luisCommandResult.SongTitle;
                if (songRequest.Title is null)
                {
                    songRequest.Title = _songChooser.GetKnownSongTitleFromSentence(userInput);
                }

                var(bestMatchTitle, similarityRatioTitle) = _songChooser.GetClosestKnownSongTitle(luisCommandResult.SongTitle, _songChooser.ThresholdSimilarityRatio);

                if (bestMatchTitle is null)
                {
                    songRequest.Title = _songChooser.ExpandSentenceToKnownSongTitle(userInput);

                    if (songRequest.Title is null)
                    {
                        // Run the RequestSongDialog giving it whatever details we have from the LUIS call, it will fill out the remainder.
                        return(await stepContext.BeginDialogAsync(nameof(RequestSongDialog), songRequest, cancellationToken));
                    }
                    bestMatchTitle = songRequest.Title;
                }
                songRequest.Title = bestMatchTitle;
                songToBePlayed    = _songChooser.GetSongByClosestTitle(songRequest.Title);
                break;

            case UserCommand.Intent.PlayByGenre:

                // Initialize SongRequest with any entities we may have found in the response.
                songRequest.Genre = luisCommandResult.SongGenre;

                var(bestMatchGenre, similarityRatioGenre) = _songChooser.GetClosestKnownGenre(luisCommandResult.SongGenre, _songChooser.ThresholdSimilarityRatio);

                if (bestMatchGenre is null)
                {
                    // Run the RequestSongByGenreDialog giving it whatever details we have from the LUIS call, it will fill out the remainder.
                    return(await stepContext.BeginDialogAsync(nameof(RequestSongByGenreDialog), songRequest, cancellationToken));
                }

                songToBePlayed = _songChooser.ChooseRandomSongByGenre(bestMatchGenre);
                messageText    = "Random song by genre.";
                break;

            case UserCommand.Intent.PlayRandom:
                if (luisCommandResult.TopIntent().score > intentScoreThreshold)
                {
                    songToBePlayed = _songChooser.ChooseRandomSong();
                    messageText    = "Random song.";
                }
                break;

            case UserCommand.Intent.PreviousSong:
                if (luisCommandResult.TopIntent().score > intentScoreThreshold)
                {
                    _musicPlayer.PlayPrevious();
                    messageText = "Previous song.";
                }
                break;

            case UserCommand.Intent.NextSong:
                if (luisCommandResult.TopIntent().score > intentScoreThreshold)
                {
                    _musicPlayer.PlayNext();
                    messageText = "Next song.";
                }
                break;

            case UserCommand.Intent.StartPlaying:
                _musicPlayer.Resume();
                messageText = "Music resumed.";
                break;

            case UserCommand.Intent.StopPlaying:
                _musicPlayer.Pause();
                messageText = "Music paused.";
                break;

            case UserCommand.Intent.VolumeDown:
                if (luisCommandResult.TopIntent().score > intentScoreThreshold)
                {
                    _musicPlayer.VolumeDown();
                    messageText = "Volume decreased by 10%.";
                }
                break;

            case UserCommand.Intent.VolumeUp:
                _musicPlayer.VolumeUp(20);
                messageText = "Volume increased by 20%.";
                break;

            default:
                // Catch all for unhandled intents
                messageText            = $"Sorry, I didn't get that. Please try asking in a different way (intent was {luisCommandResult.TopIntent().intent})";
                (_, spokenMessageText) = SentenceGenerator.CommandNotUnderstood();
                inputHint = InputHints.ExpectingInput;
                break;
            }

            var message = MessageFactory.Text(messageText, spokenMessageText, inputHint);
            await stepContext.Context.SendActivityAsync(message, cancellationToken);

            return(await stepContext.NextAsync(songToBePlayed, cancellationToken));
        }