示例#1
0
        public static async Task MainAsync()
        {
            ITextProcessor textProcessor;

            Console.WriteLine("Select the sample: ");
            Console.WriteLine("1. Calculator");
            Console.WriteLine("2. Calendar");

            switch (Console.ReadLine() ?? "".Trim())
            {
            case "2":
                Console.WriteLine("Starting the calendar...");
                textProcessor = Calendar2.CreateTextProcessor();
                break;

            default:
                Console.WriteLine("Starting the calculator...");
                textProcessor = Calculator.CreateTextProcessor();
                break;
            }

            Console.Clear();

            // Creates an empty context
            var context = new RequestContext();

            string inputText;

            do
            {
                Console.WriteLine();
                Console.Write("> ");
                inputText = Console.ReadLine();

                var sw = Stopwatch.StartNew();

                try
                {
                    await textProcessor.ProcessAsync(inputText, context, CancellationToken.None);
                }
                catch (MatchNotFoundException)
                {
                    Console.WriteLine("There's no match for the specified input");
                }
                catch (ArgumentException)
                {
                    break;
                }

                sw.Stop();

#if DEBUG
                Console.WriteLine("Elapsed: {0} ms ({1} ticks)", sw.ElapsedMilliseconds, sw.ElapsedTicks);
#endif
            } while (!string.IsNullOrWhiteSpace(inputText));
        }
示例#2
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);
        }