public virtual ConversationResponse ProcessUserInput(IConversationContext context)
        {
            if (string.IsNullOrWhiteSpace(context.Result.Query) || context.Result == null)
            {
                return(IntentProvider.GetDefaultResponse(context.AppId));
            }

            // gather data
            var intent         = IntentProvider.GetTopScoringIntent(context);
            var conversation   = context.GetCurrentConversation();
            var isConfident    = context.Result.TopScoringIntent.Score > ApiKeys.LuisIntentConfidenceThreshold;
            var hasValidIntent = intent != null && isConfident;
            var inConversation = conversation != null && !conversation.IsEnded;
            var requestedQuit  = hasValidIntent && intent.KeyName.Equals(context.QuitIntentName);

            // if the user is trying to end or finish a conversation
            if (inConversation && requestedQuit)
            {
                return(EndCurrentConversation(context));
            }

            // continue conversation
            if (inConversation)
            {
                return(HandleCurrentConversation(context));
            }

            // is a user frustrated or is their intention unclear
            var sentimentScore = context.Result.SentimentAnalysis?.score ?? 1;

            return((sentimentScore <= 0.4)
                ? IntentProvider.GetIntent(context.AppId, context.FrustratedIntentName)?.Respond(null, null, null) ?? IntentProvider.GetDefaultResponse(context.AppId)
                : IntentProvider.GetDefaultResponse(context.AppId));
        }
Ejemplo n.º 2
0
        public ConversationResponse HandleMessage(Activity activity, ItemContextParameters parameters)
        {
            IConversation conversation = (ConversationHistory.Conversations.Any())
                ? ConversationHistory.Conversations.Last()
                : null;

            var inConversation = conversation != null && !conversation.IsEnded;

            // determine which intent user wants and context
            var result        = LuisService.Query(AppId, activity.Text);
            var requestedQuit =
                result.TopScoringIntent.Intent.ToLower().Equals("quit") &&
                result.TopScoringIntent.Score > 0.4;

            var intent = IntentProvider.GetIntent(AppId, result.TopScoringIntent.Intent);

            // if the user is trying to end or finish a conversation
            if (inConversation && requestedQuit)
            {
                conversation.IsEnded = true;
                return(intent.Respond(null, null, null));
            }

            // start a new conversation if not in one
            if (!inConversation && intent != null && result.TopScoringIntent.Score > 0.4)
            {
                conversation = ConversationFactory.Create(result, intent);
                ConversationHistory.Conversations.Add(conversation);
                inConversation = true;
            }

            if (inConversation)
            {
                // check and request all required parameters of a conversation
                foreach (ConversationParameter p in conversation.Intent.RequiredParameters)
                {
                    if (!TryGetParam(p.ParamName, result, conversation, parameters, p.ParamGetter))
                    {
                        return(RequestParam(p, conversation, parameters));
                    }
                }

                conversation.IsEnded = true;

                return(conversation.Intent.Respond(result, parameters, conversation));
            }

            // determine mood of comment
            var sentiment      = GetSentiment(activity.Text);
            var sentimentScore = (sentiment?.Documents != null && sentiment.Documents.Any())
                ? sentiment.Documents.First().Score
                : 1;

            // is a user frustrated or is their intention unclear
            return((sentimentScore <= 0.4)
                ? IntentProvider.GetIntent(AppId, "frustrated user").Respond(null, null, null)
                : IntentProvider.GetDefaultResponse(AppId));
        }