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 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); }
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); }