Esempio n. 1
0
        protected virtual async Task DispatchToIntentHandler(IDialogContext context,
                                                             IAwaitable <IMessageActivity> item,
                                                             IntentRecommendation bestIntent,
                                                             LuisResult result)
        {
            if (this.handlerByIntent == null)
            {
                this.handlerByIntent = new Dictionary <string, IntentActivityHandler>(GetHandlersByIntent());
            }

            IntentActivityHandler handler = null;

            if (result == null || !this.handlerByIntent.TryGetValue(bestIntent.Intent, out handler))
            {
                handler = this.handlerByIntent[string.Empty];
            }

            if (handler != null)
            {
                await handler(context, item, result);
            }
            else
            {
                var text = $"No default intent handler found.";
                throw new Exception(text);
            }
        }
        public async Task ProfileUpdatesModule(IDialogContext context, LuisResult result)
        {
            IntentRecommendation re = result.TopScoringIntent;
            string test             = re.Intent;

            context.Call(new ProfileUpdatesDialog(), Callback);
        }
 protected override async Task DispatchToIntentHandler(IDialogContext context, IAwaitable <IMessageActivity> item, IntentRecommendation bestIntent, LuisResult result)
 {
     if (GoToHandler != null)
     {
         var task = GoToHandler(context, result);
         GoToHandler = null;
         await task;
     }
     else
     {
         if (bestIntent.Score < this.HighThreshold && bestIntent.Score > LowThreshold)
         {
             bestIntent = new IntentRecommendation()
             {
                 Intent = MEDIUM_THRESHOLD_DIALOG, Score = 1
             };
         }
         else if (bestIntent.Score < this.LowThreshold)
         {
             bestIntent = new IntentRecommendation()
             {
                 Intent = LOW_THRESHOLD_DIALOG, Score = 1
             };
         }
         await base.DispatchToIntentHandler(context, item, bestIntent, result);
     }
 }
 public Scope(ILuisModel model, LuisResult result, IntentRecommendation intent, IResolver inner)
     : base(inner)
 {
     SetField.NotNull(out this.Model, nameof(model), model);
     SetField.NotNull(out this.Result, nameof(result), result);
     SetField.NotNull(out this.Intent, nameof(intent), intent);
 }
Esempio n. 5
0
 public LuisServiceResult(LuisResult result, IntentRecommendation intent, ILuisService service, ILuisOptions luisRequest = null)
 {
     this.Result      = result;
     this.BestIntent  = intent;
     this.LuisService = service;
     this.LuisRequest = luisRequest;
 }
        public async Task ArticleSearch(IDialogContext context, LuisResult result)
        {
            IntentRecommendation re = result.TopScoringIntent;
            string test             = re.Intent;

            //await context.PostAsync(test);
            context.Call(new ArticleSearch(test), Callback);
        }
Esempio n. 7
0
        public async Task None(IDialogContext context, LuisResult result)
        {
            string message = "Sorry I did not understand.";
            IntentRecommendation bestBet = this.BestIntentFrom(result);
            await context.PostAsync(message);

            context.Wait(MessageReceived);
        }
Esempio n. 8
0
        public static IntentRecommendation[] IntentsFor <D>(Expression <Func <D, Task> > expression, double?score)
        {
            var body      = (MethodCallExpression)expression.Body;
            var attribute = body.Method.GetCustomAttribute <LuisIntentAttribute>();
            var name      = attribute.IntentName;
            var intent    = new IntentRecommendation(name, score);

            return(new[] { intent });
        }
Esempio n. 9
0
        public static IntentRecommendation[] IntentsFor(Expression <Func <SimpleAlarmBot.SimpleAlarmDialog, Task> > expression)
        {
            var body      = (MethodCallExpression)expression.Body;
            var attribute = body.Method.GetCustomAttribute <LuisIntentAttribute>();
            var name      = attribute.IntentName;
            var intent    = new IntentRecommendation(name);

            return(new[] { intent });
        }
