コード例 #1
0
        /// <summary>
        /// This method evaluates a cond special form. A cond must have at least one
        /// clause. This method evaluates each clause one by one, sequentially, and if the
        /// predicate in a clause evaluates to #t, then corresponding expressions are evaluated
        /// sequentially and the result of the last of the expressions gets returned.
        /// </summary>
        private Bounce ApplyCondSpecialForm(IExpression expression, Environment env, Cont cont)
        {
            var condSpecialForm = expression as ScmCondSpecialForm;
            // Since we write everything in CPS style, we define this helper closure, which will
            // evaluate clauses recursively, one by one.
            ClauseHandler ClauseEvaluator = null;

            ClauseEvaluator = (clauses, _env, _cont) =>
            {
                // We return unassigned value if there is nothing to evaluate.
                if (clauses.IsEmpty())
                {
                    return((Bounce)_cont(ScmUnassigned.Instance));
                }
                // We get a clause and check whether it is not empty.
                var clause = clauses.Head();
                if (clause.IsEmpty)
                {
                    throw new EvaluatorException(UserMessages.ClauseCannotBeEmptyList, condSpecialForm);
                }
                // Now we evaluate the predicate.
                return(Eval(clause.Expressions.First(), _env, evalResult =>
                {
                    // If it evaluates to #t, or returns any other object that is not
                    // #f, then we evaluate corresponding expressions.
                    if (evalResult is ScmTrueValue || !(evalResult is IBoolean))
                    {
                        // Since the predicate evaluated to #t, we evaluate expressions then.
                        // NOTE: we might need to evaluate the last expression separately.
                        return EvalList(clause.Expressions.Tail(), _env, evalResults =>
                        {
                            var evalResultsAsList = (List <IExpression>)evalResults;
                            // If there was nothing to evaluate, return the value of
                            // the predicate.
                            if (evalResultsAsList.IsEmpty())
                            {
                                return (Bounce)_cont(evalResult);
                            }
                            // Otherwise, return the result of the last expression.
                            return (Bounce)_cont(evalResultsAsList.Last());
                        });
                    }
                    // Predicate in the clause did not evaluate to #t, so process with
                    // the next clause.
                    return ClauseEvaluator(clauses.Tail(), _env, _cont);
                }));
            };
            // Evaluate clauses one by one in a recursive manner.
            return(ClauseEvaluator(condSpecialForm.Clauses, env, cont));
        }
コード例 #2
0
ファイル: EditQueryDialog.cs プロジェクト: Mmate99/Bot_2.0
        private async Task <DialogTurnResult> ReplaceClauseAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var luisResult = await _luisRecognizer.RecognizeAsync <GeneralStatemenHandler>(stepContext.Context, cancellationToken);

            Dictionary <LuisClause, string> nc = new Dictionary <LuisClause, string>();

            ClauseHandler ch = new ClauseHandler(luisResult, nc);

            LuisClause newClause = nc.Last().Key;

            selectedQuery.getClauses()[selectedClauseID] = newClause;

            return(await stepContext.NextAsync());
        }
コード例 #3
0
        private async Task <DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            // Call LUIS and gather any potential booking details. (Note the TurnContext has the response to the prompt.)
            var luisResult = await _luisRecognizer.RecognizeAsync <GeneralStatemenHandler>(stepContext.Context, cancellationToken);

            switch (luisResult.TopIntent().intent)
            {
            case GeneralStatemenHandler.Intent.Statement:

                if (luisResult.Entities.Value[0] != null)
                {
                    ClauseHandler ch = new ClauseHandler(luisResult, clauses);

                    List <IData> list = database.getData();
                    List <IData> res  = list;

                    List <IData> foundItems = list.FindAll(findData);
                    res = res.Intersect(foundItems).ToList();
                    await handleResultsAsync(res, stepContext);

                    queryText = luisResult.Text;
                }

                break;

            case GeneralStatemenHandler.Intent.QueryManagment:
                string txt = "The queries so far:";
                await stepContext.Context.SendActivityAsync(MessageFactory.Text(txt));

                foreach (var q in queries)
                {
                    string queryId      = "Query ID: " + q.Key.ToString() + Environment.NewLine;
                    string queryText    = q.Value.getText() + Environment.NewLine;
                    string queryClauses = "";

                    foreach (var cl in q.Value.getClauses())
                    {
                        queryClauses += "Clause ID: " + cl.Key.ToString() + " searchkey: " + cl.Value.SearchKey;
                        queryClauses += cl.Value.Negated != false ? ", not equals" : "";
                        queryClauses += cl.Value.Smaller != false ? ", smaller than" : "";
                        queryClauses += cl.Value.Bigger != false ? ", bigger than" : "";
                        queryClauses += ", value: " + cl.Value.Value + Environment.NewLine;
                    }

                    var msg = MessageFactory.Text(queryId + queryText + queryClauses);
                    await stepContext.Context.SendActivityAsync(msg);
                }

                return(await stepContext.BeginDialogAsync(nameof(EditQueryDialog), null, cancellationToken));

            default:
                // Catch all for unhandled intents
                var didntUnderstandMessageText = $"Sorry, I didn't get that. Please try asking in a different way (intent was {luisResult.TopIntent().intent})";
                var didntUnderstandMessage     = MessageFactory.Text(didntUnderstandMessageText, didntUnderstandMessageText, InputHints.IgnoringInput);
                await stepContext.Context.SendActivityAsync(didntUnderstandMessage, cancellationToken);

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

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