3 # gpinyin - European-like Chinese writing 'pinyin' into 'groff'
5 # Source file position: <groff-source>/contrib/gpinyin/gpinyin.pl
6 # Installed position: <prefix>/bin/gpinyin
8 # Copyright (C) 2014-2018 Free Software Foundation, Inc.
10 # Written by Bernd Warken <groff-bernd.warken-72@web.de>.
12 # This file is part of 'gpinyin', which is part of 'groff'.
14 # 'groff' is free software; you can redistribute it and/or modify it
15 # under the terms of the GNU General Public License as published by
16 # the Free Software Foundation, either version 2 of the License, or
17 # (at your option) any later version.
19 # 'groff' is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You can find a copy of the GNU General Public License in the internet
25 # at <http://www.gnu.org/licenses/gpl-2.0.html>.
27 ########################################################################
29 ########################################################################
30 # All Pinyin syllables from wikipedia
31 ########################################################################
35 'a' => 1, 'ai' => 1, 'an' => 1, 'ang' => 1, 'ao' => 1,
36 'ba' => 1, 'bai' => 1, 'ban' => 1, 'bang' => 1, 'bao' => 1,
37 'bei' => 1, 'ben' => 1, 'beng' => 1,
38 'bi' => 1, 'bian' => 1, 'biao' => 1, 'bie' => 1, 'bin' => 1,
39 'bing' => 1, 'bo' => 1, 'bu' => 1,
40 'ca' => 1, 'cai' => 1, 'can' => 1, 'cang' => 1, 'cao' => 1,
41 'ce' => 1, 'cen' => 1, 'ceng' => 1,
42 'cha' => 1, 'chai' => 1, 'chan' => 1, 'chang' => 1, 'chao' => 1,
43 'che' => 1, 'chen' => 1, 'cheng' => 1, 'chi' => 1,
44 'chong' => 1, 'chou' => 1, 'chu' => 1,
45 'chua' => 1, 'chuai' => 1, 'chuan' => 1, 'chuang' => 1,
46 'chui' => 1, 'chun' => 1, 'chuo' => 1,
47 'ci' => 1, 'cong' => 1, 'cou' => 1,
48 'cu' => 1, 'cuan' => 1, 'cui' => 1, 'cun' => 1, 'cuo' => 1,
49 'da' => 1, 'dai' => 1, 'dan' => 1, 'dang' => 1, 'dao' => 1,
50 'de' => 1, 'dei' => 1, 'den' => 1, 'deng' => 1,
51 'di' => 1, 'dian' => 1, 'diao' => 1, 'die' => 1,
52 'ding' => 1, 'diu' => 1, 'dong' => 1, 'dou' => 1,
53 'du' => 1, 'duan' => 1, 'dui' => 1, 'dun' => 1, 'duo' => 1,
54 'e' => 1, 'ei' => 1, 'en' => 1, 'eng' => 1, 'er' => 1,
55 'fa' => 1, 'fan' => 1, 'fang' => 1,
56 'fei' => 1, 'fen' => 1, 'feng' => 1, 'fiao' => 1,
57 'fo' => 1, 'fou' => 1, 'fu' => 1,
58 'ga' => 1, 'gai' => 1, 'gan' => 1, 'gang' => 1, 'gao' => 1,
59 'ge' => 1, 'gei' => 1, 'gen' => 1, 'geng' => 1,
60 'gong' => 1, 'gou' => 1, 'gu' => 1,
61 'gua' => 1, 'guai' => 1, 'guan' => 1, 'guang' => 1, 'gui' => 1,
62 'gun' => 1, 'guo' => 1,
63 'ha' => 1, 'hai' => 1, 'han' => 1, 'hang' => 1, 'hao' => 1,
64 'he' => 1, 'hei' => 1, 'hen' => 1, 'heng' => 1,
65 'hong' => 1, 'hou' => 1,
66 'hu' => 1, 'hua' => 1, 'huai' => 1, 'huan' => 1, 'huang' => 1,
67 'hui' => 1, 'hun' => 1, 'huo' => 1,
68 'ji' => 1, 'jia' => 1, 'jian' => 1, 'jiang' => 1, 'jiao' => 1,
69 'jie' => 1, 'jin' => 1, 'jing' => 1, 'jiong' => 1, 'jiu' => 1,
70 'ju' => 1, 'juan' => 1, 'jue' => 1, 'jun' => 1,
71 'ka' => 1, 'kai' => 1, 'kan' => 1, 'kang' => 1, 'kao' => 1,
72 'ke' => 1, 'kei' => 1, 'ken' => 1, 'keng' => 1,
73 'kong' => 1, 'kou' => 1, 'ku' => 1, 'kua' => 1, 'kuai' => 1,
74 'kuan' => 1, 'kuang' => 1, 'kui' => 1, 'kun' => 1, 'kuo' => 1,
75 'la' => 1, 'lai' => 1, 'lan' => 1, 'lang' => 1, 'lao' => 1,
76 'le' => 1, 'lei' => 1, 'leng' => 1,
77 'li' => 1, 'lia' => 1, 'lian' => 1, 'liang' => 1, 'liao' => 1,
78 'lie' => 1, 'lin' => 1, 'ling' => 1, 'liu' => 1,
79 'lo' => 1, 'long' => 1, 'lou' => 1,
80 'lu' => 1, 'luan' => 1, 'lun' => 1, 'luo' => 1,
81 'lü' => 1, 'lüe' => 1,
82 'ma' => 1, 'mai' => 1, 'man' => 1, 'mang' => 1, 'mao' => 1,
83 'me' => 1, 'mei' => 1, 'men' => 1, 'meng' => 1,
84 'mi' => 1, 'mian' => 1, 'miao' => 1, 'mie' => 1,
85 'min' => 1, 'ming' => 1, 'miu' => 1,
86 'mo' => 1, 'mou' => 1, 'mu' => 1,
87 'na' => 1, 'nai' => 1, 'nan' => 1, 'nang' => 1, 'nao' => 1,
88 'ne' => 1, 'nei' => 1, 'nen' => 1, 'neng' => 1,
89 'ni' => 1, 'nian' => 1, 'niang' => 1, 'niao' => 1, 'nie' => 1,
90 'nin' => 1, 'ning' => 1, 'niu' => 1, 'nong' => 1, 'nou' => 1,
91 'nu' => 1, 'nuan' => 1, 'nun' => 1, 'nuo' => 1,
92 'nü' => 1, 'nüe' => 1,
93 'o' => 1, 'ong' => 1, 'ou' => 1,
94 'pa' => 1, 'pai' => 1, 'pan' => 1, 'pang' => 1, 'pao' => 1,
95 'pei' => 1, 'pen' => 1, 'peng' => 1,
96 'pi' => 1, 'pian' => 1, 'piao' => 1, 'pie' => 1, 'pin' => 1,
97 'ping' => 1, 'po' => 1, 'pou' => 1, 'pu' => 1,
98 'qi' => 1, 'qia' => 1, 'qian' => 1, 'qiang' => 1, 'qiao' => 1, 'qie' => 1,
99 'qin' => 1, 'qing' => 1, 'qiong' => 1, 'qiu' => 1,
100 'qu' => 1, 'quan' => 1, 'que' => 1, 'qun' => 1,
101 'ran' => 1, 'rang' => 1, 'rao' => 1, 're' => 1, 'ren' => 1,
102 'ri' => 1, 'rong' => 1, 'rou' => 1,
103 'ru' => 1, 'ruan' => 1, 'rui' => 1, 'run' => 1, 'ruo' => 1,
104 'sa' => 1, 'sai' => 1, 'san' => 1, 'sang' => 1, 'sao' => 1,
105 'se' => 1, 'sen' => 1, 'seng' => 1,
106 'sha' => 1, 'shai' => 1, 'shan' => 1, 'shang' => 1, 'shao' => 1,
107 'she' => 1, 'shei' => 1, 'shen' => 1, 'sheng' => 1, 'shi' => 1,
108 'shou' => 1, 'shu' => 1, 'shua' => 1, 'shuai' => 1, 'shuan' => 1,
109 'shuang' => 1, 'shui' => 1, 'shun' => 1, 'shuo' => 1,
110 'si' => 1, 'song' => 1, 'sou' => 1, 'su' => 1, 'suan' => 1, 'sui' => 1,
111 'sun' => 1, 'suo' => 1,
112 'ta' => 1, 'tai' => 1, 'tan' => 1, 'tang' => 1, 'tao' => 1,
113 'te' => 1, 'teng' => 1,
114 'ti' => 1, 'tian' => 1, 'tiao' => 1, 'tie' => 1, 'ting' => 1,
115 'tong' => 1, 'tou' => 1,
116 'tu' => 1, 'tuan' => 1, 'tui' => 1, 'tun' => 1, 'tuo' => 1,
117 'wa' => 1, 'wai' => 1, 'wan' => 1, 'wang' => 1,
118 'wei' => 1, 'wen' => 1, 'weng' => 1, 'wo' => 1, 'wu' => 1,
119 'xi' => 1, 'xia' => 1, 'xian' => 1, 'xiang' => 1, 'xiao' => 1,
120 'xie' => 1, 'xin' => 1, 'xing' => 1, 'xiong' => 1, 'xiu' => 1,
121 'xu' => 1, 'xuan' => 1, 'xue' => 1, 'xun' => 1,
122 'ya' => 1, 'yai' => 1, 'yan' => 1, 'yang' => 1, 'yao' => 1,
123 'ye' => 1, 'yi' => 1, 'yin' => 1, 'ying' => 1,
124 'yo' => 1, 'yong' => 1, 'you' => 1,
125 'yu' => 1, 'yuan' => 1, 'yue' => 1, 'yun' => 1,
126 'za' => 1, 'zai' => 1, 'zan' => 1, 'zang' => 1, 'zao' => 1,
127 'ze' => 1, 'zei' => 1, 'zen' => 1, 'zeng' => 1,
128 'zha' => 1, 'zhai' => 1, 'zhan' => 1, 'zhang' => 1, 'zhao' => 1,
129 'zhe' => 1, 'zhei' => 1, 'zhen' => 1, 'zheng' => 1, 'zhi' => 1,
130 'zhong' => 1, 'zhou' => 1, 'zhu' => 1, 'zhua' => 1, 'zhuai' => 1,
131 'zhuan' => 1, 'zhuang' => 1, 'zhui' => 1, 'zhun' => 1, 'zhuo' => 1,
132 'zi' => 1, 'zong' => 1, 'zou' => 1, 'zu' => 1, 'zuan' => 1,
133 'zui' => 1, 'zun' => 1, 'zuo' => 1,
136 ########################################################################
137 # Unicode variables for utf8 tty (nroff)
138 ########################################################################
205 ########################################################################
206 # glyph variables for troff
207 ########################################################################
209 #my $tone1_macron = '\\[a-]';
210 #my $tone2_acute = '\\[aa]';
211 #my $tone3_caron = '\\[ah]';
212 #my $tone4_grave = '\\[ga]';
213 my @accents = ( '', '\\[a-]', '\\[aa]', '\\[ah]', '\\[ga]', );
245 ########################################################################
247 ########################################################################
249 # Pinyin consists of syllables with a final number to be translated
250 # into an accent. Such numbered syllables are combined into words.
251 # Such words can have a final punctuation. A line is a collection of
258 ########################################################################
266 ########################################################################
268 my $starting_blanks = shift;
271 #&err('handle_line start: ' . $line);
273 my %outline = ( 'n' => $starting_blanks, 't' => $starting_blanks, );
275 # transform to Ü only for inside of Perl
285 # transform to ü only for inside of Perl
293 $line =~ s/U[eE]/Ü/g;
294 $line =~ s/u[eE]/ü/g;
296 $line =~ s/\\\(aq/'/g; # \(aq is an apostrophe
297 $line =~ s/\\\[aq\]/'/g; # \[aq] is an apostrophe
298 $line =~ s/^[']//; # remove leading apostrophe
299 $line =~ s/[']$//; # remove final apostrophe
300 $line =~ s/['][']+/'/g; # combine apostrophe groups
301 $line =~ s/([0-4])'/$1/;
302 $line =~ s/([^0-4])'/${1}0/;
304 my @words = split /\s+/, $line;
308 for my $word ( @words ) {
309 #&err('handle_line word: ' . $word);
311 next unless ( $word );
313 # this is a word, maybe composed of several syllables
314 my $punctuation = $1 if ( $word =~ s/([,.?!:;]*)$// );
315 # '$word' is now without punctuation
317 my %outword = &handle_word($word);
318 next unless ( %outword );
320 for my $roff ( @roffs ) {
321 #&err('handle_line roff ' . $roff . ': ' . $outword{$roff});
323 # combine words to line
324 next unless ( $outword{$roff} );
327 $outline{$roff} .= ' ' if ( $outline{$roff} );
329 $outline{$roff} .= $outword{$roff};
330 $outline{$roff} .= $punctuation;
333 #for my $roff ( @roffs ) {
334 #&err('handle_line end ' . $roff . ': ' . $outline{$roff});
340 ########################################################################
343 #&err('handle_word start: ' . $word);
345 $word =~ s/5/0/g; # transform 5 to 0
346 $word =~ s/([^0-4])$/${1}0/; # add lacking final no-tone
348 # remove apostrophes with tone
353 # replace apostrophes without tone by 0
360 # detect wrong tone numbers
361 if ( $word =~ s/[5-9]/0/g ) {
362 &err('word ' . $word . ': wrong tone number ' . $1);
366 $word =~ s/[']//g; # remove apostrophes
368 # remove starting apostrophe or number
369 $word =~ s/^(['0-4])+//;
371 # add 0 for final no-tone
372 $word .= '0' if ( $word =~ /[^0-4]$/ );
374 if ( $word =~ /^[0-9]/ ) { # word starts with number
375 print 'word: ' . $word . ' starts with tone number';
376 $word =~ s/^[0-9]+//;
378 #&err('handle_word 0: ' . $word);
382 my %outword = ( 'n' => '', 't' => '', );
384 # split word into syllables
385 while ( $word =~ /^[a-zA-ZüÜ']/ ) {
386 $word =~ s/^([a-zA-ZüÜ']+)([0-4])//;
389 #err('handle_word split: ' . $syll . ' ' . $tone);
390 my %outsyll = &handle_syll( $syll, $tone );
391 next unless ( %outsyll );
392 for my $roff ( @roffs ) {
393 my $out = $outsyll{$roff};
394 $out = '\\[aq]' . $out if ( $out && $out =~ /^[aeo]/ );
395 $outword{$roff} .= $out;
396 #&err('handle_word ' . $roff . ': ' . $outword{$roff});
403 ########################################################################
407 #&err( 'handle_syll start: ' . $syll . ' ' . $tone);
409 my $lower_case = lc($syll);
410 $lower_case =~ s/Ü/ü/g;
411 unless ( exists($syllables{$lower_case}) ) {
412 err('The syllable ' . $syll . ' is not a Chinese syllable.');
416 my %outsyll = ( 'n' => '', 't' => '', );
418 if ( $tone == 0 ) { # no accent
419 # use u umlaut without accent
420 $syll =~ s/Ü/\\[:U]/g;
421 $syll =~ s/ü/\\[:u]/g;
423 for my $roff ( @roffs ) {
424 $outsyll{$roff} = $syll;
425 #&err('handle_syll 0 outsyll ' . $roff . ': ' . $outsyll{$roff});
443 &err( 'Syllable ' . $syll . ' does not have vowels' );
448 my $vowels_before = '';
450 my $vowels_after = '';
454 # find vowel for accent
455 if ( $vowels =~ /^[aeiouAEIOU]$/ ) { # only 1 vowel
456 #&err('handle_syll single vowel ' . $vowels);
458 } elsif ( $vowels eq 'ü' ) {
460 } elsif ( $vowels eq 'Ü' ) {
462 } elsif ( $vowels =~ /^([^aeAE]*)([aeAE])(.*)$/ ) { # a, A, e or E
466 } elsif ( $vowels =~ /^([^oO]*)(oO)(.*)$/ ) { # o or O
470 } elsif ( $vowels =~ /^(\w)(\w)(.*)$/ ) { # take 2nd vowel
475 &err( 'Unknown vowels: ' . $vowels . ' in syllable: ' . $syll );
479 # unless ( $vowel =~ /^[aeiouAEIOU]$/ ) {
480 # print STDERR q(The argument ') . $vowel . q(' is not a vowel!);
486 $outsyll{'n'} = &vowel_n($vowel, $tone);
487 $outsyll{'t'} = &vowel_t($vowel, $tone);
489 for my $roff ( @roffs ) {
490 $outsyll{$roff} = $initial . $vowels_before .
491 $outsyll{$roff} . $vowels_after . $final;
492 #&err('handle_syll out ' . $roff . ': ' . $outsyll{$roff});
499 ########################################################################
500 sub vowel_n { # Unicode for nroff
503 #&err('vowel_n: ' . $vowel . ' ' . $tone);
505 return '' unless ( $vowel );
507 if ( $tone == 1 ) { # macron
508 $vowel = $tones1_Unicode{$vowel};
509 } elsif ( $tone == 2 ) { # acute
510 $vowel = $tones2_Unicode{$vowel};
511 } elsif ( $tone == 3 ) { # caron
512 $vowel = $tones3_Unicode{$vowel};
513 } elsif ( $tone == 4 ) { # grave
514 $vowel = $tones4_Unicode{$vowel};
520 ########################################################################
521 sub vowel_t { # named glyphs for troff
524 #&err( 'vowel_t: ' . $vowel . ' ' . $tone);
526 return '' unless ( $vowel );
528 # \o'\s-2\[:u]\s0\[a-]'
529 if ( $vowel =~ /[üÜ]/ ) {
531 $vowel = q(\\o'\\s-) . $smaller . q(\\[:u]\\s0) .
532 $accents[$tone] . q(');
536 $vowel = q(\\[.i]) if ( $vowel eq 'i' );
538 if ( $tone == 1 ) { # macron
539 $vowel = q(\\o') . $vowel . $accents[$tone] . q(');
540 } elsif ( $tone == 2 ) { # acute
541 $vowel = $tones2_glyphs{$vowel};
542 } elsif ( $tone == 3 ) { # caron
543 $vowel = q(\\o') . $vowel . $accents[$tone] . q(');
544 } elsif ( $tone == 4 ) { # grave
545 $vowel = $tones4_glyphs{$vowel};
551 ########################################################################
552 sub finish_pinyin_mode {
559 for ( @$n ) { # Unicode for nroff
563 for ( @$t ) { # glyphs for troff
568 } # finish_pinyin_mode()
572 ########################################################################