package VladimirBot::ScheduleII; # This is supposed to parse and process complex scheduling stuff # # History / Notes # 2003.12.25 # - Added history use strict; use Carp; use Parse::Earley; use Data::Dumper; $Data::Dumper::Deparse = 1; # Here I push myself onto the module call stack push @VladimirBot::vladimir_module, 'ScheduleII'; sub parse { my $sentence = shift; my $grammar = <<'END'; # Sentences which must be parsed: # "What am I doing today?" # "What am I doing tomorrow?" # "What am I doing on Wednesday?" # "What am I doing on April 9th?" # "What's going on today?" # "What was I doing last week?" # "I have an appointment with Chris tomorrow." # "There is an AzEIP meeting tomorrow which I must attend." # "I am going to the concert on july 5th" # "I went to the concert today." # "When is that concert?" schedule: question_word subject schedule_verb timespan punctuation {{ query => 'schedule', %{$_[1]}, %{$_[3]} }} question_word: 'what' 'am' | 'what' 'are' subject: 'i' | 'me' | 'my' { return {who => 'brock'} } | 'you' {{who => 'vladimir'}} schedule_verb: 'doing' 'on' | 'doing' nothing: punctuation: '.' | '?' | nothing timespan: timespan_day { $_[0] } timespan_day: /today|tomorrow|sunday|monday|tuesday|wednesday|thursday|friday|saturday|((\s|\w|,)+)/ {{ start_time => "$_[0] at 00:00:00", end_time => "$_[0] at 23:59:59" }} END my $parser = new Parse::Earley; $parser->grammar($grammar); $parser->start('schedule'); do { $parser->advance($sentence) } until( $parser->fails($sentence, 'schedule') or $parser->matches($sentence, 'schedule')); if ($parser->matches($sentence, 'schedule')) { my $out; my $graph; ($graph) = $parser->matches($sentence, 'schedule'); my $e = $parser->eval_production($graph); $out = "result: " . Dumper($e) . "\n"; return $out; } return 0; # Nope, we couldn't figure it out. } 1;