Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Converting numbers from text and vice-versa

Converting numbers from text and vice-versa

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1 2  3  4  

Posted by Tiopon   USA  (71 posts)  Bio
Date Wed 17 Mar 2010 05:03 AM (UTC)
Message
Wondering if there's a good way to convert numbers from text (twenty) to the actual number (20). I could do this as a painful manual system, but there must be a better way than detecting everything one at a time... Best thing I've come up with so far would be something that expects commas between each set and does an addition with a looping system... search for the words "one thousand" and do tempvalue = tempvalue + 1000, etc. It would work sometimes, but it's not pretty, and it'll miss most of the more complicated scenarios...
Top

Posted by Nick Gammon   Australia  (23,057 posts)  Bio   Forum Administrator
Date Reply #1 on Wed 17 Mar 2010 06:20 AM (UTC)
Message
I think it would be complicated to get right. Why do you want to do it?

Something like this would be fiddly to do:


fifty-three million billion eighty million forty-one thousand six hundred and twenty-two.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #2 on Wed 17 Mar 2010 07:18 AM (UTC)
Message
Nick Gammon said:
fifty-three million billion eighty million forty-one thousand six hundred and twenty-two.


53,000,000,000,080,041,622, I think?

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,057 posts)  Bio   Forum Administrator
Date Reply #3 on Wed 17 Mar 2010 09:08 AM (UTC)
Message
And the code you used to produce that, Twisol?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Larkin   (278 posts)  Bio
Date Reply #4 on Wed 17 Mar 2010 01:36 PM (UTC)
Message
The example Nick gave, however, is bad numbering and shouldn't be used by anyone ever.

I found code to convert numbers to names: http://rosettacode.org/wiki/Number_names#Lua

Theoretically, the reverse should be about as easy, assuming proper formatting of the input string.
Top

Posted by Tiopon   USA  (71 posts)  Bio
Date Reply #5 on Wed 17 Mar 2010 01:45 PM (UTC)

Amended on Wed 17 Mar 2010 06:25 PM (UTC) by Tiopon

Message
Too much data in the code on this... Ha. I'll put the other SQL examples in the next several posts.

