1: package pronounce_number;
2:
3: # Package Stuff.
4: use Exporter;
5: @ISA=(Exporter);
6: @EXPORT=('pronounce_number', 'set_format');
7:
8: my @v = ( [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
9: 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'],
10: [ '', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ]);
11: my @m = ('', ' million', ' billion', ' trillion', ' quadrillion', ' quintillion');
12: my @g = ('', ' thousand');
13: my @h=('',' hundred');
14: my @and=('',' and ');
15:
16: my %format = ( space=>'', point=>'point');
17:
18: sub set_format {
19: %format = @_;
20: }
21:
22: sub integer_grammar($) {
23: my ($u,$t,$h) = reverse(split//,$_[0]);
24: return $v[0]->[$h].$h[!!$h].$and[!!($h&&($t||$u))].$v[!(1==$t&&($u+=10)&&($t=0))]->[$t].$format{'space'}.$v[0]->[$u];
25: }
26:
27: sub float_grammar($) {
28: $_[0]=~s/(\d)/$v[0]->[$1] /geo;
29: return $_[0];
30: }
31:
32: sub magnitude_grammar {
33: return $g[(($_[0]-1)/3)%2].$m[$_[0]/6 * !!($_[0]%6)].' ';
34: }
35:
36: sub pronounce_integer($) {
37:
38: $_=join("",(reverse(split//,$_[0])));
39: my ($i,$n,@l)=(0,"",());
40: foreach $n(/(\d{1,3})/g) {
41: $n=join("",(reverse(split//,$n)));
42: $i+=length($n);
43: push @l,($n && defined(@l))?magnitude_grammar($i):"",integer_grammar($n);
44: }
45: return join('',reverse(@l));
46: }
47:
48: sub pronounce_float($) {
49: my @n=split/\./,$_[0];
50: return pronounce_integer($n[0])." $format{'point'} ".float_grammar($n[1]);
51: }
52:
53: sub pronounce_ip($) {
54: return join(" $format{'point'} ",map{pronounce_integer($_)}(split/\./,$_[0]));
55: }
56:
57: my @process_array=(\&pronounce_integer,\&pronounce_float,\&pronounce_ip);
58:
59: sub pronounce_number($) {
60: my $string = $_[0];
61: $string =~ s/,//g;
62: $_ = ($string =~ s/\./\./g);
63: return &{$process_array[($_<=$#process_array)?$_:2]}($string)
64: }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(ar0n) Re: Grammatical Pronunciation of numbers.
by ar0n (Priest) on Apr 11, 2001 at 21:49 UTC | |
|
Re: Grammatical Pronunciation of numbers.
by cLive ;-) (Prior) on Apr 18, 2001 at 09:53 UTC | |
by frankus (Priest) on Apr 18, 2001 at 13:06 UTC |