From c240cdc93602ddc11e210fddbf4d00f19f33c965 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Sun, 28 Jun 2009 22:48:17 -0400 Subject: [PATCH] - Add Horde_Date_Parser_Result for holding complex results from Horde_Date_Parser#parse() - Replace the 'guess' option with a generic 'return' option that allows you to pick a Horde_Date, Horde_Date_Span, or Horde_Date_Parser_Result - Move the guess() function to the Horde_Date_Parser_Result object (maybe it should actually be on the span itself?) - Horde_Date_Parser_Result can return the tagged or untagged text from the original string. --- .../lib/Horde/Date/Parser/Locale/Base.php | 57 +++++++++------------- .../Date_Parser/lib/Horde/Date/Parser/Result.php | 41 ++++++++++++++++ framework/Date_Parser/package.xml | 2 + .../test/Horde/Date/Parser/Locale/BaseTest.php | 10 ++-- .../test/Horde/Date/Parser/ParserTest.php | 14 ------ .../test/Horde/Date/Parser/ResultTest.php | 29 +++++++++++ 6 files changed, 101 insertions(+), 52 deletions(-) create mode 100644 framework/Date_Parser/lib/Horde/Date/Parser/Result.php create mode 100644 framework/Date_Parser/test/Horde/Date/Parser/ResultTest.php diff --git a/framework/Date_Parser/lib/Horde/Date/Parser/Locale/Base.php b/framework/Date_Parser/lib/Horde/Date/Parser/Locale/Base.php index 6ee13e021..45ba8ad8a 100644 --- a/framework/Date_Parser/lib/Horde/Date/Parser/Locale/Base.php +++ b/framework/Date_Parser/lib/Horde/Date/Parser/Locale/Base.php @@ -14,8 +14,8 @@ class Horde_Date_Parser_Locale_Base /** # Parses a string containing a natural language date or time. If the parser - # can find a date or time, either a Time or Chronic::Span will be returned - # (depending on the value of :guess). If no date or time can be found, + # can find a date or time, either a Horde_Date or Horde_Date_Span will be returned + # (depending on the value of :return). If no date or time can be found, # +nil+ will be returned. # # Options are: @@ -28,17 +28,20 @@ class Horde_Date_Parser_Locale_Base # past. Specify :future or omit to set a future context. # # [:now] - # Time (defaults to Time.now) + # Time (defaults to time()) # - # By setting :now to a Time, all computations will be based off - # of that time instead of Time.now + # By setting :now to a Horde_Date, all computations will be based off + # of that time instead of time(). # - # [:guess] - # +true+ or +false+ (defaults to +true+) + # [:return] + # 'result', 'span', or 'date' (defaults to 'date') # # By default, the parser will guess a single point in time for the # given date or time. If you'd rather have the entire time span returned, - # set :guess to +false+ and a Chronic::Span will be returned. + # set :return to 'span' and a Horde_Date_Span will be returned. + # If you want the entire result, including tokens (for retrieving the text + # that was or was not tagged, for example), set :return to 'result' + # and you will get a result object. # # [:ambiguousTimeRange] # Integer or :none (defaults to 6 (6am-6pm)) @@ -57,7 +60,7 @@ class Horde_Date_Parser_Locale_Base $defaultOptions = array( 'context' => 'future', 'now' => new Horde_Date(time()), - 'guess' => true, + 'return' => 'date', 'ambiguousTimeRange' => 6, ); $options = array_merge($defaultOptions, $this->args, $specifiedOptions); @@ -94,16 +97,20 @@ class Horde_Date_Parser_Locale_Base } // strip any non-tagged tokens - $tokens = array_values(array_filter($tokens, create_function('$t', 'return $t->tagged();'))); + $taggedTokens = array_values(array_filter($tokens, create_function('$t', 'return $t->tagged();'))); // do the heavy lifting - $span = $this->tokensToSpan($tokens, $options); - - // guess a time within a span if required - if ($options['guess']) { - return $this->guess($span); - } else { - return $span; + $span = $this->tokensToSpan($taggedTokens, $options); + + // generate the result and return it, the span, or a guessed time within the span + $result = new Horde_Date_Parser_Result($span, $tokens); + switch ($options['return']) { + case 'result': + return $result; + case 'span': + return $result->span; + case 'date': + return $result->guess(); } } @@ -186,22 +193,6 @@ class Horde_Date_Parser_Locale_Base return array_map(create_function('$w', 'return new Horde_Date_Parser_Token($w);'), preg_split('/\s+/', $text)); } - /** - * Guess a specific time within the given span - */ - public function guess($span) - { - if (! $span instanceof Horde_Date_Span) { - return null; - } - - if ($span->width() > 1) { - return $span->begin->add($span->width() / 2); - } else { - return $span->begin; - } - } - public function initDefinitions() { if ($this->definitions) { return; } diff --git a/framework/Date_Parser/lib/Horde/Date/Parser/Result.php b/framework/Date_Parser/lib/Horde/Date/Parser/Result.php new file mode 100644 index 000000000..415b77cfd --- /dev/null +++ b/framework/Date_Parser/lib/Horde/Date/Parser/Result.php @@ -0,0 +1,41 @@ +span = $span; + $this->tokens = $tokens; + } + + /** + * Guess a specific time within the given span + */ + public function guess() + { + if (! $this->span instanceof Horde_Date_Span) { + return null; + } + + if ($this->span->width() > 1) { + return $this->span->begin->add($this->span->width() / 2); + } else { + return $this->span->begin; + } + } + + public function taggedText() + { + $taggedTokens = array_values(array_filter($this->tokens, create_function('$t', 'return $t->tagged();'))); + return implode(' ', array_map(create_function('$t', 'return $t->word;'), $taggedTokens)); + } + + public function untaggedText() + { + $untaggedTokens = array_values(array_filter($this->tokens, create_function('$t', 'return ! $t->tagged();'))); + return implode(' ', array_map(create_function('$t', 'return $t->word;'), $untaggedTokens)); + } + +} diff --git a/framework/Date_Parser/package.xml b/framework/Date_Parser/package.xml index 7489d7b19..7c5ece3e7 100644 --- a/framework/Date_Parser/package.xml +++ b/framework/Date_Parser/package.xml @@ -57,6 +57,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -95,6 +96,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + diff --git a/framework/Date_Parser/test/Horde/Date/Parser/Locale/BaseTest.php b/framework/Date_Parser/test/Horde/Date/Parser/Locale/BaseTest.php index 5c2d33ac9..b146f95e4 100644 --- a/framework/Date_Parser/test/Horde/Date/Parser/Locale/BaseTest.php +++ b/framework/Date_Parser/test/Horde/Date/Parser/Locale/BaseTest.php @@ -335,7 +335,7 @@ class Horde_Date_Parser_Locale_BaseTest extends Horde_Test_Case $time = $this->parser->parse("this year"); $this->assertEquals(new Horde_Date(2006, 10, 24, 12, 30), $time); - $span = $this->parser->parse("this year", array('context' => 'past', 'guess' => false)); + $span = $this->parser->parse("this year", array('context' => 'past', 'return' => 'span')); $this->assertEquals(new Horde_Date_Span('2006-01-01 00:00:00', '2006-08-16 00:00:00'), $span); } @@ -657,7 +657,7 @@ class Horde_Date_Parser_Locale_BaseTest extends Horde_Test_Case public function testParseGuess_o_r_g_r() { - $span = $this->parser->parse("3rd month next year", array('guess' => false)); + $span = $this->parser->parse("3rd month next year", array('return' => 'span')); $this->assertEquals(new Horde_Date_Span('2007-03-01 00:00:00', '2007-04-01 00:00:00'), $span); $time = $this->parser->parse("3rd thursday this september"); @@ -675,15 +675,15 @@ class Horde_Date_Parser_Locale_BaseTest extends Horde_Test_Case public function testParseSpan() { - $span = $this->parser->parse('friday', array('guess' => false)); + $span = $this->parser->parse('friday', array('return' => 'span')); $this->assertEquals(new Horde_Date(2006, 8, 18), $span->begin); $this->assertEquals(new Horde_Date(2006, 8, 19), $span->end); - $span = $this->parser->parse('november', array('guess' => false)); + $span = $this->parser->parse('november', array('return' => 'span')); $this->assertEquals(new Horde_Date(2006, 11, 1), $span->begin); $this->assertEquals(new Horde_Date(2006, 12, 1), $span->end); - $span = $this->parser->parse('weekend', array('guess' => false)); + $span = $this->parser->parse('weekend', array('return' => 'span')); $this->assertEquals(new Horde_Date(2006, 8, 19), $span->begin); $this->assertEquals(new Horde_Date(2006, 8, 21), $span->end); } diff --git a/framework/Date_Parser/test/Horde/Date/Parser/ParserTest.php b/framework/Date_Parser/test/Horde/Date/Parser/ParserTest.php index ad30392e2..823aefbae 100644 --- a/framework/Date_Parser/test/Horde/Date/Parser/ParserTest.php +++ b/framework/Date_Parser/test/Horde/Date/Parser/ParserTest.php @@ -47,18 +47,4 @@ class Horde_Date_Parser_ParserTest extends Horde_Test_Case $this->assertEquals(2, count($tokens)); } - public function testGuess() - { - $parser = Horde_Date_Parser::factory(); - - $span = new Horde_Date_Span(new Horde_Date('2006-08-16 00:00:00'), new Horde_Date('2006-08-17 00:00:00')); - $this->assertEquals(new Horde_Date('2006-08-16 12:00:00'), $parser->guess($span)); - - $span = new Horde_Date_Span(new Horde_Date('2006-08-16 00:00:00'), new Horde_Date('2006-08-17 00:00:01')); - $this->assertEquals(new Horde_Date('2006-08-16 12:00:00'), $parser->guess($span)); - - $span = new Horde_Date_Span(new Horde_Date('2006-11-01 00:00:00'), new Horde_Date('2006-12-01 00:00:00')); - $this->assertEquals(new Horde_Date('2006-11-16 00:00:00'), $parser->guess($span)); - } - } diff --git a/framework/Date_Parser/test/Horde/Date/Parser/ResultTest.php b/framework/Date_Parser/test/Horde/Date/Parser/ResultTest.php new file mode 100644 index 000000000..59589a782 --- /dev/null +++ b/framework/Date_Parser/test/Horde/Date/Parser/ResultTest.php @@ -0,0 +1,29 @@ +span = new Horde_Date_Span(new Horde_Date('2006-08-16 00:00:00'), new Horde_Date('2006-08-17 00:00:00')); + $this->assertEquals(new Horde_Date('2006-08-16 12:00:00'), $result->guess($span)); + + $result->span = new Horde_Date_Span(new Horde_Date('2006-08-16 00:00:00'), new Horde_Date('2006-08-17 00:00:01')); + $this->assertEquals(new Horde_Date('2006-08-16 12:00:00'), $result->guess($span)); + + $result->span = new Horde_Date_Span(new Horde_Date('2006-11-01 00:00:00'), new Horde_Date('2006-12-01 00:00:00')); + $this->assertEquals(new Horde_Date('2006-11-16 00:00:00'), $result->guess($span)); + } + +} -- 2.11.0