| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# For Emacs: -*- mode:cperl; eval: (folding-mode 1) -*- |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Lingua::Word2Num; |
|
4
|
|
|
|
|
|
|
# ABSTRACT: A wrapper for Lingua:XXX::word2num modules Lingua::Word2Num is a module for converting texts in their spoken language representation into numbers. |
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
128088
|
use 5.16.0; |
|
|
1
|
|
|
|
|
5
|
|
|
7
|
1
|
|
|
1
|
|
7
|
use utf8; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
17
|
|
|
8
|
1
|
|
|
1
|
|
31
|
use warnings; |
|
|
1
|
|
|
|
|
14
|
|
|
|
1
|
|
|
|
|
68
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# {{{ use block |
|
11
|
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
97
|
|
|
13
|
1
|
|
|
1
|
|
721
|
use Export::Attrs; |
|
|
1
|
|
|
|
|
12910
|
|
|
|
1
|
|
|
|
|
9
|
|
|
14
|
1
|
|
|
1
|
|
998
|
use Readonly; |
|
|
1
|
|
|
|
|
5271
|
|
|
|
1
|
|
|
|
|
914
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# }}} |
|
17
|
|
|
|
|
|
|
# {{{ var block |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2004-present'; |
|
20
|
|
|
|
|
|
|
our $VERSION = '0.2603250'; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# templates for functional and object interface |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $template_func = 'use __PACKAGE_WITH_VERSION__ ();'."\n". |
|
25
|
|
|
|
|
|
|
'$result = __PACKAGE__::__FUNCTION__($word);'."\n"; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $template_obj = 'use __PACKAGE_WITH_VERSION__ ();'."\n". |
|
28
|
|
|
|
|
|
|
'my $tmp_obj = new __PACKAGE__;'."\n". |
|
29
|
|
|
|
|
|
|
'$result = $tmp_obj->__FUNCTION__("$word");'."\n"; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# {{{ ISO 639-1 to 639-3 mapping |
|
32
|
|
|
|
|
|
|
my %iso1_to_3 = ( |
|
33
|
|
|
|
|
|
|
af => 'afr', ar => 'ara', bg => 'bul', ca => 'cat', |
|
34
|
|
|
|
|
|
|
cs => 'ces', da => 'dan', de => 'deu', el => 'ell', |
|
35
|
|
|
|
|
|
|
en => 'eng', es => 'spa', eu => 'eus', fi => 'fin', |
|
36
|
|
|
|
|
|
|
fr => 'fra', hi => 'hin', hr => 'hrv', hu => 'hun', |
|
37
|
|
|
|
|
|
|
id => 'ind', it => 'ita', ja => 'jpn', ko => 'kor', |
|
38
|
|
|
|
|
|
|
lt => 'lit', nl => 'nld', 'no' => 'nor', pl => 'pol', |
|
39
|
|
|
|
|
|
|
pt => 'por', ro => 'ron', ru => 'rus', sk => 'slk', |
|
40
|
|
|
|
|
|
|
sv => 'swe', sw => 'swa', th => 'tha', tr => 'tur', |
|
41
|
|
|
|
|
|
|
uk => 'ukr', vi => 'vie', zh => 'zho', |
|
42
|
|
|
|
|
|
|
); |
|
43
|
|
|
|
|
|
|
# }}} |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# {{{ %known — auto-discovered from lib/Lingua/*/Word2Num.pm |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# Override table for legacy modules with non-standard API |
|
48
|
|
|
|
|
|
|
my %w2n_override = ( |
|
49
|
|
|
|
|
|
|
ind => { package => 'Words2Nums', function => 'words2nums' }, |
|
50
|
|
|
|
|
|
|
por => { package => 'Words2Nums', function => 'word2num' }, |
|
51
|
|
|
|
|
|
|
); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
our %known; |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# auto-discover: scan for Lingua::*::Word2Num modules |
|
56
|
|
|
|
|
|
|
{ |
|
57
|
|
|
|
|
|
|
my $lingua_dir; |
|
58
|
|
|
|
|
|
|
for my $inc (@INC) { |
|
59
|
|
|
|
|
|
|
my $try = "$inc/Lingua"; |
|
60
|
|
|
|
|
|
|
if (-d $try) { $lingua_dir = $try; last; } |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
if ($lingua_dir) { |
|
64
|
|
|
|
|
|
|
for my $dir (glob "$lingua_dir/*/") { |
|
65
|
|
|
|
|
|
|
my ($lang) = $dir =~ m{/([A-Z]{3})/\z}; |
|
66
|
|
|
|
|
|
|
next unless $lang; |
|
67
|
|
|
|
|
|
|
$lang = lc $lang; |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
if (-e "$dir/Word2Num.pm" || exists $w2n_override{$lang}) { |
|
70
|
|
|
|
|
|
|
my $ov = $w2n_override{$lang} // {}; |
|
71
|
|
|
|
|
|
|
$known{$lang} = { |
|
72
|
|
|
|
|
|
|
package => $ov->{package} // 'Word2Num', |
|
73
|
|
|
|
|
|
|
version => $ov->{version} // '', |
|
74
|
|
|
|
|
|
|
function => $ov->{function} // 'w2n', |
|
75
|
|
|
|
|
|
|
code => $ov->{code} // $template_func, |
|
76
|
|
|
|
|
|
|
}; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# }}} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# {{{ new constructor |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub new { |
|
87
|
2
|
|
|
2
|
1
|
7
|
return bless {}, shift; |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# }}} |
|
91
|
|
|
|
|
|
|
# {{{ cardinal convert text to number |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub cardinal :Export { |
|
94
|
2
|
50
|
|
2
|
1
|
1460
|
my $self = ref($_[0]) ? shift : __PACKAGE__->new(); |
|
95
|
2
|
|
|
|
|
6
|
my $result = ''; |
|
96
|
|
|
|
|
|
|
|
|
97
|
2
|
|
50
|
|
|
16
|
my $lang = shift // return $result; |
|
98
|
0
|
|
0
|
|
|
0
|
my $word = shift // return $result; |
|
99
|
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
0
|
$lang = lc $lang; |
|
101
|
0
|
0
|
|
|
|
0
|
$lang = $iso1_to_3{$lang} if exists $iso1_to_3{$lang}; |
|
102
|
|
|
|
|
|
|
|
|
103
|
0
|
0
|
|
|
|
0
|
my @langs = ($lang eq '*') |
|
104
|
|
|
|
|
|
|
? sort keys %known |
|
105
|
|
|
|
|
|
|
: ($lang) |
|
106
|
|
|
|
|
|
|
; |
|
107
|
|
|
|
|
|
|
|
|
108
|
0
|
0
|
|
|
|
0
|
return $result if (!defined $known{$langs[0]}); |
|
109
|
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
0
|
for my $lang (@langs) { |
|
111
|
0
|
|
|
|
|
0
|
eval $self->preprocess_code($lang); ## no critic 'eval' |
|
112
|
0
|
0
|
|
|
|
0
|
carp $@ if $@; |
|
113
|
|
|
|
|
|
|
|
|
114
|
0
|
0
|
0
|
|
|
0
|
return $result if (defined $result && $result ne ''); |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
0
|
return ''; |
|
118
|
1
|
|
|
1
|
|
16
|
} |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
9
|
|
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# }}} |
|
121
|
|
|
|
|
|
|
# {{{ known_langs list of currently supported languages |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub known_langs :Export { |
|
124
|
0
|
|
|
0
|
1
|
0
|
return [sort keys %known]; |
|
125
|
1
|
|
|
1
|
|
392
|
} |
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
6
|
|
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# }}} |
|
128
|
|
|
|
|
|
|
# {{{ preprocess_code prepare code for evaluation |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub preprocess_code { |
|
131
|
2
|
|
|
2
|
1
|
231878
|
my $self = shift; |
|
132
|
2
|
|
100
|
|
|
25
|
my $lang = shift // return; |
|
133
|
|
|
|
|
|
|
|
|
134
|
1
|
50
|
|
|
|
13
|
return if !defined $known{$lang}; |
|
135
|
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
my $result = $known{$lang}{code}; |
|
137
|
0
|
|
|
|
|
|
my $pkg_name = 'Lingua::' . uc($lang) . '::' . $known{$lang}{package}; |
|
138
|
0
|
0
|
|
|
|
|
my $pkg_name_with_version = $known{$lang}{version} ne '' |
|
139
|
|
|
|
|
|
|
? "$pkg_name $known{$lang}{version}" : $pkg_name; |
|
140
|
0
|
|
|
|
|
|
my $function = $known{$lang}{function}; |
|
141
|
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
$result =~ s/__PACKAGE_WITH_VERSION__/$pkg_name_with_version/g; |
|
143
|
0
|
|
|
|
|
|
$result =~ s/__PACKAGE__/$pkg_name/g; |
|
144
|
0
|
|
|
|
|
|
$result =~ s/__FUNCTION__/$function/g; |
|
145
|
0
|
|
|
|
|
|
$result =~ s/__CHARSET__/$known{$lang}{charset}/g; |
|
146
|
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
return $result; |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# }}} |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
1; |
|
153
|
|
|
|
|
|
|
__END__ |