Exemplo n.º 1
0
        public static ITextProcessor CreateTextProcessor(DelegateOutputProcessor <int> outputProcessor)
        {
            // Fist, define the calculator methods
            Func <int, int, Task <int> > sumFunc = (a, b) => Task.FromResult(a + b);

            // After that, the syntaxes for all operations, using the CSDL parser:
            // 1. Sum:
            // a) The default syntax, for inputs like 'sum 1 and 2' or 'sum 3 4'
            var sumSyntax = CsdlParser.Parse("operation+:VWord(sum) a:Integer :Word?(and) b:Integer");
            // b) The alternative syntax, for inputs like '3 plus 4'
            var alternativeSumSyntax = CsdlParser.Parse("a:Integer :Word(plus,more) b:Integer");

            // Now create the command processors, to bind the methods to the syntaxes
            var sumCommandProcessor = new DelegateCommandProcessor(
                sumFunc,
                true,
                outputProcessor,
                sumSyntax,
                alternativeSumSyntax
                );

            // Finally, create the text processor and register all command processors
            var textProcessor = new TextProcessor(new PunctuationTextSplitter());

            textProcessor.CommandProcessors.Add(sumCommandProcessor);

            textProcessor.TextPreprocessors.Add(new TrimTextPreprocessor());

            return(textProcessor);
        }
Exemplo n.º 2
0
        public static ITextProcessor CreateTextProcessor()
        {
            // The parsed syntaxes
            var confirmOrderSyntax1 = CsdlParser.Parse(
                ":LDWord?(quero,mande,solicito) :Word?(uma) :Word?(pizza) :Word?(do,no) :LDWord?(tamanho) size:LDWord(pequena,media,média,grande,gigante) :Word?(sabor,de) flavor:LDWord(marguerita,pepperoni,calabreza) :Word?(para) :Word?(à,a,o) address:Text");

            var confirmOrderSyntax2 = CsdlParser.Parse(
                ":LDWord?(quero,mande,solicito) :Word?(uma) :Word?(pizza) :Word?(sabor,de) flavor:LDWord(marguerita,pepperoni,calabreza) :Word?(do,no) :LDWord?(tamanho) size:LDWord(pequena,media,média,grande,gigante) :Word?(para) :Word?(à,a,o) address:Text");

            var processOrderSyntax = CsdlParser.Parse(
                ":Word(sim) orderId:Long");
            var cancelOrderSyntax = CsdlParser.Parse(
                ":Word(nao,não) orderId:Long");

            // The output processor handles the command method return value
            var addReminderOutputProcessor = new DelegateOutputProcessor <string>(
                (text, context) => Console.WriteLine(text));

            var pizza = new Pizza();
            var confirmOrderCommandProcessor = new ReflectionCommandProcessor(
                pizza,
                nameof(ConfirmOrderAsync),
                true,
                addReminderOutputProcessor,
                confirmOrderSyntax1,
                confirmOrderSyntax2);
            var processOrderCommandProcessor2 = new ReflectionCommandProcessor(
                pizza,
                nameof(ProcessOrderAsync),
                true,
                addReminderOutputProcessor,
                processOrderSyntax);
            var cancelOrderCommandProcessor = new ReflectionCommandProcessor(
                pizza,
                nameof(CancelOrderAsync),
                true,
                addReminderOutputProcessor,
                cancelOrderSyntax);

            // Register the the processor
            var textProcessor = new TextProcessor(new PunctuationTextSplitter());

            textProcessor.CommandProcessors.Add(confirmOrderCommandProcessor);
            textProcessor.CommandProcessors.Add(processOrderCommandProcessor2);
            textProcessor.CommandProcessors.Add(cancelOrderCommandProcessor);

            // Add some preprocessors to normalize the input text
            textProcessor.TextPreprocessors.Add(new TextNormalizerPreprocessor());
            textProcessor.TextPreprocessors.Add(new ToLowerCasePreprocessor());

            return(textProcessor);
        }
