### Thread: Converting english words to number

1. #### 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(\$word, array_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(\$word, array_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> ```

 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. 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
3. 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
4. >>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!
5. 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.
6. 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.
7. Personally? Id prefer something that works when the data is gramatically correct :-)

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

Join Date
Nov 2002
Location
Melbourne, Australia
Posts
58
Rep Power
17
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.
9. 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
10. No Profile Picture
Xeo
\$post_count++;
Devshed Beginner (1000 - 1499 posts)

Join Date
Jun 2001
Location
Southern MN, USA
Posts
1,142
Rep Power
25
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
It was in the first post of this thread.

• quiquedcode agrees : very quick help
11. thanks again Xeo !!
nice Tip !!
12. No Profile Picture
Xeo
\$post_count++;
Devshed Beginner (1000 - 1499 posts)

Join Date
Jun 2001
Location
Southern MN, USA
Posts
1,142
Rep Power
25
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!