| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8; -*- | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | package Lingua::CAT::Word2Num; | ||||||||||||||||||||||
| 4 | # ABSTRACT: Word to number conversion in Catalan | ||||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | 1 | ▶ | use 5.16.0; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 1 | ▶ | use utf8; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | # {{{ use block | ||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 1 | ▶ | use Parse::RecDescent; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | 1 | ▶ | use Export::Attrs; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | # }}} | ||||||||||||||||||||||
| 16 | # {{{ var block | ||||||||||||||||||||||
| 17 | 1 | our $VERSION = '0.2603300'; | |||||||||||||||||||||
| 18 | 1 | my $parser = cat_numerals(); | |||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | # }}} | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | # {{{ w2n convert text to number | ||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | 2 | ▶ | sub w2n :Export { | ||||||||||||||||||||
|
Subroutine
w2n: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 25 | 2 | ▶ | my $input = shift // return; | ||||||||||||||||||||
|
Condition100%2/2
shift() // (return)
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | 1 | $input = lc $input; | |||||||||||||||||||||
| 28 | 1 | $input =~ s/\s+/ /g; | |||||||||||||||||||||
| 29 | 1 | $input =~ s/^\s+|\s+$//g; | |||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | 1 | return $parser->numeral($input); | |||||||||||||||||||||
| 32 | 1 | ▶ | } | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 33 | # }}} | ||||||||||||||||||||||
| 34 | # {{{ cat_numerals create parser for numerals | ||||||||||||||||||||||
| 35 | |||||||||||||||||||||||
| 36 | 1 | ▶ | sub cat_numerals { | ||||||||||||||||||||
|
Subroutine
cat_numerals: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 37 | 1 | return Parse::RecDescent->new(q{ | |||||||||||||||||||||
| 38 | <autoaction: { $item[1] } > | ||||||||||||||||||||||
| 39 | <nocheck> | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | numeral: mega | ||||||||||||||||||||||
| 42 | | kOhOd | ||||||||||||||||||||||
| 43 | | 'zero' { 0 } | ||||||||||||||||||||||
| 44 | | { } | ||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | number: 'un' { 1 } | ||||||||||||||||||||||
| 47 | | 'una' { 1 } | ||||||||||||||||||||||
| 48 | | 'dos' { 2 } | ||||||||||||||||||||||
| 49 | | 'dues' { 2 } | ||||||||||||||||||||||
| 50 | | 'tres' { 3 } | ||||||||||||||||||||||
| 51 | | 'quatre' { 4 } | ||||||||||||||||||||||
| 52 | | 'cinc' { 5 } | ||||||||||||||||||||||
| 53 | | 'sis' { 6 } | ||||||||||||||||||||||
| 54 | | 'vuit' { 8 } | ||||||||||||||||||||||
| 55 | | 'nou' { 9 } | ||||||||||||||||||||||
| 56 | | 'deu' { 10 } | ||||||||||||||||||||||
| 57 | | 'onze' { 11 } | ||||||||||||||||||||||
| 58 | | 'dotze' { 12 } | ||||||||||||||||||||||
| 59 | | 'tretze' { 13 } | ||||||||||||||||||||||
| 60 | | 'catorze' { 14 } | ||||||||||||||||||||||
| 61 | | 'quinze' { 15 } | ||||||||||||||||||||||
| 62 | | 'setze' { 16 } | ||||||||||||||||||||||
| 63 | | 'disset' { 17 } | ||||||||||||||||||||||
| 64 | | 'divuit' { 18 } | ||||||||||||||||||||||
| 65 | | 'dinou' { 19 } | ||||||||||||||||||||||
| 66 | | 'set' { 7 } | ||||||||||||||||||||||
| 67 | | 'vint-i-un' { 21 } | ||||||||||||||||||||||
| 68 | | 'vint-i-una' { 21 } | ||||||||||||||||||||||
| 69 | | 'vint-i-dos' { 22 } | ||||||||||||||||||||||
| 70 | | 'vint-i-dues' { 22 } | ||||||||||||||||||||||
| 71 | | 'vint-i-tres' { 23 } | ||||||||||||||||||||||
| 72 | | 'vint-i-quatre' { 24 } | ||||||||||||||||||||||
| 73 | | 'vint-i-cinc' { 25 } | ||||||||||||||||||||||
| 74 | | 'vint-i-sis' { 26 } | ||||||||||||||||||||||
| 75 | | 'vint-i-set' { 27 } | ||||||||||||||||||||||
| 76 | | 'vint-i-vuit' { 28 } | ||||||||||||||||||||||
| 77 | | 'vint-i-nou' { 29 } | ||||||||||||||||||||||
| 78 | | 'vint' { 20 } | ||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | tens: 'trenta' { 30 } | ||||||||||||||||||||||
| 81 | | 'quaranta' { 40 } | ||||||||||||||||||||||
| 82 | | 'cinquanta' { 50 } | ||||||||||||||||||||||
| 83 | | 'seixanta' { 60 } | ||||||||||||||||||||||
| 84 | | 'setanta' { 70 } | ||||||||||||||||||||||
| 85 | | 'vuitanta' { 80 } | ||||||||||||||||||||||
| 86 | | 'noranta' { 90 } | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | hundreds: number /\-?cents?/ { $item[1] * 100 } | ||||||||||||||||||||||
| 89 | | /cents?/ { 100 } | ||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | deca: tens /-/ number { $item[1] + $item[3] } | ||||||||||||||||||||||
| 92 | | tens | ||||||||||||||||||||||
| 93 | | number | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | hecto: hundreds deca { $item[1] + $item[2] } | ||||||||||||||||||||||
| 96 | | hundreds | ||||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | hOd: hecto | ||||||||||||||||||||||
| 99 | | deca | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | kilo: hOd milnotmeg hOd { $item[1] * 1000 + $item[3] } | ||||||||||||||||||||||
| 102 | | hOd milnotmeg { $item[1] * 1000 } | ||||||||||||||||||||||
| 103 | | milnotmeg hOd { 1000 + $item[2] } | ||||||||||||||||||||||
| 104 | | milnotmeg { 1000 } | ||||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | milnotmeg: ...!'mili' 'mil' | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | kOhOd: kilo | ||||||||||||||||||||||
| 109 | | hOd | ||||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | mega: hOd /mili(ons|ó)/ kOhOd { $item[1] * 1_000_000 + $item[3] } | ||||||||||||||||||||||
| 112 | | hOd /mili(ons|ó)/ { $item[1] * 1_000_000 } | ||||||||||||||||||||||
| 113 | }); | ||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||
| 115 | |||||||||||||||||||||||
| 116 | # }}} | ||||||||||||||||||||||
| 117 | # {{{ ordinal2cardinal convert ordinal text to cardinal text | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | 0 | ▶ | sub ordinal2cardinal :Export { | ||||||||||||||||||||
|
Subroutine
ordinal2cardinal: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||
| 120 | 0 | ▶ | my $input = shift // return; | ||||||||||||||||||||
|
Condition0%0/2
shift() // (return)
MC/DC0%0/1
shift() // (return)
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | # Catalan ordinals 1-10 are fully irregular | ||||||||||||||||||||||
| 123 | # Masculine (-è / special) and feminine (-a) forms | ||||||||||||||||||||||
| 124 | 0 | state $irregular = { | |||||||||||||||||||||
| 125 | 'primer' => 'un', 'primera' => 'un', | ||||||||||||||||||||||
| 126 | 'segon' => 'dos', 'segona' => 'dos', | ||||||||||||||||||||||
| 127 | 'tercer' => 'tres', 'tercera' => 'tres', | ||||||||||||||||||||||
| 128 | 'quart' => 'quatre', 'quarta' => 'quatre', | ||||||||||||||||||||||
| 129 | 'cinquè' => 'cinc', 'cinquena' => 'cinc', | ||||||||||||||||||||||
| 130 | 'sisè' => 'sis', 'sisena' => 'sis', | ||||||||||||||||||||||
| 131 | 'setè' => 'set', 'setena' => 'set', | ||||||||||||||||||||||
| 132 | 'vuitè' => 'vuit', 'vuitena' => 'vuit', | ||||||||||||||||||||||
| 133 | 'novè' => 'nou', 'novena' => 'nou', | ||||||||||||||||||||||
| 134 | 'desè' => 'deu', 'desena' => 'deu', | ||||||||||||||||||||||
| 135 | }; | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | 0 | ▶ | return $irregular->{$input} if exists $irregular->{$input}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | # Regular (11+): cardinal stem + "è" (masc) or "ena" (fem) | ||||||||||||||||||||||
| 140 | # Try feminine first (longer suffix) | ||||||||||||||||||||||
| 141 | 0 | ▶ | if ($input =~ s{ena\z}{}xms) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 142 | 0 | _cat_restore_vowel(\$input); | |||||||||||||||||||||
| 143 | 0 | return $input; | |||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||
| 145 | |||||||||||||||||||||||
| 146 | # Masculine: strip "è" | ||||||||||||||||||||||
| 147 | 0 | ▶ | $input =~ s{è\z}{}xms or return; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | 0 | _cat_restore_vowel(\$input); | |||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | 0 | return $input; | |||||||||||||||||||||
| 152 | 1 | ▶ | } | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | # }}} | ||||||||||||||||||||||
| 155 | # {{{ _cat_restore_vowel restore dropped vowel on stem | ||||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | 0 | ▶ | sub _cat_restore_vowel { | ||||||||||||||||||||
|
Subroutine
_cat_restore_vowel: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||
| 158 | 0 | my $ref = shift; | |||||||||||||||||||||
| 159 | |||||||||||||||||||||||
| 160 | # Catalan drops the final vowel before ordinal suffixes. The dropped | ||||||||||||||||||||||
| 161 | # vowel varies by word, so we restore it based on the stem ending. | ||||||||||||||||||||||
| 162 | |||||||||||||||||||||||
| 163 | # nou family: dino→dinou, vint-i-no→vint-i-nou (stem ends in "o" vowel → need "u") | ||||||||||||||||||||||
| 164 | 0 | ▶ | if ($$ref =~ m{no\z}xms) { $$ref .= 'u' } | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 165 | # quatre: vint-i-quatr→vint-i-quatre | ||||||||||||||||||||||
| 166 | 0 | elsif ($$ref =~ m{tr\z}xms) { $$ref .= 'e' } | |||||||||||||||||||||
| 167 | # decades (trenta, quaranta, etc.): trent→trenta, quarant→quaranta | ||||||||||||||||||||||
| 168 | 0 | elsif ($$ref =~ m{[ae]nt\z}xms) { $$ref .= 'a' } | |||||||||||||||||||||
| 169 | # teens ending in -z: onz→onze, dotz→dotze, tretz→tretze, etc. | ||||||||||||||||||||||
| 170 | 0 | elsif ($$ref =~ m{z\z}xms) { $$ref .= 'e' } | |||||||||||||||||||||
| 171 | |||||||||||||||||||||||
| 172 | 0 | return; | |||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||
| 174 | |||||||||||||||||||||||
| 175 | # }}} | ||||||||||||||||||||||
| 176 | |||||||||||||||||||||||
| 177 | 1 | 1; | |||||||||||||||||||||
| 178 | |||||||||||||||||||||||
| 179 | __END__ | ||||||||||||||||||||||