Exemplo n.º 3
0
        public void AddProcessor(string syntaxPattern, Delegate fn = null)
        {
            var syntax = CsdlParser.Parse(syntaxPattern);

            fn = fn ?? (Func <Task <string> >)(() => Task.FromResult(string.Empty));
            // Now create the command processors, to bind the methods to the syntaxes
            var cmd_proc = new DelegateCommandProcessor(
                fn,
                true,
                this,
                syntax
                );

            CommandProcessors.Add(cmd_proc);
        }
Exemplo n.º 4
0
        public static ITextProcessor CreateTextProcessor()
        {
            // The parsed syntaxes
            var syntax1 = CsdlParser.Parse(
                ":Word(lembrar) :Word?(de) reminder:Text");
            var syntax2 = CsdlParser.Parse(
                ":Word(lembre) :Word?(me) date:Word?(hoje,amanha,eventualmente) :Word?(de) reminder:Text");
            var syntax3 = CsdlParser.Parse(
                ":Word?(me) :Word(lembre) :Word~(de) reminder:Text date:Word?(hoje,amanha,eventualmente) :Word?(a) time:Word?(manha,tarde,noite)");

            // The output processor handles the command method return value
            var addReminderOutputProcessor = new DelegateOutputProcessor <string>(
                (text, context) => Console.WriteLine(text));

            var calendar          = new Calendar2();
            var commandProcessor1 = new ReflectionCommandProcessor(
                calendar,
                nameof(AddReminderAsync),
                true,
                addReminderOutputProcessor,
                syntax1);
            var commandProcessor2 = new ReflectionCommandProcessor(
                calendar,
                nameof(AddReminderForDateAsync),
                true,
                addReminderOutputProcessor,
                syntax2);
            var commandProcessor3 = new ReflectionCommandProcessor(
                calendar,
                nameof(AddReminderForDateAndTimeAsync),
                true,
                addReminderOutputProcessor,
                syntax3);

            // Register the the processor
            var textProcessor = new TextProcessor();

            textProcessor.CommandProcessors.Add(commandProcessor1);
            textProcessor.CommandProcessors.Add(commandProcessor2);
            textProcessor.CommandProcessors.Add(commandProcessor3);

            // Add some preprocessors to normalize the input text
            textProcessor.TextPreprocessors.Add(new TextNormalizerPreprocessor());
            textProcessor.TextPreprocessors.Add(new ToLowerCasePreprocessor());

            return(textProcessor);
        }
Exemplo n.º 5
0
        SlidingTextProcessor CreateTextProcessor <TRes>(string syntaxPattern, Delegate fn)
        {
            var out_proc = new DelegateOutputProcessor <TRes>(
                (o, ctx) =>
                Log($"Result: {o}; op: {ctx.GetVariable("operation")}"));
            var syntax = CsdlParser.Parse(syntaxPattern);
            // Now create the command processors, to bind the methods to the syntaxes
            var cmd_proc = new DelegateCommandProcessor(
                fn,
                true,
                out_proc,
                syntax
                );
            // Finally, create the text processor and register all command processors
            //var text_proc = new TextProcessor(new PunctuationTextSplitter());
            var text_proc = new SlidingTextProcessor();

            text_proc.CommandProcessors.Add(cmd_proc);
            text_proc.TextPreprocessors.Add(new TrimTextPreprocessor());

            return(text_proc);
        }
 /// <summary>
 /// Adds a new command syntax to the <see cref="TextcMessageReceiver"/> builder.
 /// </summary>
 /// <param name="culture">The syntax culture.</param>
 /// <param name="syntaxPattern">The CSDL statement. Please refer to <seealso cref="https://github.com/takenet/textc-csharp#csdl"/> about the notation.</param>
 /// <returns></returns>
 public SyntaxTextcMessageReceiverBuilder ForSyntax(CultureInfo culture, string syntaxPattern) =>
 ForSyntax(CsdlParser.Parse(syntaxPattern, culture));
 /// <summary>
 /// Adds a new command syntax to the <see cref="TextcMessageReceiver"/> builder.
 /// </summary>
 /// <param name="syntaxPattern">The CSDL statement. Please refer to <seealso cref="https://github.com/takenet/textc-csharp#csdl"/> about the notation.</param>
 /// <returns></returns>
 public SyntaxTextcMessageReceiverBuilder ForSyntax(string syntaxPattern) =>
 ForSyntax(CsdlParser.Parse(syntaxPattern));
 /// <summary>
 /// Adds multiple command syntaxes to the <see cref="TextcMessageReceiver"/> builder.
 /// The added syntaxes should be related and will be associated to a same command processor.
 /// </summary>
 /// <param name="culture">The syntaxes culture.</param>
 /// <param name="syntaxPatterns">The CSDL statements. Please refer to <seealso cref="https://github.com/takenet/textc-csharp#csdl"/> about the notation.</param>
 /// <returns></returns>
 public SyntaxTextcMessageReceiverBuilder ForSyntaxes(CultureInfo culture, params string[] syntaxPatterns) =>
 new SyntaxTextcMessageReceiverBuilder(_commandProcessorFactories, syntaxPatterns.Select(s => CsdlParser.Parse(s, culture)).ToList(), this);
