Fix ambiguity with the meaning of the German wordn 'morgen'.
authorJan Schneider <jan@horde.org>
Sat, 2 Oct 2010 21:21:45 +0000 (23:21 +0200)
committerJan Schneider <jan@horde.org>
Sat, 2 Oct 2010 21:21:52 +0000 (23:21 +0200)
framework/Date_Parser/lib/Horde/Date/Parser/Locale/De.php
framework/Date_Parser/lib/Horde/Date/Parser/Locale/De/Repeater.php
framework/Date_Parser/test/Horde/Date/Parser/Locale/DeTest.php

index e7e9467..5a6baa1 100644 (file)
@@ -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;
+    }
 }
index 0db68e8..1cb3393 100644 (file)
@@ -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',
     );
index 06fe9bc..4813ff8 100644 (file)
@@ -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));
     }
 
 }