| 1 | # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8; -*- | ||||||||||||
| 2 | |||||||||||||
| 3 | package Lingua::MKD::Word2Num; | ||||||||||||
| 4 | # ABSTRACT: Word to number conversion in Macedonian | ||||||||||||
| 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 Export::Attrs; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use Parse::RecDescent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | # }}} | ||||||||||||
| 17 | # {{{ variable declarations | ||||||||||||
| 18 | 1 | our $VERSION = '0.2603270'; | |||||||||||
| 19 | 1 | my $parser = mkd_numerals(); | |||||||||||
| 20 | |||||||||||||
| 21 | # }}} | ||||||||||||
| 22 | |||||||||||||
| 23 | # {{{ w2n convert text to number | ||||||||||||
| 24 | |||||||||||||
| 25 | sub w2n :Export { | ||||||||||||
| 26 | 31 | ▶ | my $input = shift // return; | ||||||||||
|
Subroutine
w2n: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
shift() // (return)
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 27 | |||||||||||||
| 28 | 30 | $input .= " "; # Grant end space before normalizing | |||||||||||
| 29 | |||||||||||||
| 30 | 30 | $input =~ s/илјади /илјада /g; # Thousand variations. Normalize to илјада | |||||||||||
| 31 | 30 | $input =~ s/милиони /милион /g; # Million variations. Normalize to милион | |||||||||||
| 32 | |||||||||||||
| 33 | 30 | return $parser->numeral($input); | |||||||||||
| 34 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | |||||||||||||
| 36 | # }}} | ||||||||||||
| 37 | # {{{ mkd_numerals create parser for numerals | ||||||||||||
| 38 | |||||||||||||
| 39 | sub mkd_numerals { | ||||||||||||
| 40 | 1 | ▶ | return Parse::RecDescent->new(q{ | ||||||||||
|
Subroutine
mkd_numerals: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 41 | numeral: <rulevar: local $number = 0> | ||||||||||||
| 42 | numeral: million { return $item[1]; } # root parse. go from maximum to minimum value | ||||||||||||
| 43 | | millenium { return $item[1]; } | ||||||||||||
| 44 | | century { return $item[1]; } | ||||||||||||
| 45 | | decade { return $item[1]; } | ||||||||||||
| 46 | | { return undef; } | ||||||||||||
| 47 | |||||||||||||
| 48 | number: 'деветнаесет ' { $return = 19; } # try to find a word from 0 to 19 | ||||||||||||
| 49 | | 'осумнаесет ' { $return = 18; } | ||||||||||||
| 50 | | 'седумнаесет ' { $return = 17; } | ||||||||||||
| 51 | | 'шестнаесет ' { $return = 16; } | ||||||||||||
| 52 | | 'петнаесет ' { $return = 15; } | ||||||||||||
| 53 | | 'четиринаесет ' { $return = 14; } | ||||||||||||
| 54 | | 'тринаесет ' { $return = 13; } | ||||||||||||
| 55 | | 'дванаесет ' { $return = 12; } | ||||||||||||
| 56 | | 'единаесет ' { $return = 11; } | ||||||||||||
| 57 | | 'десет ' { $return = 10; } | ||||||||||||
| 58 | | 'девет ' { $return = 9; } | ||||||||||||
| 59 | | 'осум ' { $return = 8; } | ||||||||||||
| 60 | | 'седум ' { $return = 7; } | ||||||||||||
| 61 | | 'шест ' { $return = 6; } | ||||||||||||
| 62 | | 'пет ' { $return = 5; } | ||||||||||||
| 63 | | 'четири ' { $return = 4; } | ||||||||||||
| 64 | | 'три ' { $return = 3; } | ||||||||||||
| 65 | | 'две ' { $return = 2; } | ||||||||||||
| 66 | | 'два ' { $return = 2; } | ||||||||||||
| 67 | | 'еден ' { $return = 1; } | ||||||||||||
| 68 | | 'една ' { $return = 1; } | ||||||||||||
| 69 | | 'нула ' { $return = 0; } | ||||||||||||
| 70 | |||||||||||||
| 71 | tens: 'дваесет ' { $return = 20; } # try to find a word that represents | ||||||||||||
| 72 | | 'триесет ' { $return = 30; } # values 20,30,..,90 | ||||||||||||
| 73 | | 'четириесет ' { $return = 40; } | ||||||||||||
| 74 | | 'педесет ' { $return = 50; } | ||||||||||||
| 75 | | 'шеесет ' { $return = 60; } | ||||||||||||
| 76 | | 'седумдесет ' { $return = 70; } | ||||||||||||
| 77 | | 'осумдесет ' { $return = 80; } | ||||||||||||
| 78 | | 'деведесет ' { $return = 90; } | ||||||||||||
| 79 | |||||||||||||
| 80 | hundreds: 'деветстотини ' { $return = 900; } # try to find a word that represents | ||||||||||||
| 81 | | 'осумстотини ' { $return = 800; } # values 100,200,..,900 | ||||||||||||
| 82 | | 'седумстотини ' { $return = 700; } | ||||||||||||
| 83 | | 'шестстотини ' { $return = 600; } | ||||||||||||
| 84 | | 'петстотини ' { $return = 500; } | ||||||||||||
| 85 | | 'четиристотини ' { $return = 400; } | ||||||||||||
| 86 | | 'триста ' { $return = 300; } | ||||||||||||
| 87 | | 'двесте ' { $return = 200; } | ||||||||||||
| 88 | | 'сто ' { $return = 100; } | ||||||||||||
| 89 | |||||||||||||
| 90 | decade: tens 'и ' number # tens и units (e.g. дваесет и три) | ||||||||||||
| 91 | { $return = $item[1] + $item[3]; } | ||||||||||||
| 92 | | tens # plain tens (e.g. педесет) | ||||||||||||
| 93 | { $return = $item[1]; } | ||||||||||||
| 94 | | number # plain number 0-19 | ||||||||||||
| 95 | { $return = $item[1]; } | ||||||||||||
| 96 | |||||||||||||
| 97 | century: hundreds 'и ' decade(?) # hundreds "и" decade (for remainder < 20 or tens only) | ||||||||||||
| 98 | { $return = 0; | ||||||||||||
| 99 | for (@item) { | ||||||||||||
| 100 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 101 | $return += $$_[0]; | ||||||||||||
| 102 | } elsif (!ref $_ && $_ =~ /^\d+$/) { | ||||||||||||
| 103 | $return += $_; | ||||||||||||
| 104 | } | ||||||||||||
| 105 | } | ||||||||||||
| 106 | $return ||= undef; | ||||||||||||
| 107 | } | ||||||||||||
| 108 | | hundreds tens 'и ' number # hundreds tens "и" units | ||||||||||||
| 109 | { $return = 0; | ||||||||||||
| 110 | for (@item) { | ||||||||||||
| 111 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 112 | $return += $$_[0]; | ||||||||||||
| 113 | } elsif (!ref $_ && $_ =~ /^\d+$/) { | ||||||||||||
| 114 | $return += $_; | ||||||||||||
| 115 | } | ||||||||||||
| 116 | } | ||||||||||||
| 117 | $return ||= undef; | ||||||||||||
| 118 | } | ||||||||||||
| 119 | | hundreds # plain hundreds (e.g. двесте) | ||||||||||||
| 120 | { $return = $item[1]; } | ||||||||||||
| 121 | |||||||||||||
| 122 | millenium: century(?) decade(?) 'илјада ' 'и ' century # thousand "и" hundred (e.g. илјада и сто) | ||||||||||||
| 123 | { $return = 0; | ||||||||||||
| 124 | for (@item) { | ||||||||||||
| 125 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 126 | $return += $$_[0]; | ||||||||||||
| 127 | } elsif (!ref $_ && $_ =~ /^\d+$/) { | ||||||||||||
| 128 | $return += $_; | ||||||||||||
| 129 | } elsif ($_ eq "\x{0438}\x{043b}\x{0458}\x{0430}\x{0434}\x{0430} ") { | ||||||||||||
| 130 | $return = ($return>0) ? $return * 1000 : 1000; | ||||||||||||
| 131 | } | ||||||||||||
| 132 | } | ||||||||||||
| 133 | $return ||= undef; | ||||||||||||
| 134 | } | ||||||||||||
| 135 | | century(?) decade(?) 'илјада ' 'и ' decade # thousand "и" remainder (e.g. илјада и еден) | ||||||||||||
| 136 | { $return = 0; | ||||||||||||
| 137 | for (@item) { | ||||||||||||
| 138 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 139 | $return += $$_[0]; | ||||||||||||
| 140 | } elsif (!ref $_ && $_ =~ /^\d+$/) { | ||||||||||||
| 141 | $return += $_; | ||||||||||||
| 142 | } elsif ($_ eq "\x{0438}\x{043b}\x{0458}\x{0430}\x{0434}\x{0430} ") { | ||||||||||||
| 143 | $return = ($return>0) ? $return * 1000 : 1000; | ||||||||||||
| 144 | } | ||||||||||||
| 145 | } | ||||||||||||
| 146 | $return ||= undef; | ||||||||||||
| 147 | } | ||||||||||||
| 148 | | century(?) decade(?) 'илјада ' century(?) decade(?) # thousand with hundreds remainder | ||||||||||||
| 149 | { $return = 0; | ||||||||||||
| 150 | for (@item) { | ||||||||||||
| 151 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 152 | $return += $$_[0]; | ||||||||||||
| 153 | } elsif ($_ eq "\x{0438}\x{043b}\x{0458}\x{0430}\x{0434}\x{0430} ") { | ||||||||||||
| 154 | $return = ($return>0) ? $return * 1000 : 1000; | ||||||||||||
| 155 | } | ||||||||||||
| 156 | } | ||||||||||||
| 157 | $return ||= undef; | ||||||||||||
| 158 | } | ||||||||||||
| 159 | |||||||||||||
| 160 | million: century(?) decade(?) # try to find words that represents values | ||||||||||||
| 161 | 'милион ' # from 1.000.000 to 999.999.999 | ||||||||||||
| 162 | millenium(?) century(?) decade(?) | ||||||||||||
| 163 | { $return = 0; | ||||||||||||
| 164 | for (@item) { | ||||||||||||
| 165 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 166 | $return += $$_[0]; | ||||||||||||
| 167 | } elsif ($_ eq "\x{043c}\x{0438}\x{043b}\x{0438}\x{043e}\x{043d} ") { | ||||||||||||
| 168 | $return = ($return>0) ? $return * 1000000 : 1000000; | ||||||||||||
| 169 | } | ||||||||||||
| 170 | } | ||||||||||||
| 171 | $return ||= undef; | ||||||||||||
| 172 | } | ||||||||||||
| 173 | }); | ||||||||||||
| 174 | } | ||||||||||||
| 175 | |||||||||||||
| 176 | # }}} | ||||||||||||
| 177 | |||||||||||||
| 178 | 1 | 1; | |||||||||||
| 179 | |||||||||||||
| 180 | __END__ | ||||||||||||