-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyalify
executable file
·207 lines (189 loc) · 6.62 KB
/
yalify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/perl
# vi: set sw=3 ai sm:
# Written on May 12, 2021 by Ambrose Li for project 116
# Last update May 13, 2021
# © 2021 by Ambrose Li
# This script takes a Jyutping pronunciation on the command line
# and converts it to the corresponding Yale spelling. The Yale
# spelling is output to standard output.
# Only numerals for tones are currently supported; prefixed
# spacing diacritics will produce incorrect results.
# Reference:
# https://en.wikipedia.org/wiki/Yale_romanization_of_Cantonese
# see https://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default for these crazy hacks
# THERE ARE MORE CRAZY HACKS THERE FOR MORE COMPLEX PROJECTS!!
#use v5.14; # //u modifier
use utf8;
use open qw( :encoding(UTF-8) :std );
use charnames qw( :full :short );
use feature "unicode_strings";
use Encode qw( encode decode );
@ARGV = map { decode("UTF-8", $_) } @ARGV if grep /\P{ASCII}/ => @ARGV;
use strict;
use integer;
use Getopt::Long;
use vars qw( $use_dotted_i_p $use_macrons_p );
$use_dotted_i_p = 0;
$use_macrons_p = 1;
use vars qw( $debug_p $verbose_p );
use vars qw( $appName );
$appName = $1 if $0 =~ /([^\/]+$)/;
use vars qw( %initials %finals );
%initials = (
'' => '',
'b' => 'b',
'p' => 'p',
'm' => 'm',
'f' => 'f',
'd' => 'd',
't' => 't',
'n' => 'n',
'l' => 'l',
'g' => 'g',
'k' => 'k',
'ng' => 'ng',
'h' => 'h',
'gw' => 'gw',
'kw' => 'kw',
'w' => 'w',
'z' => 'j',
'c' => 'ch',
's' => 's',
'j' => 'y',
);
%finals = (
'aa' => 'a',
'aai' => 'aai',
'aau' => 'aau',
'aam' => 'aam',
'aan' => 'aan',
'aang' => 'aang',
'aap' => 'aap',
'aat' => 'aat',
'aak' => 'aak',
'ai' => 'ai',
'au' => 'au',
'am' => 'am',
'an' => 'an',
'ang' => 'ang',
'ap' => 'ap',
'at' => 'at',
'ak' => 'ak',
'e' => 'e',
'ei', => 'ei',
'eng' => 'eng',
'ek' => 'ek',
'i' => 'i',
'iu' => 'iu',
'im' => 'im',
'in' => 'in',
'ing' => 'ing',
'ip' => 'ip',
'it' => 'it',
'ik' => 'ik',
'o' => 'o',
'oi' => 'oi',
'ou' => 'ou',
'on' => 'on',
'ong' => 'ong',
'ot' => 'ot',
'ok' => 'ok',
'u' => 'u',
'ui' => 'ui',
'un' => 'un',
'ung' => 'ung',
'ut' => 'ut',
'uk' => 'uk',
'oe' => 'eu',
'eoi' => 'eui',
'eon' => 'eun',
'oeng' => 'eung',
'eot' => 'eut',
'oek' => 'euk',
'yu' => 'yu',
'yun' => 'yun',
'yut' => 'yut',
'm' => 'm',
'ng' => 'ng',
);
sub yalify_syllable_real ($$$$$$) {
my($initial_consonant, $root, $tone, $punctuation, $capped_p, $all_caps_p) = @_;
my($initial_vowel, $other_vowels, $final) = ($1, $2, $3) if $root =~ /^(ng|\w)([aeiou]*)(.*)/;
my $mod1 = ($tone == 1 && $use_macrons_p)? "\x{304}": ($tone == 2 || $tone == 5)? "\x{301}": ($tone == 1 || $tone == 4)? "\x{300}": '';
my $mod2 = 'h' if $tone > 3;
my $it;
if ($use_dotted_i_p && (!$tone || $tone == 3 || $tone == 6) && ($all_caps_p)) {
$initial_vowel =~ s/(?<=i)/\x{307}/g;
}
$it .= $initial_consonant if !($initial_consonant eq 'y' && $initial_vowel =~ /^y/);
$it .= $1 . $mod1 . $2 . $other_vowels . $mod2 . $final . $punctuation if $initial_vowel =~ /^(\w)(\w*)$/;
$it = ucfirst $it if $capped_p;
$it = uc $it if $all_caps_p;
printf STDERR "[(%s) (%s) %s (%s) %s %s] => (%s)\n", $initial_consonant, $root, ($tone? $tone: 'null'), $punctuation, ($capped_p? 'true': 'false'), ($all_caps_p? 'true': 'false'), $it if $verbose_p;
return $it;
}
sub yalify_syllable ($) {
my($s) = @_;
my $it;
my $all_caps_p = 1 if $s eq uc $s;
my $capped_p = 1 if $s eq ucfirst lc $s;
my $re_part_1 = '((?!ng(?:\d|$))[bcdfghjklmnpstwz]+)?([a-z]+)';
my $re_part_2 = '(\W+)?$';
my($initial, $final, $tone, $punctuation);
if ($s =~ /^$re_part_1(?:¹|₁|⁷|₇|7)$re_part_2/i && defined $initials{$1} && defined $finals{$2}) {
($initial, $final, $tone, $punctuation) = (lc $initials{$1}, lc $finals{$2}, 1, $3);
} elsif ($s =~ /^$re_part_1(?:²|₂)$re_part_2/i && defined $initials{$1} && defined $finals{$2}) {
($initial, $final, $tone, $punctuation) = (lc $initials{$1}, lc $finals{$2}, 2, $3);
} elsif ($s =~ /^$re_part_1(?:³|₃|⁸|₈|8)$re_part_2/i && defined $initials{$1} && defined $finals{$2}) {
($initial, $final, $tone, $punctuation) = (lc $initials{$1}, lc $finals{$2}, 3, $3);
} elsif ($s =~ /^$re_part_1(?:⁴|₄)$re_part_2/i && defined $initials{$1} && defined $finals{$2}) {
($initial, $final, $tone, $punctuation) = (lc $initials{$1}, lc $finals{$2}, 4, $3);
} elsif ($s =~ /^$re_part_1(?:⁵|₅)$re_part_2/i && defined $initials{$1} && defined $finals{$2}) {
($initial, $final, $tone, $punctuation) = (lc $initials{$1}, lc $finals{$2}, 5, $3);
} elsif ($s =~ /^$re_part_1(?:⁶|₆|⁹|₉|9)$re_part_2/i && defined $initials{$1} && defined $finals{$2}) {
($initial, $final, $tone, $punctuation) = (lc $initials{$1}, lc $finals{$2}, 6, $3);
} elsif ($s =~ /^$re_part_1([1-6])?$re_part_2/i && defined $initials{$1} && defined $finals{$2}) {
($initial, $final, $tone, $punctuation) = (lc $initials{$1}, lc $finals{$2}, $3, $4);
} else {
$it = $s;
}
$it = yalify_syllable_real($initial, $final, $tone, $punctuation, $capped_p, $all_caps_p) unless defined $it;
return $it;
}
sub yalify ($) {
my($s) = @_;
my $it;
for my $t (split(/\s+|(?<=\d)(?=[a-zA-Z])|(?<=¹|²|³|⁴|⁵|⁶|⁷|⁸|⁹|₁|₂|₃|₄|₅|₆|₇|₈|₉)(?=[a-zA-Z])/, $s)) {
$it .= ' ' if defined $it;
$it .= yalify_syllable($t);
}
return $it;
}
sub show_usage (;$) {
my($status) = @_;
my $h = $status? *STDERR: *STDOUT;
print $h <<EOF;
Usage: $appName [OPTION]... JYUTPING...
Converts Jyutping to Yale spellings.
--disable-dotted-i always use regular i (default)
--disable-macrons use grave accent for first tone
--enable-dotted-i use dotted capital i for third and sixth tones
--enable-macrons use macron for first tone (default)
-v, --verbose explain what is being done
--help display this help and exit
Jyutping spellings can be given with or without tones, but tones must
be given in regular numerals. Superscripted or subscripted numerals
should also work, but prefixed diacritics are not supported.
Report bugs to ambrose.li\@gmail.com.
EOF
exit $status;
}
GetOptions(
'disable-dotted-i|without-dotted-i' => sub { $use_dotted_i_p = 0; },
'disable-macrons|without-macrons' => sub { $use_macrons_p = 0; },
'enable-dotted-i|with-dotted-i' => \$use_dotted_i_p,
'enable-macrons|with-macrons' => \$use_macrons_p,
'v|verbose' => \$verbose_p,
'help' => \&show_usage,
) || exit(1);
printf "%s\n", yalify(join(' ', @ARGV));