Exemplo n.º 9
0
        public static ITextProcessor CreateTextProcessor()
        {
            // 1. Define the calendar syntaxes, using some LDWords for input flexibility
            var addReminderSyntax = CsdlParser.Parse(
                "^[:Word?(hey,ok) :LDWord?(calendar,agenda) :Word?(add,new,create) command:LDWord(remind,reminder) :Word?(me) :Word~(to,of) message:Text :Word?(for) when:LDWord?(today,tomorrow,someday)]");
            var partialAddReminderSyntax = CsdlParser.Parse(
                "^[:Word?(hey,ok) :LDWord?(calendar,agenda) :Word?(add,new,create) command+:LDWord(remind,reminder) :Word?(for,me) when+:LDWord?(today,tomorrow,someday)]");
            var getRemindersSyntax = CsdlParser.Parse(
                "[when:LDWord?(today,tomorrow,someday) :LDWord(reminders)]");

            // 2. Now the output processors
            var addReminderOutputProcessor = new DelegateOutputProcessor <Reminder>((reminder, context) =>
            {
                Console.WriteLine($"Reminder '{reminder.Message}' added successfully for '{reminder.When}'");
            });
            var getRemindersOutputProcessor = new DelegateOutputProcessor <IEnumerable <Reminder> >((reminders, context) =>
            {
                var remindersDictionary = reminders
                                          .GroupBy(r => r.When)
                                          .ToDictionary(r => r.Key, r => r.Select(reminder => reminder.Message));

                foreach (var when in remindersDictionary.Keys)
                {
                    Console.WriteLine($"Reminders for {when}:");

                    foreach (var reminderMessage in remindersDictionary[when])
                    {
                        Console.WriteLine($"* {reminderMessage}");
                    }

                    Console.WriteLine();
                }
            });

            // 3. Create a instance of the processor object to be shared by all processors
            var calendar = new Calendar();

            // 4. Create the command processors
            var addRemiderCommandProcessor = new ReflectionCommandProcessor(
                calendar,
                nameof(AddReminderAsync),
                true,
                addReminderOutputProcessor,
                addReminderSyntax);

            var partialAddRemiderCommandProcessor = new DelegateCommandProcessor(
                new Func <string, Task>((when) =>
            {
                Console.Write($"What do you want to be reminded {when}?");
                return(Task.FromResult(0));
            }),
                syntaxes: partialAddReminderSyntax);

            var getRemidersCommandProcessor = new ReflectionCommandProcessor(
                calendar,
                nameof(GetRemindersAsync),
                true,
                getRemindersOutputProcessor,
                getRemindersSyntax);


            // 5. Register the the processors
            var textProcessor = new TextProcessor();

            textProcessor.CommandProcessors.Add(addRemiderCommandProcessor);
            textProcessor.CommandProcessors.Add(partialAddRemiderCommandProcessor);
            textProcessor.CommandProcessors.Add(getRemidersCommandProcessor);

            // 6. Add some preprocessors to normalize the input text
            textProcessor.TextPreprocessors.Add(new TextNormalizerPreprocessor());
            textProcessor.TextPreprocessors.Add(new ToLowerCasePreprocessor());

            return(textProcessor);
        }
