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 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); }
public void Test1() { var context = new RequestContext(); // Define a output processor that prints the command results to the console var outputProcessor = new DelegateOutputProcessor <int>( (o, ctx) => Console.WriteLine($"Result: {o}") ); var textProcessor = Calculator.CreateTextProcessor(outputProcessor); string inputText = "sum 5 3"; try { var task = textProcessor.ProcessAsync(inputText, context, CancellationToken.None); task.Wait(); } catch (MatchNotFoundException) { throw new InvalidOperationException("There's no match for the specified input"); } }
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; }
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; }