From: Jan Schneider Date: Sat, 2 Oct 2010 21:21:45 +0000 (+0200) Subject: Fix ambiguity with the meaning of the German wordn 'morgen'. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=79292a06b0398e45ef7e7a7f2a86853e363d9be8;p=horde.git Fix ambiguity with the meaning of the German wordn 'morgen'. --- diff --git a/framework/Date_Parser/lib/Horde/Date/Parser/Locale/De.php b/framework/Date_Parser/lib/Horde/Date/Parser/Locale/De.php index e7e94679e..5a6baa1a6 100644 --- a/framework/Date_Parser/lib/Horde/Date/Parser/Locale/De.php +++ b/framework/Date_Parser/lib/Horde/Date/Parser/Locale/De.php @@ -3,4 +3,62 @@ */ class Horde_Date_Parser_Locale_De extends Horde_Date_Parser_Locale_Base { + /** + * Clean up the specified input text by stripping unwanted characters, + * converting idioms to their canonical form, converting number words + * to numbers (three => 3), and converting ordinal words to numeric + * ordinals (third => 3rd) + */ + public function preNormalize($text) + { + $text = strtolower($text); + $text = $this->numericizeNumbers($text); + $text = preg_replace('/[\'"\.]/', '', $text); + $text = preg_replace('/([\/\-\,\@])/', ' \1 ', $text); + $text = preg_replace('/\bheute\b/', 'dieser tag', $text); + $text = preg_replace('/\bmorgen\b/', 'nächster tag', $text); + $text = preg_replace('/\bgestern\b/', 'letzter tag', $text); + $text = preg_replace('/\bmittags?\b/', '12:00', $text); + $text = preg_replace('/\bmitternachts?\b/', '24:00', $text); + $text = preg_replace('/\bjetzt\b/', 'diese sekunde', $text); + $text = preg_replace('/\b(vor|früher)\b/', 'past', $text); + $text = preg_replace('/\b(?:in|during) the (morning)\b/', '\1', $text); + $text = preg_replace('/\bam (morgen|nachmittag|abend)\b/', '\1', $text); + $text = preg_replace('/\in der nacht\b/', 'nachts', $text); + $text = $this->numericizeOrdinals($text); + + return $text; + } + + /** + * Remove tokens that don't fit our definitions and re-orders tokens when + * necessary. + * + * @param array $tokens Array of tagged tokens. + * + * @return array Filtered tagged tokens. + */ + public function postTokenize($tokens) + { + $tokens = parent::postTokenize($tokens); + // Catch ambiguous constructs like "heute morgen/morgen früh". + foreach ($tokens as $num => $token) { + if ($num >= 2 && + ($repeater = $token->getTag('repeater')) && + $repeater instanceof Horde_Date_Repeater_Day && + ($grabber = $tokens[$num - 1]->getTag('grabber')) && + $grabber == 'next') { + $token = new Horde_Date_Parser_Token(''); + $token->tag('repeater_day_portion', + new Horde_Date_Repeater_DayPortion('morning')); + array_splice( + $tokens, + $num - 1, + 2, + array($token)); + return $tokens; + } + } + return $tokens; + } } diff --git a/framework/Date_Parser/lib/Horde/Date/Parser/Locale/De/Repeater.php b/framework/Date_Parser/lib/Horde/Date/Parser/Locale/De/Repeater.php index 0db68e8ad..1cb339370 100644 --- a/framework/Date_Parser/lib/Horde/Date/Parser/Locale/De/Repeater.php +++ b/framework/Date_Parser/lib/Horde/Date/Parser/Locale/De/Repeater.php @@ -28,8 +28,8 @@ class Horde_Date_Parser_Locale_De_Repeater extends Horde_Date_Parser_Locale_Base public $dayPortionScanner = array( '/^vormittags?$/' => 'morning', - '/^morgens?$/' => 'morning', - '/^afternoons?$/' => 'afternoon', + '/^(morgens?|früh)$/' => 'morning', + '/^nachmittags?$/' => 'afternoon', '/^abends?$/' => 'evening', '/^nachts?$/' => 'night', ); diff --git a/framework/Date_Parser/test/Horde/Date/Parser/Locale/DeTest.php b/framework/Date_Parser/test/Horde/Date/Parser/Locale/DeTest.php index 06fe9bc27..4813ff8c9 100644 --- a/framework/Date_Parser/test/Horde/Date/Parser/Locale/DeTest.php +++ b/framework/Date_Parser/test/Horde/Date/Parser/Locale/DeTest.php @@ -28,8 +28,12 @@ class Horde_Date_Parser_Locale_DeTest extends Horde_Test_Case public function testTomorrow() { - $this->markTestIncomplete('This is failing for me. I assume "morgen" matches the "morgens?" regex rather than indicating tomorrow.'); - $this->assertEquals('2006-08-17 12:00:00', (string)$this->parser->parse('morgen')); + $this->assertEquals('2006-08-17 09:00:00', (string)$this->parser->parse('morgen früh', array(), false)); + } + + public function testMorning() + { + $this->assertEquals('2006-08-16 09:00:00', (string)$this->parser->parse('heute morgen', array(), false)); } }