--- /dev/null
+<?php
+/**
+ *
+ */
+class Horde_Support_Numerizer
+{
+ public static function factory($locale = null, $args = array())
+ {
+ if ($locale) {
+ $locale = ucfirst($locale);
+ $class = 'Horde_Support_Numerizer_Locale_' . $locale;
+ if (class_exists($class)) {
+ return new $class($args);
+ }
+
+ $country = array_shift(explode('_', $locale));
+ if ($country != $locale) {
+ $class = 'Horde_Support_Numerizer_Locale_' . $country;
+ if (class_exists($class)) {
+ return new $class($args);
+ }
+ }
+ }
+
+ return new Horde_Support_Numerizer_Locale_Base($args);
+ }
+
+}
--- /dev/null
+<?php
+class Horde_Support_Numerizer_Locale_Base
+{
+ public $DIRECT_NUMS = array(
+ 'eleven' => '11',
+ 'twelve' => '12',
+ 'thirteen' => '13',
+ 'fourteen' => '14',
+ 'fifteen' => '15',
+ 'sixteen' => '16',
+ 'seventeen' => '17',
+ 'eighteen' => '18',
+ 'nineteen' => '19',
+ 'ninteen' => '19', // Common mis-spelling
+ 'zero' => '0',
+ 'one' => '1',
+ 'two' => '2',
+ 'three' => '3',
+ 'four(\W|$)' => '4$1', // The weird regex is so that it matches four but not fourty
+ 'five' => '5',
+ 'six(\W|$)' => '6$1',
+ 'seven(\W|$)' => '7$1',
+ 'eight(\W|$)' => '8$1',
+ 'nine(\W|$)' => '9$1',
+ 'ten' => '10',
+ '\ba[\b^$]' => '1', // doesn't make sense for an 'a' at the end to be a 1
+ );
+
+ public $TEN_PREFIXES = array(
+ 'twenty' => 20,
+ 'thirty' => 30,
+ 'fourty' => 40,
+ 'fifty' => 50,
+ 'sixty' => 60,
+ 'seventy' => 70,
+ 'eighty' => 80,
+ 'ninety' => 90,
+ );
+
+ public $BIG_PREFIXES = array(
+ 'hundred' => 100,
+ 'thousand' => 1000,
+ 'million' => 1000000,
+ 'billion' => 1000000000,
+ 'trillion' => 1000000000000,
+ );
+
+ public function numerize($string)
+ {
+ // preprocess
+ // will mutilate hyphenated-words but shouldn't matter for date extraction
+ $string = preg_replace('/ +|([^\d])-([^d])/', '$1 $2', $string);
+ // take the 'a' out so it doesn't turn into a 1, save the half for the end
+ $string = str_replace('a half', 'haAlf', $string);
+
+ // easy/direct replacements
+ foreach ($this->DIRECT_NUMS as $dn => $dn_replacement) {
+ $string = preg_replace("/$dn/i", $dn_replacement, $string);
+ }
+
+ // ten, twenty, etc.
+ foreach ($this->TEN_PREFIXES as $tp => $tp_replacement) {
+ $string = preg_replace_callback(
+ "/(?:$tp)( *\d(?=[^\d]|\$))*/i",
+ create_function(
+ '$m',
+ 'return ' . $tp_replacement . ' + (isset($m[1]) ? (int)$m[1] : 0);'
+ ),
+ $string);
+ }
+
+ // hundreds, thousands, millions, etc.
+ foreach ($this->BIG_PREFIXES as $bp => $bp_replacement) {
+ $string = preg_replace_callback(
+ '/(\d*) *' . $bp . '/i',
+ create_function(
+ '$m',
+ 'return ' . $bp_replacement . ' * (int)$m[1];'
+ ),
+ $string);
+ $string = $this->_andition($string);
+ }
+
+ // fractional addition
+ // I'm not combining this with the previous block as using float addition complicates the strings
+ // (with extraneous .0's and such )
+ $string = preg_replace_callback(
+ '/(\d+)(?: | and |-)*haAlf/i',
+ create_function(
+ '$m',
+ 'return (string)((float)$m[1] + 0.5);'
+ ),
+ $string);
+
+ return $string;
+ }
+
+ protected function _andition($string)
+ {
+ while (true) {
+ if (preg_match('/(\d+)( | and )(\d+)(?=[^\w]|$)/i', $string, $sc, PREG_OFFSET_CAPTURE)) {
+ if (preg_match('/and/', $sc[2][0]) || $sc[1][0] > $sc[3][0]) {
+ $string = substr($string, 0, $sc[1][1]) . ((int)$sc[1][0] + (int)$sc[3][0]) . substr($string, $sc[3][1] + strlen($sc[3][0]));
+ continue;
+ }
+ }
+ break;
+ }
+ return $string;
+ }
+
+}
--- /dev/null
+<?php
+/**
+ * @category Horde
+ * @package Support
+ * @subpackage UnitTests
+ * @copyright 2008 The Horde Project (http://www.horde.org/)
+ * @license http://opensource.org/licenses/bsd-license.php
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+ define('PHPUnit_MAIN_METHOD', 'Horde_Support_AllTests::main');
+}
+
+require_once 'PHPUnit/Framework/TestSuite.php';
+require_once 'PHPUnit/TextUI/TestRunner.php';
+
+class Horde_Support_AllTests {
+
+ public static function main()
+ {
+ PHPUnit_TextUI_TestRunner::run(self::suite());
+ }
+
+ public static function suite()
+ {
+ set_include_path(dirname(__FILE__) . '/../../../lib' . PATH_SEPARATOR . get_include_path());
+ if (!spl_autoload_functions()) {
+ spl_autoload_register(create_function('$class', '$filename = str_replace(array(\'::\', \'_\'), \'/\', $class); include "$filename.php";'));
+ }
+
+ $suite = new PHPUnit_Framework_TestSuite('Horde Framework - Horde_Support');
+
+ $basedir = dirname(__FILE__);
+ $baseregexp = preg_quote($basedir . DIRECTORY_SEPARATOR, '/');
+
+ foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($basedir)) as $file) {
+ if ($file->isFile() && preg_match('/Test.php$/', $file->getFilename())) {
+ $pathname = $file->getPathname();
+ require $pathname;
+
+ $class = str_replace(DIRECTORY_SEPARATOR, '_',
+ preg_replace("/^$baseregexp(.*)\.php/", '\\1', $pathname));
+ $suite->addTestSuite('Horde_Support_' . $class);
+ }
+ }
+
+ return $suite;
+ }
+
+}
+
+if (PHPUnit_MAIN_METHOD == 'Horde_Support_AllTests::main') {
+ Horde_Support_AllTests::main();
+}
--- /dev/null
+<?php
+/**
+ * @category Horde
+ * @package Horde_Support
+ * @subpackage UnitTests
+ */
+
+/**
+ * @category Horde
+ * @package Horde_Support
+ * @subpackage UnitTests
+ */
+class Horde_Support_NumerizerTest extends PHPUnit_Framework_TestCase
+{
+ public function testStraightParsing()
+ {
+ $numerizer = Horde_Support_Numerizer::factory();
+ $strings = array(
+ 1 => 'one',
+ 5 => 'five',
+ 10 => 'ten',
+ 11 => 'eleven',
+ 12 => 'twelve',
+ 13 => 'thirteen',
+ 14 => 'fourteen',
+ 15 => 'fifteen',
+ 16 => 'sixteen',
+ 17 => 'seventeen',
+ 18 => 'eighteen',
+ 19 => 'nineteen',
+ 20 => 'twenty',
+ 27 => 'twenty seven',
+ 31 => 'thirty-one',
+ 59 => 'fifty nine',
+ 100 => 'a hundred',
+ 100 => 'one hundred',
+ 150 => 'one hundred and fifty',
+ // 150 => 'one fifty',
+ 200 => 'two-hundred',
+ 500 => '5 hundred',
+ 999 => 'nine hundred and ninety nine',
+ 1000 => 'one thousand',
+ 1200 => 'twelve hundred',
+ 1200 => 'one thousand two hundred',
+ 17000 => 'seventeen thousand',
+ 21473 => 'twentyone-thousand-four-hundred-and-seventy-three',
+ 74002 => 'seventy four thousand and two',
+ 99999 => 'ninety nine thousand nine hundred ninety nine',
+ 100000 => '100 thousand',
+ 250000 => 'two hundred fifty thousand',
+ 1000000 => 'one million',
+ 1250007 => 'one million two hundred fifty thousand and seven',
+ 1000000000 => 'one billion',
+ 1000000001 => 'one billion and one',
+ );
+
+ foreach ($strings as $key => $string) {
+ $this->assertEquals($key, (int)$numerizer->numerize($string));
+ }
+ }
+
+}