Beispiel #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);
        }
        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);
        }
Beispiel #3
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);
        }
Beispiel #4
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();
            textProcessor.CommandProcessors.Add(sumCommandProcessor);
            textProcessor.CommandProcessors.Add(subtractCommandProcessor);
            textProcessor.CommandProcessors.Add(multiplyCommandProcessor);
            textProcessor.CommandProcessors.Add(divideCommandProcessor);

            return textProcessor;
        }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
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;
        }