Exemplo n.º 10
0
        public static ITextProcessor CreateTextProcessor()
        {
            // Fist, define the calculator methods
            Func <int, int, Task <int> > sumFunc      = (a, b) => Task.FromResult(a + b);
            Func <int, int, Task <int> > subtractFunc = (a, b) => Task.FromResult(a - b);
            Func <int, int, Task <int> > multiplyFunc = (a, b) => Task.FromResult(a * b);
            Func <int, int, Task <int> > divideFunc   = (a, b) => Task.FromResult(a / b);

            // After that, the syntaxes for all operations, using the CSDL parser:

            // 1. Sum:
            // a) The default syntax, for inputs like 'sum 1 and 2' or 'sum 3 4'
            var sumSyntax = CsdlParser.Parse("operation+:Word(sum) a:Integer :Word?(and) b:Integer");
            // b) The alternative syntax, for inputs like '3 plus 4'
            var alternativeSumSyntax = CsdlParser.Parse("a:Integer :Word(plus,more) b:Integer");

            // 2. Subtract:
            // a) The default syntax, for inputs like 'subtract 2 from 3'
            var subtractSyntax = CsdlParser.Parse("operation+:Word(subtract,sub) b:Integer :Word(from) a:Integer");
            // b) The alternative syntax, for inputs like '5 minus 3'
            var alternativeSubtractSyntax = CsdlParser.Parse("a:Integer :Word(minus) b:Integer");

            // 3. Multiply:
            // a) The default syntax, for inputs like 'multiply 3 and 3' or 'multiply 5 2'
            var multiplySyntax = CsdlParser.Parse("operation+:Word(multiply,mul) a:Integer :Word?(and,by) b:Integer");
            // b) The alternative syntax, for inputs like '6 times 2'
            var alternativeMultiplySyntax = CsdlParser.Parse("a:Integer :Word(times) b:Integer");

            // 4. Divide:
            // a) The default syntax, for inputs like 'divide 3 by 3' or 'divide 10 2'
            var divideSyntax = CsdlParser.Parse("operation+:Word(divide,div) a:Integer :Word?(by) b:Integer");
            // b) The alternative syntax, for inputs like '6 by 2'
            var alternativeDivideSyntax = CsdlParser.Parse("a:Integer :Word(by) b:Integer");

            // Define a output processor that prints the command results to the console
            var outputProcessor = new DelegateOutputProcessor <int>((o, context) => Console.WriteLine($"Result: {o}"));

            // Now create the command processors, to bind the methods to the syntaxes
            var sumCommandProcessor = new DelegateCommandProcessor(
                sumFunc,
                true,
                outputProcessor,
                sumSyntax,
                alternativeSumSyntax
                );
            var subtractCommandProcessor = new DelegateCommandProcessor(
                subtractFunc,
                true,
                outputProcessor,
                subtractSyntax,
                alternativeSubtractSyntax
                );
            var multiplyCommandProcessor = new DelegateCommandProcessor(
                multiplyFunc,
                true,
                outputProcessor,
                multiplySyntax,
                alternativeMultiplySyntax
                );
            var divideCommandProcessor = new DelegateCommandProcessor(
                divideFunc,
                true,
                outputProcessor,
                divideSyntax,
                alternativeDivideSyntax
                );

            // Finally, create the text processor and register all command processors
            var textProcessor = new TextProcessor(new PunctuationTextSplitter());

            textProcessor.CommandProcessors.Add(sumCommandProcessor);
            textProcessor.CommandProcessors.Add(subtractCommandProcessor);
            textProcessor.CommandProcessors.Add(multiplyCommandProcessor);
            textProcessor.CommandProcessors.Add(divideCommandProcessor);

            textProcessor.TextPreprocessors.Add(new TrimTextPreprocessor());

            return(textProcessor);
        }