| 1 | #!/usr/bin/perl -- | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package Lingua::NL::Numbers::GroeneBoekje; | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | # Numbers in words, according to the dutch "Groene Boekje". | ||||||||||||||||||||
| 6 | # http://woordenlijst.org/leidraad/6/9/ | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Lingua::NL::Numbers::GroeneBoekje - Convert numeric values into their Dutch equivalents | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | use Lingua::NL::Numbers::GroeneBoekje; | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | my $converter = Lingua::NL::Numbers::GroeneBoekje->new; | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | my $text = $converter->parse(124); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # prints 'honderdvierentwintig' | ||||||||||||||||||||
| 21 | print $text; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | Lingua::NL::Numbers::GroeneBoekje converts numeric values to their | ||||||||||||||||||||
| 26 | Dutch written equivalent, according to the guidelines as defined in | ||||||||||||||||||||
| 27 | the "Groene Boekje". For example: | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | twee | ||||||||||||||||||||
| 30 | twintig | ||||||||||||||||||||
| 31 | tweeëntwintig | ||||||||||||||||||||
| 32 | tweehonderd | ||||||||||||||||||||
| 33 | tweehonderdtweeëntwintig | ||||||||||||||||||||
| 34 | tweeëntwintighonderd | ||||||||||||||||||||
| 35 | tweeduizend tweehonderdtwintig | ||||||||||||||||||||
| 36 | twee miljoen tweehonderdtwintigduizend tweehonderdtweeëntwintig | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =head1 KNOWN BUGS | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | Estimated upper bound is 10**18 - 1, but this is not checked. | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | When "twee" is glued to "en" it will get a dieraesis on the "e" of "en". | ||||||||||||||||||||
| 43 | The result will be a Perl Unicode string. | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | When "drie" is glued to "en" it will get a dieraesis on the "e" of "en". | ||||||||||||||||||||
| 46 | The result will be a Perl Unicode string. | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =head1 AUTHOR | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | Johan Vromans E<lt>jvromans@squirrel.nlE<gt>. | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head1 SEE ALSO | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | L<Lingua::NL::Numbers> | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | http://woordenlijst.org/leidraad/6/9/ | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 2 | $VERSION = "0.10"; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||
| 66 | 2 | my ($pkg) = @_; | |||||||||||||||||||
| 67 | 2 | ▶ | $pkg = ref($pkg) || $pkg; | ||||||||||||||||||
|
Condition33%1/3
ref $pkg || $pkg
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg
$pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
| |||||||||||||||||||||
| 68 | 2 | bless \(my $o), $pkg; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 36 | ▶ | sub parse { | ||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 72 | 36 | my ($self, $n) = @_; | |||||||||||||||||||
| 73 | 36 | _num_($n); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 2 | ▶ | use constant HOUNDRED => "honderd"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | 2 | ▶ | use constant THOUSAND => "duizend"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 78 | 2 | ▶ | use constant TEN6TH => "miljoen"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | 2 | ▶ | use constant TEN9TH => "miljard"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 2 | my @units = | |||||||||||||||||||
| 82 | ( qw(nul een twee drie vier | ||||||||||||||||||||
| 83 | vijf zes zeven acht negen), | ||||||||||||||||||||
| 84 | qw(tien elf twaalf dertien veertien | ||||||||||||||||||||
| 85 | vijftien zestien zeventien achttien negentien) ); | ||||||||||||||||||||
| 86 | 2 | my @tens = | |||||||||||||||||||
| 87 | ( qw(nul tien twintig dertig veertig | ||||||||||||||||||||
| 88 | vijftig zestig zeventig tachtig negentig) ); | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | # Regel 6.N | ||||||||||||||||||||
| 91 | # We schrijven een getal in één woord, tot en met het woord duizend. | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 83 | ▶ | sub _num_ { | ||||||||||||||||||
|
Subroutine
_num_: called CC=14 SCAR=26.4
| |||||||||||||||||||||
| 94 | 83 | my ($n) = @_; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # Prefab numbers. | ||||||||||||||||||||
| 97 | 83 | ▶ | return $units[$n] if $n < @units; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | # Small numbers, up to 99. | ||||||||||||||||||||
| 100 | 53 | ▶ | if ( $n < 100 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 18 | my $t = int($n / 10); | |||||||||||||||||||
| 102 | 18 | my $res = ""; | |||||||||||||||||||
| 103 | 18 | ▶ | if ( my $r = $n % 10 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 12 | $res = $units[$r] . "en"; | |||||||||||||||||||
| 105 | 12 | $res =~ s/([ie])ee/$1."eë"/e; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | 18 | return $res.$tens[$t]; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # Up to 1_999. | ||||||||||||||||||||
| 111 | 35 | ▶ | if ( $n < 1999 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 22 | my $t = int($n / 100); | |||||||||||||||||||
| 113 | 22 | my $r = $n % 100; | |||||||||||||||||||
| 114 | 22 | my $res; | |||||||||||||||||||
| 115 | 22 | ▶ | if ( $t % 10 == 0 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 5 | ▶ | $res = ($t > 10 ? _num_($t/10) : "") . THOUSAND; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | # Regel 6.N (vervolg) | ||||||||||||||||||||
| 118 | # Na het woord duizend volgt een spatie. | ||||||||||||||||||||
| 119 | 5 | ▶ | $res .= " " if $r; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | else { | ||||||||||||||||||||
| 122 | 17 | ▶ | $res = ($t > 1 ? _num_($t) : "") . HOUNDRED; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | 22 | ▶ | $res .= _num_($r) if $r; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 22 | return $res; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # Up to 999_999. | ||||||||||||||||||||
| 129 | 13 | ▶ | if ( $n < 1000000 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 10 | my $t = int($n / 1000); | |||||||||||||||||||
| 131 | 10 | my $r = $n % 1000; | |||||||||||||||||||
| 132 | 10 | ▶ | my $res = ($t > 1 ? _num_($t) : "") . THOUSAND; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | # Regel 6.N (vervolg) | ||||||||||||||||||||
| 134 | # Na het woord duizend volgt een spatie. | ||||||||||||||||||||
| 135 | 10 | ▶ | $res .= " " . _num_($r) if $r; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 10 | return $res; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | # THe higher ones. | ||||||||||||||||||||
| 140 | 3 | ▶ | return _very_high($n, 1000000, TEN6TH) if $n < 1000000000; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 1 | return _very_high($n, 1000000000, TEN9TH); | |||||||||||||||||||
| 142 | 0 | "ontzettend veel"; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | # Regel 6.N (vervolg) | ||||||||||||||||||||
| 146 | # De woorden miljoen, miljard, biljoen enz. schrijven we los. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 3 | ▶ | sub _very_high { | ||||||||||||||||||
|
Subroutine
_very_high: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 149 | 3 | my ($n, $m, $w) = @_; | |||||||||||||||||||
| 150 | 3 | my $t = int($n / $m); | |||||||||||||||||||
| 151 | 3 | my $r = $n % $m; | |||||||||||||||||||
| 152 | 3 | my $res = _num_($t) . " " . $w; | |||||||||||||||||||
| 153 | 3 | ▶ | $res .= " " . _num_($r) if $r; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 3 | return $res; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 2 | ▶ | unless ( caller ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | package main; | ||||||||||||||||||||
| 159 | 0 | foreach ( @ARGV ) { | |||||||||||||||||||
| 160 | 0 | printf("%8d: %s\n", | |||||||||||||||||||
| 161 | $_, Lingua::NL::Numbers::GroeneBoekje->new->parse($_)); | ||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | } | ||||||||||||||||||||