Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Spelled out numbers to numerical equivalent function

by Akira71 (Scribe)
on Apr 12, 2005 at 15:39 UTC ( [id://447053]=perlquestion: print w/replies, xml ) Need Help??

Akira71 has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

Once again I must graciously throw myself upon your collective wisdom for a solution to a problem. I am looking for a function or perhaps a regex that can take a spelled out number like 'first' and then convert it to '1st'. Initally this would seem to be easy but then interesting possibilities creep up. Many years ago in my lost C porgramming days I had an algorithm for just this tpye of thing, but now I can find no reference to it nor find something similar online. The reason for my frustration is a billing company I am interfacing with requires the addresses to replace spelled out numbers to numerical equivalents. Numbers after 10 rarely show up, but a quick check in our database reveals people entering addresses like:
12308 Thirteenth Street

The answer should then be 12308 13th Street.

If anyone knows of such an algorithm in perl I would appreciate it.

Sincerely,

Akira the most humble in this regards.
  • Comment on Spelled out numbers to numerical equivalent function

Replies are listed 'Best First'.
Re: Spelled out numbers to numerical equivalent function
by davido (Cardinal) on Apr 12, 2005 at 15:52 UTC
Re: Spelled out numbers to numerical equivalent function
by NateTut (Deacon) on Apr 12, 2005 at 15:56 UTC
      Thank you both very much! I could not find this package, but I knew I ran into something in the past. This looks to be it.
Re: Spelled out numbers to numerical equivalent function
by Akira71 (Scribe) on Apr 12, 2005 at 17:53 UTC
    Ahh, now it looks as though I spoke too soon. For instance in a case like this test below:
    #!/usr/bin/perl + + use Lingua::EN::Numericalize; + + print str2nbr('First, Second, Third, Twentythird');


    It prints '123' not what I would want to print which is:
    '1st, 2nd, 3rd, 23rd'
    It seems as if this problem is still a bit stickier. At least the package is a step in the right direction.

    Thank you -- Akira the humble
      There is a easy way to remedy this...
      for my $text (qw/first third thirteenth twentyfirst twentysecond oneh +undredone onehundredtwelve onehundredeleven/) { my $number = str2nbr($text); my $sufix = 'th'; if ( $number =~ /^1$/ or $number =~ /[^1]1$/ ) { $sufix = 'st'; } if ( $number =~ /^2$/ or $number =~ /[^1]2$/ ) { $sufix = 'nd'; } if ( $number =~ /^3$/ or $number =~ /[^1]3$/ ) { $sufix = 'rd'; } print $number . $sufix, $/; }
      and i get 1st, 3rd, 13th, 21st, 22nd, 101st, 112th, 111th I believe this will work for all your cases, i would test it more though. ED: changed a var name, didn't like it after i looked at again
Re: Spelled out numbers to numerical equivalent function
by Akira71 (Scribe) on Apr 12, 2005 at 19:17 UTC
    Monks, Both of those are excellent suggestions. I am going to see what works best. I prefer the roll your solution as I am having a bit of fun with this, but as usual I am hampered by the time to get something done. I am frustrated at the requirements for this billing system, but they all are frustrating on different things.

    Thank you all very much.

    Akira
Re: Spelled out numbers to numerical equivalent function
by gam3 (Curate) on Apr 12, 2005 at 19:06 UTC
    Or roll your own.
    use strict; my $hash = { 'first' => '1st', 'second' => '2nd', 'third' => '3rd', 'twentythird' => '23rd', }; sub ordinal_t2n { my @ret; for (@_) { my $b = "$_"; $b =~ s/(\w+)/$hash->{lc($1)} or $1/eg; push @ret, $b; } @ret; } print ordinal_t2n('First, Second, Third, Twentythird', 'First, Second, Three is not here Third, Twentythird +'),"\n";
    -- gam3
    A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://447053]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-25 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found