| 1 | # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8; -*- | ||||||||||||
| 2 | |||||||||||||
| 3 | package Lingua::BUL::Word2Num; | ||||||||||||
| 4 | # ABSTRACT: Word to number conversion in Bulgarian | ||||||||||||
| 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 = bul_numerals(); | |||||||||||
| 20 | |||||||||||||
| 21 | # }}} | ||||||||||||
| 22 | |||||||||||||
| 23 | # {{{ w2n convert text to number | ||||||||||||
| 24 | |||||||||||||
| 25 | sub w2n :Export { | ||||||||||||
| 26 | 2 | ▶ | 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 | 1 | $input .= " "; # Grant end space before normalizing | |||||||||||
| 29 | |||||||||||||
| 30 | 1 | $input =~ s/хиляди /хиляда /g; # Thousand variations. Normalize to хиляда | |||||||||||
| 31 | 1 | $input =~ s/милиона /милион /g; # Million variations. Normalize to милион | |||||||||||
| 32 | |||||||||||||
| 33 | 1 | return $parser->numeral($input); | |||||||||||
| 34 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | |||||||||||||
| 36 | # }}} | ||||||||||||
| 37 | # {{{ bul_numerals create parser for numerals | ||||||||||||
| 38 | |||||||||||||
| 39 | sub bul_numerals { | ||||||||||||
| 40 | 1 | ▶ | return Parse::RecDescent->new(q{ | ||||||||||
|
Subroutine
bul_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 | connector: 'и ' # Bulgarian "и" (and) connector | ||||||||||||
| 91 | |||||||||||||
| 92 | decade: tens 'и ' number # tens и units (e.g. двадесет и три) | ||||||||||||
| 93 | { $return = $item[1] + $item[3]; } | ||||||||||||
| 94 | | tens # plain tens (e.g. петдесет) | ||||||||||||
| 95 | { $return = $item[1]; } | ||||||||||||
| 96 | | number # plain number 0-19 | ||||||||||||
| 97 | { $return = $item[1]; } | ||||||||||||
| 98 | |||||||||||||
| 99 | century: hundreds 'и ' decade(?) # hundreds "и" decade (for remainder < 20 or tens only) | ||||||||||||
| 100 | { $return = 0; | ||||||||||||
| 101 | for (@item) { | ||||||||||||
| 102 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 103 | $return += $$_[0]; | ||||||||||||
| 104 | } elsif (!ref $_ && $_ =~ /^\d+$/) { | ||||||||||||
| 105 | $return += $_; | ||||||||||||
| 106 | } | ||||||||||||
| 107 | } | ||||||||||||
| 108 | $return ||= undef; | ||||||||||||
| 109 | } | ||||||||||||
| 110 | | hundreds tens 'и ' number # hundreds tens "и" units | ||||||||||||
| 111 | { $return = 0; | ||||||||||||
| 112 | for (@item) { | ||||||||||||
| 113 | if (ref $_ && defined $$_[0]) { | ||||||||||||
| 114 | $return += $$_[0]; | ||||||||||||
| 115 | } elsif (!ref $_ && $_ =~ /^\d+$/) { | ||||||||||||
| 116 | $return += $_; | ||||||||||||
| 117 | } | ||||||||||||
| 118 | } | ||||||||||||
| 119 | $return ||= undef; | ||||||||||||
| 120 | } | ||||||||||||
| 121 | | hundreds # plain hundreds (e.g. двеста) | ||||||||||||
| 122 | { $return = $item[1]; } | ||||||||||||
| 123 | |||||||||||||
| 124 | millenium: century(?) decade(?) 'хиляда ' connector(?) century(?) decade(?) # try to find words that represents values | ||||||||||||
| 125 | { $return = 0; # from 1.000 to 999.999 | ||||||||||||
| 126 | for (@item) { | ||||||||||||
| 127 | if (ref $_ && defined $$_[0] && $$_[0] =~ /^\d+$/) { | ||||||||||||
| 128 | $return += $$_[0]; | ||||||||||||
| 129 | } elsif ($_ eq "хиляда ") { | ||||||||||||
| 130 | $return = ($return>0) ? $return * 1000 : 1000; | ||||||||||||
| 131 | } | ||||||||||||
| 132 | } | ||||||||||||
| 133 | $return ||= undef; | ||||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | million: century(?) decade(?) # try to find words that represents values | ||||||||||||
| 137 | 'милион ' # from 1.000.000 to 999.999.999 | ||||||||||||
| 138 | millenium(?) connector(?) century(?) decade(?) | ||||||||||||
| 139 | { $return = 0; | ||||||||||||
| 140 | for (@item) { | ||||||||||||
| 141 | if (ref $_ && defined $$_[0] && $$_[0] =~ /^\d+$/) { | ||||||||||||
| 142 | $return += $$_[0]; | ||||||||||||
| 143 | } elsif ($_ eq "милион ") { | ||||||||||||
| 144 | $return = ($return>0) ? $return * 1000000 : 1000000; | ||||||||||||
| 145 | } | ||||||||||||
| 146 | } | ||||||||||||
| 147 | $return ||= undef; | ||||||||||||
| 148 | } | ||||||||||||
| 149 | }); | ||||||||||||
| 150 | } | ||||||||||||
| 151 | |||||||||||||
| 152 | # }}} | ||||||||||||
| 153 | |||||||||||||
| 154 | 1 | 1; | |||||||||||
| 155 | |||||||||||||
| 156 | __END__ | ||||||||||||