Hmm... threw this into Google with a slightly clearer head. Shockingly there wasn't a good Lua answer. :) Lots of stuff for the reverse... most people copy Microsoft's Excel thing (http://support.microsoft.com/kb/213360) in some way or use some form of num2word to do it.

For my purposes though, it seems this Oracle forum at http://forums.oracle.com/forums/thread.jspa?threadID=506385 is likely the best result... Here's some of the code. No clue what I'll be doing to convert it back to Lua though at this point. Maybe after a lot of staring?

create or replace function word2num( p_word in varchar2 ) return number is
 type myArray is table of varchar2(255);
 
 places_arr myArray := myArray( 
'vigintillion',
'novemdecillion',
'octodecillion',
'septendecillion',
'sexdecillion',
'quindecillion',
'quattuordecillion',
'tredecillion',
'duodecillion',
'undecillion',
'decillion',
'nonillion',
'octillion',
'septillion',
'sextillion',
'quintillion',
'quadrillion',
'trillion', 'billion', 'million', 'thousand' );
 
 place_count number := places_arr.count;
 
type strarr is table of number index by varchar2(20);
hunds_arr strarr;
num_Arr strarr;
ret_num number := 0;
o_str varchar2(4000) := trim(lower(p_word)) || ' ';
temp_str varchar2(4000);
temp_num number;
 
loc number;
begin
if lower(o_str) = 'zero' then return 0; end if; 
 
for i in 1 .. 9 loop
 hunds_arr( to_Char( to_Date(i*100,'j'), 'jsp') ) := i*100;
end loop;
 
for i in 1 .. 99 loop
 num_arr( to_Char( to_Date(i,'j'), 'jsp') ) := i;
end loop;
 
for place_idx in 1 .. places_arr.count loop
 loc := instr( o_str, places_arr(place_idx) );
 if loc  0 then
  temp_num := 0;
  temp_Str := trim(substr( o_str, 1, loc-1)) || ' ';
  if hunds_arr.exists( trim(substr(temp_str,1,instr(temp_str,' ',1,2))) ) then
   temp_num := hunds_arr( trim(substr(temp_str,1,instr(temp_str,' ',1,2))) );
   temp_str := trim(substr(temp_str,instr(temp_str,' ',1,2)));
  end if;
  if num_arr.exists( trim(temp_str) ) then
   temp_num := temp_num + num_arr( trim(temp_str) );
  end if;
  ret_num := ret_num + ( power(10,3*(place_count+1-place_idx)) * temp_num );
  o_str := trim(substr( o_str, loc+length(places_arr(place_idx)) )) ||' ';
 end if;
end loop;
-- deal with <1000 portion
 temp_num := 0;
 temp_str := o_str || ' ';
  if hunds_arr.exists( trim(substr(temp_str,1,instr(temp_str,' ',1,2))) ) then
   temp_num := hunds_arr( trim(substr(temp_str,1,instr(temp_str,' ',1,2))) );
   temp_str := trim(substr(temp_str,instr(temp_str,' ',1,2)));
  end if;
  if num_arr.exists( trim(temp_str) ) then
   temp_num := temp_num + num_arr( trim(temp_str) );
  end if;
  ret_num := ret_num + temp_num;
 
 
 return (ret_num );
end;
/ 
show errors
 
 
-- test it 50 times (50 just sounded like a nice round number)
select str, num, word2num(str) from (
select num, to_Char( to_Date(num,'j'), 'Jsp') str
from (
select trunc(dbms_random.value(1,5373484)) num from dual connect by level<=50
)
);


The original version had an issue with zero, but I added their quick suggestion for a check right after it starts...
Top

Posted by Tiopon   USA  (71 posts)  Bio
Date Reply #6 on Wed 17 Mar 2010 01:46 PM (UTC)

Amended on Wed 17 Mar 2010 01:52 PM (UTC) by Tiopon

Message
Still too long... I'll split the functions. Third post has the third version.

Here is another SQL function to do the same...

create or replace procedure word_to_num(p_words IN VARCHAR2) is
  p_word varchar2(4000) := p_words;
  type mynums is table of number index by varchar2(30);
  type myquals is table of number index by varchar2(30);
  v_nums mynums;
  v_quals myquals;
  v_word VARCHAR2(30);
  v_resval NUMBER := 0;
  v_tmpresval NUMBER := 0;
  v_val    NUMBER;
  v_switch NUMBER := 0;
  FUNCTION get_word(v_str IN OUT VARCHAR2) RETURN varchar2 IS
    v_ret VARCHAR2(30);
  BEGIN
    IF INSTR(v_str,' ') = 0 THEN
      v_ret := v_str;
      v_str := '';
    ELSE
      v_ret := SUBSTR(v_str, 1, INSTR(v_str, ' ')-1);
      v_str := SUBSTR(v_str, INSTR(v_str, ' ')+1);
    END IF;
    RETURN v_ret;
  END;
begin
  v_nums('ZERO') := 0;
  v_nums('ONE') := 1;
  v_nums('TWO') := 2;
  v_nums('THREE') := 3;
  v_nums('FOUR') := 4;
  v_nums('FIVE') := 5;
  v_nums('SIX') := 6;
  v_nums('SEVEN') := 7;
  v_nums('EIGHT') := 8;
  v_nums('NINE') := 9;
  v_nums('TEN') := 10;
  v_nums('ELEVEN') := 11;
  v_nums('TWELVE') := 12;
  v_nums('THIRTEEN') := 13;
  v_nums('FOURTEEN') := 14;
  v_nums('FIFTEEN') := 15;
  v_nums('SIXTEEN') := 16;
  v_nums('SEVENTEEN') := 17;
  v_nums('EIGHTEEN') := 18;
  v_nums('NINETEEN') := 19;
  v_nums('TWENTY') := 20;
  v_nums('THIRTY') := 30;
  v_nums('FORTY') := 40;
  v_nums('FIFTY') := 50;
  v_nums('SIXTY') := 60;
  v_nums('SEVENTY') := 70;
  v_nums('EIGHTY') := 80;
  v_nums('NINETY') := 90;
  v_quals('HUNDRED') := 100;
  v_quals('THOUSAND') := 1000;
  v_quals('MILLION') := 1000000;
  v_quals('BILLION') := 1000000000;
  v_quals('TRILLION') := 1000000000000;
  v_quals('QUADRILLION') := 1000000000000000;
  v_quals('QUINTILLION') := 1000000000000000000;
  v_quals('SEXTILLION') := 1000000000000000000000;
  v_quals('SEPTILLION') := 1000000000000000000000000;
  v_quals('OCTILLION') := 1000000000000000000000000000;
  v_quals('NONILLION') := 1000000000000000000000000000000;
  v_quals('DECILLION') := 1000000000000000000000000000000000;
  v_quals('UNDECILLION') := 1000000000000000000000000000000000000;
  v_quals('DUODECILLION') := 1000000000000000000000000000000000000000;
  DBMS_OUTPUT.PUT_LINE('word     : '||p_word);
  LOOP
    EXIT WHEN p_word IS NULL;
    v_word := get_word(p_word);
    BEGIN
      v_val := v_nums(v_word);
      v_resval := v_resval + v_tmpresval;
      v_tmpresval := v_val;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        BEGIN
          v_val := v_quals(v_word);
          IF v_val >= v_switch THEN
            v_resval := v_resval*v_val;
          END IF;
          v_tmpresval := v_tmpresval*v_val;
          v_switch := v_val;
        EXCEPTION
          WHEN NO_DATA_FOUND THEN
            DBMS_OUTPUT.PUT_LINE('Error In Number String : '||v_word);
        END;
    END;
/*    DBMS_OUTPUT.PUT_LINE('word     : '||p_word);
    DBMS_OUTPUT.PUT_LINE('tmpresval: '||TO_CHAR(v_tmpresval,'999,999,999,999,999,999,999,999,999,999,999,999,999,999'));
    DBMS_OUTPUT.PUT_LINE('resval   : '||TO_CHAR(v_resval,'999,999,999,999,999,999,999,999,999,999,999,999,999,999'));
    DBMS_OUTPUT.PUT_LINE('v_switch : '||TO_CHAR(v_switch,'999,999,999,999,999,999,999,999,999,999,999,999,999,999'));
    IF b_switch THEN
      DBMS_OUTPUT.PUT_LINE('b_switch : TRUE');
    ELSE
      DBMS_OUTPUT.PUT_LINE('b_switch : FALSE');
    END IF;
*/
  END LOOP;
  v_resval := v_resval + v_tmpresval;
  DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_resval,'999,999,999,999,999,999,999,999,999,999,999,999,999,999'));