Esempio n. 10
0
        protected override async Task DispatchToIntentHandler(IDialogContext context, IAwaitable <IMessageActivity> item,
                                                              IntentRecommendation bestIntent, LuisResult result)
        {
            string message =
                $"ULIS translated that as: {result.Query}\n\n" +
                $"Intent: {result.TopScoringIntent.Intent} | Score: {result.TopScoringIntent.Score}\n\n" +
                $"Entities: {JsonConvert.SerializeObject(result.Entities)}";
            await context.PostAsync(message);

            context.Done(0);
        }
Esempio n. 11
0
        public void NullOrEmptyIntents_DefaultsTo_TopScoringIntent()
        {
            var intent = new IntentRecommendation();
            var result = new LuisResult()
            {
                TopScoringIntent = intent
            };

            LuisService.Fix(result);

            Assert.AreEqual(1, result.Intents.Count);
            Assert.AreEqual(intent, result.Intents[0]);
        }
Esempio n. 12
0
        private async Task WelcomeOptionSelection(IDialogContext context, IAwaitable <string> result)
        {
            EntityRecommendation er = new EntityRecommendation();

            er.Entity = await result;
            var entities = new List <EntityRecommendation>();
            //entities.Add(new EntityRecommendation(type: "MeetExpert") { Entity = er.Entity });

            IntentRecommendation ir = new IntentRecommendation();

            ir.Intent = await result;
            var intents = new List <IntentRecommendation>();
            //    intents.Add(new IntentRecommendation(intent: "MeetExpert") { Intent = er.Entity });

            LuisResult lr = new LuisResult(await result, intents, entities);


            switch (await result)
            {
            case "Confirm Your Subscription":
                await context.PostAsync("!!! Coming soon... !!!");

                context.Wait(MessageReceived);
                break;

            case "Register For Tech Support":
                await RegisterForTechSupport(context, lr);

                break;

            case "Trainings":
                await Trainings(context, lr);

                break;

            case "Ask A Support Question":
                await SupportQuestion(context, lr);

                break;

            case "Meet With An Expert":
                await MeetExpert(context, lr);

                break;

            default:
                await None(context, lr);

                break;
            }
        }
Esempio n. 13
0
        protected virtual async Task MessageReceived(IDialogContext context, IAwaitable <IMessageActivity> item)
        {
            var message     = await item;
            var messageText = await GetLuisQueryTextAsync(context, message);

            if (messageText != null)
            {
                // Modify request by the service to add attributes and then by the dialog to reflect the particular query
                var tasks   = this.services.Select(s => s.QueryAsync(ModifyLuisRequest(s.ModifyRequest(new LuisRequest(messageText))), context.CancellationToken)).ToArray();
                var results = await Task.WhenAll(tasks);

                var winners = from result in results.Select((value, index) => new { value, index })
                              let resultWinner = BestIntentFrom(result.value)
                                                 where resultWinner != null
                                                 select new LuisServiceResult(result.value, resultWinner, this.services[result.index]);

                var winner = this.BestResultFrom(winners);

                if (winner == null)
                {
                    throw new InvalidOperationException("No winning intent selected from Luis results.");
                }

                if (winner.Result.Dialog?.Status == DialogResponse.DialogStatus.Question)
                {
#pragma warning disable CS0618
                    var childDialog = await MakeLuisActionDialog(winner.LuisService,
                                                                 winner.Result.Dialog.ContextId,
                                                                 winner.Result.Dialog.Prompt);

#pragma warning restore CS0618
                    context.Call(childDialog, LuisActionDialogFinished);
                }
                else
                {
                    await DispatchToIntentHandler(context, item, winner.BestIntent, winner.Result);
                }
            }
            else
            {
                var intent = new IntentRecommendation()
                {
                    Intent = string.Empty, Score = 1.0
                };
                var result = new LuisResult()
                {
                    TopScoringIntent = intent
                };
                await DispatchToIntentHandler(context, item, intent, result);
            }
        }
