#1
  1. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301

    Converting english words to number


    Inspired by the code Onslaught came up with in convert 21 to "twenty one" and a need for a function to do the reverse that arose in Written number (two, thirteen) to binary (10, 10101) , I came up with a routine that converts written text (such as "Two thousand, four hundred and seventy two") into a number. Without further ado, presenting ....

    PHP Code:
    <?
    function string_to_num($string) {
      
    $words_array preg_split("/[^\\w]+/"$string);
      
    $value 0;
      
    $value_array = array();
      for (
    $i 0$i count($words_array); $i++){
        
    $word strtolower($words_array[$i]);
        if (
    check_adds($word$result))
          
    $value += $result;
        elseif (
    check_muls($word$result)) {
          if (
    $value != 0)
        
    $value *= $result;
          else {
        
    $value array_pop($value_array);
        
    $value *= $result;
          }
          
    array_push($value_array$value);
          
    $value 0;
        }
        else 
          return 
    "Error: Unrecognized word '$word' in string '$string'";
      }

      
    array_push($value_array$value);

      
    /* Now compute the sum of the values in value_array */
      
    $value 0;
      for (
    $i 0$i count($value_array); $i++) 
        
    $value += $value_array[$i];

      return 
    $value;
    }

    function 
    check_adds($word, &$result) {
      
    $words = array (
               
    "zero" => 0"one" => 1"two" => 2"three" => 3,
               
    "four" => 4"five" => 5"six" => 6"seven" => 7,
               
    "eight" => 8"nine" => 9"and" => 0,
               
    "eleven" => 11"twelve" => 12"thirteen" => 13,
               
    "fourteen" => 14"fifteen" => 15"sixteen" => 16,
               
    "seventeen" => 17"eighteen" => 18"nineteen" => 19,
               
    "twenty" => 20"thirty" => 30"forty" => 40,
               
    "fifty" => 50"sixty" => 60"seventy" => 70,
               
    "eighty" => 80"ninety" => 90
               
    );

      if (
    in_array($wordarray_keys($words))) {
        
    $result $words[$word];
        return 
    true;
      } else
        return 
    false;
    }

    function 
    check_muls($word, &$result) {
      
    $words = array("hundred" => 100"thousand" => 1000"million" => 1000000,
             
    "billion" => 100000000);
      if (
    in_array($wordarray_keys($words))) {
        
    $result $words[$word];
        return 
    true;
      } else
        return 
    false;  
    }
    ?>

    <html>
      <head><title>Words to Number</title></head>
      <body>
    <?
    $text 
    "five";
    print 
    "The value of $text is " string_to_num($text) . "<br>";
    $text "forty two";
    print 
    "The value of $text is " string_to_num($text) . "<br>";
    $text "forty-two";
    print 
    "The value of $text is " string_to_num($text) . "<br>";
    $text "Four hundred and thirty eight";
    print 
    "The value of $text is " string_to_num($text) . "<br>";
    $text "Seven thousand, six hundred and fifty three";
    print 
    "The value of $text is " string_to_num($text) . "<br>";
    $text "nineteen thousand and twenty three";
    print 
    "The value of $text is " string_to_num($text) . "<br>";
    $text "four hundred thousand, seven hundred and seventy nine";
    print 
    "The value of $text is " string_to_num($text) . "<br>";
    $text "six million, fifty two thousand, seven hundred and twenty two";
    print 
    "The value of $text is " string_to_num($text) . "<br>";
    $text "six million, fifty two thousand, seven hundread and twenty two";
    print 
    "The value of $text is " string_to_num($text) . "<br>";
    ?>
      </body>
    </html>
    All comments/criticisms are welcome

    [edit] Looks like pasting the code directly from emacs into Opera sort of screwed up my code indenting. I removed all the extra newlines that got inserted, but the indenting may still be slightly screwed. Sorry! [/edit]
    Enjoy!
    Last edited by Scorpions4ever; October 2nd, 2002 at 12:02 AM.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301
    Found one issue with the above code. It won't parse numbers like "four hundred and twelve thousand" correctly. Oh well, I guess it's accurate upto "ninety nine thousand, nine hundred and ninety nine". After that it works for things like "four hundred thousand" but not "four hundred and twelve thousand". However, the fix for this isn't too hard to do. I have a good idea how to do it in my head, but I'll come up with the actual fix when I wake up tomorrow
  4. #3
  5. No Profile Picture
    Designer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    Netherlands
    Posts
    104
    Rep Power
    17
    WHY?

    LoL just what I'm wondering why make this? Normal people do it in reversed mode? It seems good enough code . I like your coding style (looks like mine :P).

    Good luck on the project
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301
    >>LoL just what I'm wondering why make this?
    Well, there was actually a need for such a routine in Written number (two, thirteen) to binary (10, 10101)

    Anyways, here's the corrected string_to_num routine(). This one will also handle text such as "six million, seven hundred and fifty two thousand, seven hundred and twenty two" correctly. It only needed 3 additional lines of code
    PHP Code:
    function string_to_num($string) {
        
    $words_array preg_split("/[^\w]+/"$string);
        
    $value 0;
        
    $value_array = array();
        for (
    $i 0$i count($words_array); $i++){
            
    $word strtolower($words_array[$i]);
            if (
    check_adds($word$result))
                
    $value += $result;
            elseif (
    check_muls($word$result)) {
                if (
    $value != 0) {
                    if (
    $value_array[count($value_array) - 1] > $value $result)
                        
    $value *= $result;
                    else
                        
    $value = (array_pop($value_array) + $value) * $result;
                } else {
                    
    $value array_pop($value_array);
                    
    $value *= $result;
                }
                
    array_push($value_array$value);
                
    $value 0;
            }
        else
            return 
    "Error: Unrecognized word '$word' in string '$string'";
        }

        
    array_push($value_array$value);

        
    /* Now compute the sum of the values in value_array */
        
    $value 0;
        for (
    $i 0$i count($value_array); $i++)
            
    $value += $value_array[$i];

        return 
    $value;

    Enjoy!
  8. #5
  9. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Pennsylvania
    Posts
    171
    Rep Power
    0
    Just a side note:

    The word "AND" is gramatically incorrect in ANY number, except when referring to a decimal.

    1,450: One thousand four hundred fifty
    142,000: One hundred fourty two thousand

    125.5: One hundred twenty five AND five tenths

    That is the correct way to use the word AND in mathematics.
  10. #6
  11. Mobbing Gangster
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Sep 2001
    Location
    "Best City" 2002 and 2003- Melbourne, Australia
    Posts
    4,912
    Rep Power
    36
    Originally posted by Pasiphae1213
    Just a side note:

    The word "AND" is gramatically incorrect in ANY number, except when referring to a decimal.

    1,450: One thousand four hundred fifty
    142,000: One hundred fourty two thousand

    125.5: One hundred twenty five AND five tenths

    That is the correct way to use the word AND in mathematics.
    What would you rather have - something grammatically correct or something that works?

    Btw, helleva bump.
    And you know I mean that.
  12. #7
  13. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Pennsylvania
    Posts
    171
    Rep Power
    0
    Personally? Id prefer something that works when the data is gramatically correct :-)

    But thats just me.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Location
    Melbourne, Australia
    Posts
    58
    Rep Power
    16
    Originally posted by Pasiphae1213
    Just a side note:

    The word "AND" is gramatically incorrect in ANY number, except when referring to a decimal.

    1,450: One thousand four hundred fifty
    142,000: One hundred fourty two thousand

    125.5: One hundred twenty five AND five tenths

    That is the correct way to use the word AND in mathematics.
    I think there is also a little cultural difference here, Americans don't put AND, but I've seen a lot of Australians that do.
  16. #9
  17. webber
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2000
    Location
    San Juan - Argentina
    Posts
    594
    Rep Power
    18
    hi guys
    i found this thread coz i need to do viceversa..

    convert 123 to one hundred and twenty three (but in spanish)...

    so, does some1 have some code to do this ?

    yea, still newbie here

    thanks
    Enrique Becerra
    CodeIgniter, Joomla & Java Development
    www.itexa.com.ar
  18. #10
  19. No Profile Picture
    $post_count++;
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2001
    Location
    Southern MN, USA
    Posts
    1,142
    Rep Power
    24
    Originally Posted by quiquedcode
    hi guys
    i found this thread coz i need to do viceversa..

    convert 123 to one hundred and twenty three (but in spanish)...

    so, does some1 have some code to do this ?

    yea, still newbie here

    thanks
    Inspired by the code Onslaught came up with in http://forums.devshed.com/showthread.php?threadid=44240...
    It was in the first post of this thread.

    Comments on this post

    • quiquedcode agrees : very quick help
  20. #11
  21. webber
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2000
    Location
    San Juan - Argentina
    Posts
    594
    Rep Power
    18
    thanks again Xeo !!
    nice Tip !!
    Enrique Becerra
    CodeIgniter, Joomla & Java Development
    www.itexa.com.ar
  22. #12
  23. No Profile Picture
    $post_count++;
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2001
    Location
    Southern MN, USA
    Posts
    1,142
    Rep Power
    24
    Originally Posted by quiquedcode
    thanks again Xeo !!
    nice Tip !!
    It's not my tip, it's from the first post of the thread you bumped!

IMN logo majestic logo threadwatch logo seochat tools logo