END;


Not exactly sure on that one... or the next.
Top

Posted by Tiopon   USA  (71 posts)  Bio
Date Reply #7 on Wed 17 Mar 2010 01:50 PM (UTC)
Message
Two posts to this one. Still SQL.
create or replace function calculate_string(p_line in varchar2) return r is
  n number;
begin
  execute immediate 'select '||p_line||' from dual'
    into n;
  return(n);
end;
Top

Posted by Tiopon   USA  (71 posts)  Bio
Date Reply #8 on Wed 17 Mar 2010 01:50 PM (UTC)
Message
And its SQL query...
with t as (select 'score' word,      '*20+' num from dual union all
             select 'hundred' ,        '*100+' from dual union all
             select 'thousand',        ')*1000+(' from dual union all
             select 'lakh',            ')*power(10,5)+(' from dual union all
             select 'million',         ')*power(10,6)+(' from dual union all
             select 'crore',           ')*power(10,7)+(' from dual union all
             select 'billion',         ')*power(10,9)+(' from dual union all
             select 'trillion',        ')*power(10,12)+(' from dual union all
             select 'quadrillion',     ')*power(10,15)+(' from dual union all
             select 'quintillion',     ')*power(10,18)+(' from dual union all
             select 'sextillion',      ')*power(10,21)+(' from dual union all
             select 'septillion',      ')*power(10,24)+(' from dual union all
             select 'octillion',       ')*power(10,27)+(' from dual union all
             select 'nonillion',       ')*power(10,30)+(' from dual union all
             select 'undecillion',     ')*power(10,36)+(' from dual union all
             select 'duodecillion',    ')*power(10,39)+(' from dual union all
             select 'tredecillion',    ')*power(10,42)+(' from dual union all
             select 'quattuordecillion',')*power(10,45)+(' from dual union all
             select 'quindecillion',   ')*power(10,48)+(' from dual union all
             select 'sexdecillion',    ')*power(10,51)+(' from dual union all
             select 'septendecillion', ')*power(10,54)+(' from dual union all
             select 'octodecillion',   ')*power(10,57)+(' from dual union all
             select 'novemdecillion',  ')*power(10,60)+(' from dual union all
             select 'decillion',       ')*power(10,33)+(' from dual union all
             select 'vigintillion',    ')*power(10,63)+(' from dual union all
             select 'thirteen',        '13' from dual union all
             select 'fourteen',        '14' from dual union all
             select 'fifteen',         '15' from dual union all
             select 'sixteen',         '16' from dual union all
             select 'seventeen',       '17' from dual union all
             select 'eighteen',        '18' from dual union all
             select 'nineteen',        '19' from dual union all
             select 'twenty',          '20+' from dual union all
             select 'thirty',          '30+' from dual union all
             select 'forty',          '40+' from dual union all
             select 'fifty',           '50+' from dual union all
             select 'sixty',           '60+' from dual union all
             select 'seventy',         '70+' from dual union all
             select 'eighty',          '80+' from dual union all
             select 'ninety',          '90+' from dual union all
             select 'zero',            '0+' from dual union all
             select 'one',             '1+' from dual union all
             select 'two',             '2+' from dual union all
             select 'three',           '3+' from dual union all
             select 'four',            '4+' from dual union all
             select 'five',            '5+' from dual union all
             select 'six',             '6+' from dual union all
             select 'seven',           '7+' from dual union all
             select 'eight',           '8+' from dual union all
             select 'nine',            '9+' from dual union all
             select 'ten',             '10+' from dual union all
             select 'eleven',          '11' from dual union all
             select 'twelve',          '12' from dual),
             --
  word_num as (select 'one hundred and fifty' a from dual union all
               select 'one thousand and hundred' a from dual union all
               select 'two lakhs fifty' from dual union all
               select 'three crores six lakhs fifteen' from dual union all
               select 'ONE THOUSAND - ONE HUNDRED - ELEVEN' from dual union all
               select 'Four Thousand Four Hundred' from dual union all
               select 'Five hundred and twenty seven' from dual union all
               select 'Score And Seven' from dual union all
               select  to_Char(to_Date(trunc(dbms_random.value(1,5373484)),'j'), 'Jsp') from dual union all
               select  to_Char(to_Date(trunc(dbms_random.value(1,5373484)),'j'), 'Jsp') from dual union all
               select  to_Char(to_Date(trunc(dbms_random.value(1,5373484)),'j'), 'Jsp') from dual union all
               select  to_Char(to_Date(trunc(dbms_random.value(1,5373484)),'j'), 'Jsp') from dual union all
  --end of test data
    select text,
           calculate_string(str) "number",
           str string_for_calculating
     from
      (select text,
              replace(replace(replace('('||regexp_replace(regexp_replace(a, '(power)|[[:alpha:] ]','\1'),'(\+)([^[:digit:](]|$)', '\2')||')','()*','(1)*'),'()','(0)'),'(*','(') str
         from (select *
                 from (select rownum rn, a text, regexp_replace(lower(a),'[^[:alnum:]]',' ') a from word_num)
                model
                   reference r
                     on (select rownum rn, word, num from t)
                     dimension by (rn)
                     measures(word, num)
                   main m
                    dimension by (rn dim)
                    measures (text, cast(a as varchar2(4000)) a)
                    rules iterate(1000) until(PRESENTV(r.word[iteration_number+1],1,0)<1)
                     (a[ANY]=regexp_replace(a[CV()], r.word[iteration_number+1], r.num[iteration_number+1]))
              )
           )
/ 


Anyways, I think the first would likely be the easiest to convert. Does that seem right?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #9 on Wed 17 Mar 2010 04:06 PM (UTC)
Message
Ack, I was hoping for a nifty Lua thing and then I see this SQL monster. :-P

(One of my big issues with SQL code is that it depends so heavily on which DB server you use...)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #10 on Wed 17 Mar 2010 04:13 PM (UTC)
Message
Nick Gammon said:

And the code you used to produce that, Twisol?


Nothing typed up. I just noticed that it always went from small to large (Fifty-three, million, billion. Eighty, million . Forty-one, thousand. Six, hundred. Twenty-two). Then I converted each "word" separately, and multiplied the words in a "sentence" together. Then I added up the sentences. (I guess "Fifty-three" would count as a sentence, so to speak, but it's more of a sub-sentence. You'd treat it as "Fifty, three" then use the result in the sentence it belonged to.)

It might not hold up in general, considering Larkin's comment, but I don't know what specifically was wrong about it. And I just ran this algorithm in my head (using Calculator to do the large-number arithmetic).

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #11 on Wed 17 Mar 2010 04:54 PM (UTC)

Amended on Wed 17 Mar 2010 05:11 PM (UTC) by David Haley

Message
Yeah, I agree that it doesn't look that hard, really. If you go from unit to unit, you know that the first unit is modifying the second unit. (Thousand million == thousands of millions. Billion thousand, weird as it may be: billions of thousands.)

So basically your grammar is the following:

Number := (EnglishNumber Unit*)+

EnglishNumber := One | Two | ... | Seventeen | Ninety-Nine | blablabla

Unit := Hundred | Thousand | Million | blablabla


(I guess I need "and" in my grammar but whatever <EDIT: or as Twisol says, we can just strip it and it still works>)

then once you have this parsed, you walk forward. You take the unit list, you multiply through as appropriate, then you multiply that by your English number. Finally, you sum your individual "unit components".

So, "fifty-three million billion eighty million forty-one thousand six hundred and twenty-two"

is parsed by breaking it into units like this:

fifty-three million billion --> 53 * 1,000,000 * 1,000,000,000 = 53,000,000,000,000,000
eighty million = 80 * 1,000,000 = 80,000,000
forty-one thousand = 41 * 1,000 = 41,000
six hundred = 6 * 100 = 600
and
twenty-two = 22

Finally,


  53,000,000,000,000,000
+             80,000,000
+                 41,000
+                    600
+                     22
= 53,000,000,080,041,622


This is not what Twisol got (he has an extra 000 magnitude), maybe I mixed up or he mixed up, but basically I think this isn't a super hard problem if the input grammar is fairly well-specified.


EDIT: fix tags

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #12 on Wed 17 Mar 2010 05:08 PM (UTC)
Message
I think I must've gotten it mixed up, because that looks right. That's what I'm talking about though, yeah. I think you can ignore (strip out) "and".

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Tiopon   USA  (71 posts)  Bio
Date Reply #13 on Wed 17 Mar 2010 06:41 PM (UTC)
Message
Found a VB conversion script that someone made for turning cost in Euros into numbers... biggest issue there is that it's multilingual and huge... over 18k. It was a part of a number to word conversion, and I think it's the flip side, but I get lost somewhere in the French. Anyways, does this look worth actually pursuing, or is this another number to word conversion and nothing more?

And of course, I forgot to actually include the link. :) http://visualbasic.ittoolbox.com/groups/technical-functional/visualbasic-l/convert-amount-in-words-into-amoint-in-numbers-1449021
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #14 on Wed 17 Mar 2010 06:52 PM (UTC)
Message
What exactly are you trying to do? What is your input like? The algorithm I outlined above would do the trick if your input is of the form I gave; you'd just need to add translations from English numbers to numeric values.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


144,985 views.

This is page 1, subject is 4 pages long: 1 2  3  4  [Next page]

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.