Esempio n. 14
0
        protected virtual async Task <DialogTurnResult> MessageReceived(DialogContext context, IMessageActivity item, CancellationToken cancellationToken = default(CancellationToken))
        {
            var message     = item;
            var messageText = await GetLuisQueryTextAsync(context, message);

            if (messageText != null)
            {
                // Modify request by the service to add attributes and then by the dialog to reflect the particular query
                var tasks = this.services.Select(async s =>
                {
                    var request = ModifyLuisRequest(s.ModifyRequest(new LuisRequest(messageText)));
                    var result  = await s.QueryAsync(request, cancellationToken);

                    return(Tuple.Create(request, result));
                }).ToArray();
                var results = await Task.WhenAll(tasks);



                var winners = from result in results.Select((value, index) => new { value = value.Item2, request = value.Item1, index })
                              let resultWinner = BestIntentFrom(result.value)
                                                 where resultWinner != null
                                                 select new LuisServiceResult(result.value, resultWinner, this.services[result.index], result.request);

                var winner = this.BestResultFrom(winners);

                if (winner == null)
                {
                    throw new InvalidOperationException("No winning intent selected from Luis results.");
                }

                await EmitTraceInfo(context, winner.Result, winner.LuisRequest, winner.LuisService.LuisModel);

                return(await DispatchToIntentHandler(context, item, winner.BestIntent, winner.Result));
            }
            else
            {
                var intent = new IntentRecommendation()
                {
                    Intent = string.Empty, Score = 1.0
                };
                var result = new LuisResult()
                {
                    TopScoringIntent = intent
                };
                return(await DispatchToIntentHandler(context, item, intent, result));
            }
        }
