#!/usr/bin/perl # COnvertor of digits to text. # can handle digits with spaces, recognizes decimal COMMA and not point. # # Jan Cernocky # Thu May 20 21:05:23 CEST 1999 ####################################################################### ### define some useful things ### %basic=(0 => "nula", 1 => "jedna", 2 => "dvě", 3 => "tři", 4 => "čtyři", 5 => "pět", 6 => "šest", 7 => "sedm", 8 => "osm", 9 => "devět"); %teens=(10 => "deset", 11 => "jedenáct", 12 => "dvanáct", 13 => "třináct", 14 => "čtrnáct", 15 => "patnáct", 16 => "šestnáct", 17 => "sedmnáct", 18 => "osmnáct", 19 => "devatenáct"); %tens=(2 => "dvacet", 3 => "třicet", 4 => "čtyřicet", 5 => "padesát", 6 => "šedesát", 7 => "sedmdesát", 8 => "osmdesát", 9 => "devadesát"); ########################################################## ### do a function for the conversion of numbers < 1000 ### ########################################################## sub till1000 { my $nb = $_[0]; my $out = ""; ### first hundreds ### my $h = int($nb / 100); # print "hundreds=$h\n"; if ($h > 0) { $out="sto" if $h == 1; # was "jedno sto" $out=$basic{$h}." stě" if $h == 2; $out=$basic{$h}." sta" if $h == 3 || $h == 4; $out=$basic{$h}." set" if $h >= 5 && $h <= 9; } ### then tens, only > 2 ### my $rest = $nb - $h * 100; my $t = int ($rest / 10); # print "tens=$t\n"; if ($t >= 2) { $out=$out." ".$tens{$t}; } elsif ($t == 1) { # must handle teens and finish $out=$out." ".$teens{$rest}; return $out; } ### now ones ### my $o = $rest - $t * 10; # print "ones=$o\n"; if ($o > 0) { $out=$out." ".$basic{$o}; } ### no zero handling here !!! ### # elsif ($nb == 0) { # "zero" only if the whole nb is zero # $out=$basic{$o}; # } return $out; } #################################################### ### function for conversion of numbers < billion ### #################################################### sub tillbillion { my $nb = $_[0]; my $out = ""; ### millions ### my $strm=""; my $m = int($nb / 1000000); my $mil = ""; if ($m > 0) { $strm = till1000 ($m); #print "$strm\n"; if ($strm =~ s/jedna$/jeden/) { $mil="milión" }; if ($strm =~ s/dvě$/dva/) { $mil = "milióny" }; if ($strm =~ /tři$/ || $strm =~ /čtyři$/) { $mil = "milióny" }; $mil="miliónů" if $mil eq ""; } $out=$out.$strm." ".$mil if ($strm ne ""); ### thousands ### my $strt=""; my $rest = $nb - $m * 1000000; my $t = int($rest / 1000); my $thou = ""; if ($t > 0) { $strt = till1000 ($t); #print "$strt\n"; if ($strt =~ s/jedna$/jeden/) { $thou="tisíc" }; if ($strt =~ s/dvě$/dva/) { $thou = "tisíce" }; if ($strt =~ /tři$/ || $strt =~ /čtyři$/) { $thou = "tisíce" }; $thou="tisíc" if $thou eq ""; } $out=$out." ".$strt." ".$thou if ($strt ne ""); ### and the rest ### $rest=$rest - $t * 1000; my $o = int($rest); if ($o > 0) { my $stro = till1000 ($o); $out = $out." ".$stro; } elsif ($nb == 0) { $out = "nula" }; # otherwise NOTHING ! return $out; } ############################################################### ###### frac numbers, needs also int part for cela, cele,... ### ############################################################### # the function needs nonempty fractional part ! sub csfrac { my $frac=$_[0]; my $integ=$_[1]; my $sinteg=$_[2]; # integer part as string ### convert the fractional part and add the word ### $lfrac=length($frac); die "Sorry, can handle only 1/1000000's\n" if $lfrac > 6; my $sfrac = tillbillion($frac); my $fracword = ""; if ($sfrac =~ /jedna$/) { $fracword=("desetina","setina","tisícina", "desetitisícina","stotisícina","milióntina")[$lfrac-1]; } if ($sfrac =~ /dvě$/ || $sfrac =~ /tři$/ || $sfrac =~ /čtyři$/ ) { $fracword=("desetiny","setiny","tisíciny", "desetitisíciny","stotisíciny","milióntiny")[$lfrac-1]; } if ($fracword eq "") { $fracword=("desetin","setin","tisícin", "desetitisícin","stotisícin","milióntin")[$lfrac-1]; } # print "sfrac=`$sfrac' fracword=`$fracword'\n"; ### make celych, cele, etc ### my $cel = ""; die "Dva should not be in integer part if frac part present !\n" if $sinteg =~ /dva$/; if ($sinteg =~ /jedna$/ || $sinteg =~ /nula$/ ) { $cel="celá"; } if ($sinteg =~ /dvě$/ || $sinteg =~ /tři$/ || $sinteg =~ /čtyři$/ ) { $cel="celé"; } if ($cel eq "") { $cel = "celých"; } ### put all that together ### my $out = $cel." ".$sfrac." ".$fracword; return $out; } ################################################################## ########### the main prog, very simple ########################### ################################################################## while ($line=<>) { chomp $line; $line =~ s/ //g; ## replace spaces ($minteg, $mfrac) = split /,/,$line; ## divide to whole and frac part # print "converting minteg=`$minteg' mfrac=`$mfrac'\n"; ### convert integer ### $sminteg=tillbillion ($minteg); # if frac part not present, convert dve -> dva at the end of string if (!defined($mfrac)) { $sminteg =~ s/dvě$/dva/; } # print "$minteg ---> $sminteg\n"; $mainout=$sminteg; ### convert fractional ### if (defined($mfrac)) { $smfrac = csfrac($mfrac, $minteg, $sminteg); $mainout = $mainout." ".$smfrac; } ### delete mult. blanks, blanks at the end, blanks at the beg, ### and print it out ### $mainout =~ s/ +/ /g; $mainout =~ s/ $//; $mainout =~ s/^ //; # print "$line -> $mainout\n"; print "$mainout\n"; }