AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Simple PHP Examples
 
 

Ok, here’s the code for the function to convert “numeric words” (e.g. “one”, “seventeen”, “four thousand and ninety six”) to numbers, and the link for the page to test it:

http://www.geekcavecreations.com/lToN.php

And the code:

function convert_lToN($in){
  
global $addCollumn;
  if (empty(
$in)) return $in;
  
$numberWords = array(
                          
"zero"      => 0,
                          
"one"       => 1,
                          
"two"       => 2,
                          
"three"     => 3,
                          
"four"      => 4,
                          
"five"      => 5,
                          
"six"       => 6,
                          
"seven"     => 7,
                          
"eight"     => 8,
                          
"nine"      => 9,
                          
"ten"       => 10,
                          
"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,
                          
"hundred"   => 100,
                          
"thousand"  => 1000,
                          
"million"  => 1000000,
                          
"billion"  => 1000000000,
                          
"and"       => 0
                          
);

  
$wordList explode(" "$in);
  
$out "";
  
$myNumber 0;
  
$outArray = array();
  
$lastComma 0;
  foreach (
$wordList as $index => $word{
    $test 
strtolower(rtrim($word,",.?!"));
    if (!
array_key_exists($test$numberWords)) {
      $outArray[] 
$word;
      continue;
    
}
    
if ($test == "billion"{
      $acCount 
count($addCollumn) - 1;
      
$tmpTotal 0;
      for(
$n $lastComma$n <= $acCount;$n++) {
        $tmpAdd 
$addCollumn[$n];
        
$addCollumn[$n] 0;
        
$tmpTotal += $tmpAdd;
      
}
      $addCollumn[] 
0;
      
$lastComma $n 1;
      continue;
    
}
    
if ($test == "million"{
      $acCount 
count($addCollumn) - 1;
      
$tmpTotal 0;
      for(
$n $lastComma$n <= $acCount;$n++) {
        $tmpAdd 
$addCollumn[$n];
        
$addCollumn[$n] 0;
        
$tmpTotal += $tmpAdd;
      
}
      $addCollumn[$acCount] 
$tmpTotal 1000000;
      
$addCollumn[] 0;
      
$lastComma $n 1;
      continue;
    
}
    
if ($test == "thousand"{
      $acCount 
count($addCollumn) - 1;
      
$tmpTotal 0;
      for(
$n $lastComma$n <= $acCount;$n++) {
        $tmpAdd 
$addCollumn[$n];
        
$addCollumn[$n] 0;
        
$tmpTotal += $tmpAdd;
      
}
      $addCollumn[$acCount] 
$tmpTotal 1000;
      
$addCollumn[] 0;
      
$lastComma $n 1;
      continue;
    
}
    
if ($test == "hundred"{
      $acCount 
count($addCollumn);
      
$addCollumn[$acCount 1] *= 100;
      
$addCollumn[] 0;
      continue;
    
}
    $addCollumn[] 
$numberWords[$test];
    
$outArray[] "ph";
  
}
  $total 
0;
  foreach (
$addCollumn as $num{
    $total 
+= $num;
  
}
  $total 
number_format($total);
  
$phFound false;
  foreach (
$outArray as $place{
    
if ($place == "ph"{
      
if ($phFound) continue;
      
$phFound true;
      
$out .= $total";
      continue;
    
}
    $out 
.= $place";
  
}
  
return ltrim($out);

Again, I don’t think it will handle multiple instances of number words, nor will it process things like “999 billion”, but the core functionality works as intended.

 

 
  [ # 1 ]

Awesome job Dave !!

I tested it with

nine hundred and eighty six thousand four hundred and nine

and it got it bang on !!

 

 
  [ # 2 ]

Try it with it’s limit of “nine hundred and ninety nine billion nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine”. smile
(Note the word “and” in there several times. I made sure to provide for that, as well.)

 

 
  [ # 3 ]

As I had feared, sentences like “There are six billion people in the world, and only one of me.” don’t work. I’ll think about how to address this, and post the corrected code once I figure it out.

 

 
  [ # 4 ]

But, why does that not work, when the limit works?

 

 
  [ # 5 ]

Hi, Abhishek!

I’m sorry it took me so long to get back to this. I was in Maryland, attending a conference, and your post just “fell through the cracks”. I only just noticed the post, and thought I would answer you, even if I’m a few months late. smile

The reason that my example of “There are six billion people in the world, but only one of me” is because there are words in the input string that aren’t related to numbers (e.g. “in the world”), and the function does not know how to deal with them. In order to process such a string input, you would first have to pass it through another function (that I have yet to write) that separates the “number strings” (e.g. “six billion”) and process them, and then replacing the old text (“six billion”) and replacing it with the results you received (6.000.000.000). To do this within the scope of the current function is not a part of the intended design.

 

 
  login or register to react