Esempio n. 15
0
        protected override async Task MessageReceived(IDialogContext context, IAwaitable <IMessageActivity> item)
        {
            try
            {
                var message     = await item;
                var messageText = await GetLuisQueryTextAsync(context, message);

                Trace.TraceInformation(":::::: User Message :::::::>" + messageText);

                if (!string.IsNullOrEmpty(messageText))
                {
                    var tasks   = this.services.Select(s => s.QueryAsync(messageText, context.CancellationToken)).ToArray();
                    var results = await Task.WhenAll(tasks);

                    var winners = from result in results.Select((value, index) => new { value, index })
                                  let resultWinner = BestIntentFrom(result.value)
                                                     where resultWinner != null && (resultWinner.Score > 0.6 || (results[0].Intents.Count == 1 && resultWinner.Score > 0.4)) //<== 가끔 0.6 이하로 걸리는 경우가 있으므로 점수가 0.4 이상인 결과가 하나만 나오는 경우 맞다고 간주.
                                                     select new LuisServiceResult(result.value, resultWinner, this.services[result.index]);

                    var winner = this.BestResultFrom(winners) ?? new LuisServiceResult(results[0], new IntentRecommendation {
                        Intent = "None", Score = 1
                    }, this.services[0]);
                    service = winner.LuisService;
                    await DispatchToIntentHandler(context, item, winner.BestIntent, winner.Result);
                }
                else
                {
                    var intent = new IntentRecommendation()
                    {
                        Intent = string.Empty, Score = 1.0
                    };
                    var result = new LuisResult()
                    {
                        TopScoringIntent = intent
                    };
                    await DispatchToIntentHandler(context, item, intent, result);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceInformation("It is possible that the LUIS appid has changed.");
                Trace.TraceError(ex.Message);
                Trace.TraceError(ex.StackTrace);
            }
        }
Esempio n. 16
0
 public Task LuisAllTypes(
     ILuisModel model,
     IntentRecommendation intent,
     LuisResult result,
     [Entity("entityTypeA")] string entityA_as_String,
     [Entity("entityTypeA")] IEnumerable <string> entityA_as_IEnumerable_String,
     [Entity("entityTypeA")] IReadOnlyCollection <string> entityA_as_IReadOnlyCollection_String,
     [Entity("entityTypeA")] IReadOnlyList <string> entityA_as_IReadOnlyList_String,
     [Entity("entityTypeA")] EntityRecommendation entityA_as_EntityRecommendation,
     [Entity("entityTypeA")] IEnumerable <EntityRecommendation> entityA_as_IEnumerable_EntityRecommendation,
     [Entity("entityTypeA")] IReadOnlyCollection <EntityRecommendation> entityA_as_IReadOnlyCollection_EntityRecommendation,
     [Entity("entityTypeA")] IReadOnlyList <EntityRecommendation> entityA_as_IReadOnlyList_EntityRecommendation,
     [Entity("entityTypeB")] string entityB_as_String,
     [Entity("entityTypeB")] IEnumerable <string> entityB_as_IEnumerable_String,
     [Entity("entityTypeB")] IReadOnlyCollection <string> entityB_as_IReadOnlyCollection_String,
     [Entity("entityTypeB")] IReadOnlyList <string> entityB_as_IReadOnlyList_String,
     [Entity("entityTypeB")] EntityRecommendation entityB_as_EntityRecommendation,
     [Entity("entityTypeB")] IEnumerable <EntityRecommendation> entityB_as_IEnumerable_EntityRecommendation,
     [Entity("entityTypeB")] IReadOnlyCollection <EntityRecommendation> entityB_as_IReadOnlyCollection_EntityRecommendation,
     [Entity("entityTypeB")] IReadOnlyList <EntityRecommendation> entityB_as_IReadOnlyList_EntityRecommendation)
 {
     return(methods.LuisAllTypes(model, intent, result, entityA_as_String, entityA_as_IEnumerable_String, entityA_as_IReadOnlyCollection_String, entityA_as_IReadOnlyList_String, entityA_as_EntityRecommendation, entityA_as_IEnumerable_EntityRecommendation, entityA_as_IReadOnlyCollection_EntityRecommendation, entityA_as_IReadOnlyList_EntityRecommendation, entityB_as_String, entityB_as_IEnumerable_String, entityB_as_IReadOnlyCollection_String, entityB_as_IReadOnlyList_String, entityB_as_EntityRecommendation, entityB_as_IEnumerable_EntityRecommendation, entityB_as_IReadOnlyCollection_EntityRecommendation, entityB_as_IReadOnlyList_EntityRecommendation));
 }
Esempio n. 17
0
 protected override Task DispatchToIntentHandler(IDialogContext context, IAwaitable <IMessageActivity> item, IntentRecommendation bestIntent, LuisResult result)
 {
     Trace.WriteLine($"Intent: {bestIntent.Intent}, Entities: {string.Join(", ", result.Entities.Select(i => i.Type ?? i.Role))}");
     return(base.DispatchToIntentHandler(context, item, bestIntent, result));
 }
Esempio n. 18
0
        protected override Task DispatchToIntentHandler(IDialogContext context, IAwaitable <IMessageActivity> item, IntentRecommendation bestIntent, LuisResult result)
        {
            if (result.TopScoringIntent.Score.HasValue && result.TopScoringIntent.Score.Value < IntentThreshold)
            {
                return(None(context, result));
            }

            return(base.DispatchToIntentHandler(context, item, bestIntent, result));
        }
Esempio n. 19
0
        protected override async Task MessageReceived(IDialogContext context, IAwaitable <IMessageActivity> item)
        {
            var message     = await item;
            var messageText = await GetLuisQueryTextAsync(context, message);

            if (messageText == null)
            {
                var intent = new IntentRecommendation()
                {
                    Intent = string.Empty, Score = 1.0
                };
                var result = new LuisResult()
                {
                    TopScoringIntent = intent
                };
                await DispatchToIntentHandler(context, item, intent, result);

                return;
            }

            if (keyPhrases.ContainsKey(messageText.ToLowerInvariant()))
            {
                //TODO: Handle translation
                var intent = new IntentRecommendation()
                {
                    Intent = keyPhrases[messageText.ToLowerInvariant()], Score = 1.0
                };
                var result = new LuisResult()
                {
                    TopScoringIntent = intent
                };
                await DispatchToIntentHandler(context, item, intent, result);

                return;
            }

            // Modify request by the service to add attributes and then by the dialog to reflect the particular query
            var tasks = this.services.Select(s => s.QueryAsync(ModifyLuisRequest(s.ModifyRequest(new LuisRequest(messageText))), context.CancellationToken)).ToArray();

            try
            {
                var qnaTask = qnaService.QueryAsync(message.Text, context.CancellationToken);
                var results = Task.WhenAll(tasks);

                await Task.WhenAll(results, qnaTask);

                var qnaResult = qnaTask.Result;


                var winners = from result in results.Result.Select((value, index) => new { value, index })
                              let resultWinner = BestIntentFrom(result.value)
                                                 where resultWinner != null
                                                 select new LuisServiceResult(result.value, resultWinner, this.services[result.index]);

                var winner = this.BestResultFrom(winners);
                if (qnaResult.Score > QNA_THRESHOLD && qnaResult.Score > winner.BestIntent.Score)
                {
                    await QnaHandler(context, qnaResult);

                    return;
                }

                if (winner == null)
                {
                    throw new InvalidOperationException("No winning intent selected from Luis results.");
                }

                if (winner.Result.Dialog?.Status == DialogResponse.DialogStatus.Question)
                {
#pragma warning disable CS0618
                    var childDialog = await MakeLuisActionDialog(winner.LuisService,
                                                                 winner.Result.Dialog.ContextId,
                                                                 winner.Result.Dialog.Prompt);

#pragma warning restore CS0618
                    context.Call(childDialog, LuisActionDialogFinished);
                }
                else
                {
                    await DispatchToIntentHandler(context, item, winner.BestIntent, winner.Result);
                }
            }
            catch (Exception ex)
            { Console.Write(ex.Message); }
        }
Esempio n. 20
0
 public LuisServiceResult(LuisResult result, IntentRecommendation intent)
 {
     this.Result     = result;
     this.BestIntent = intent;
 }
Esempio n. 21
0
 protected override Task DispatchToIntentHandler(IDialogContext context, IAwaitable <IMessageActivity> item, IntentRecommendation bestIntent, LuisResult result)
 {
     return(base.DispatchToIntentHandler(context, item, bestIntent, result));
 }
 public static bool IsInRange(this IntentRecommendation result, double range = 0.7)
 {
     return(result.Score.Value > range);
 }
    protected override async Task DispatchToIntentHandler(IDialogContext context, IAwaitable <IMessageActivity> item, IntentRecommendation bestIntent, LuisResult result)
    {
        if (bestIntent.Intent == "FindFood" || bestIntent.Intent == "BuyFood")
        {
            if (result.Intents[0].Score - result.Intents[1].Score < 0.1)
            {
                bestIntent.Intent = "FindOrBuyFood";
                bestIntent.Score  = 1;
            }
        }

        await base.DispatchToIntentHandler(context, item, bestIntent, result);
    }
 protected override Task DispatchToIntentHandler(IDialogContext context, IAwaitable <IMessageActivity> item, IntentRecommendation bestIntent, LuisResult result)
 {
     using (var scope = Conversation.Container.BeginLifetimeScope())
     {
         var service = scope.Resolve <IBotFrameworkInstrumentation>();
         service.TrackLuisIntent(context.Activity, result);
     }
     return(base.DispatchToIntentHandler(context, item, bestIntent, result));
 }
Esempio n. 25
0
        private bool IsScoreTooLow(IDialogContext context, LuisResult result)
        {
            IntentRecommendation intent = result.Intents[0];

            return(intent.Score.HasValue && intent.Score.Value < MIN_ALLOWED_SCORE);
        }