-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipa.pl
237 lines (220 loc) · 4.77 KB
/
ipa.pl
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/perl
# takes plain text in Irish on standard input and outputs
# a phonetic transcription; designed to be piped into filiocht.pl
#
# TODO: last resort transcriptions: look up in English CMU dictionary,
# use Caighdeánaitheoir spelling rules, or just implement default
# grapheme to phoneme rules
use strict;
use warnings;
use utf8;
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";
if ($#ARGV != 0) {
die "Usage: $0 FP.txt\n";
}
my $ipafile = $ARGV[0];
my %ipa;
my @prefixes;
my @suffixes;
my $prefixrule;
my $suffixrule;
my $total=0;
my $found=0;
open(DICT, "<:utf8", $ipafile) or die "Could not open FP.txt: $!";
while (<DICT>) {
chomp;
(my $w, my $p) = m/^([^\t]+)\t([^\t]+)$/;
$ipa{$w} = $p;
}
close DICT;
open(PREF, "<:utf8", 'reimir.csv') or die "Could not open reimir.csv: $!";
while (<PREF>) {
chomp;
next if (/^#/);
my @r = split(/,/);
push @prefixes, \@r;
}
close PREF;
open(SUFF, "<:utf8", 'iarmhir.csv') or die "Could not open iarmhir.csv: $!";
while (<SUFF>) {
chomp;
next if (/^#/);
my @r = split(/,/);
push @suffixes, \@r;
}
close SUFF;
sub strip_prefixes {
(my $t) = @_;
for my $rule (@prefixes) {
my $tomatch = $rule->[1];
if ($t =~ m/^$tomatch/) {
my $repl = $rule->[0];
$t =~ s/^$tomatch/$repl/;
$prefixrule = $rule;
return $t;
}
}
return $t;
}
# unlike prefixes fn above, only return modified word if it's in the ipa hash
sub strip_suffixes {
(my $t) = @_;
for my $rule (@suffixes) {
my $tomatch = $rule->[1];
if ($t =~ m/$tomatch$/) {
my $repl = $rule->[0];
my $ans = $t;
$ans =~ s/$tomatch$/$repl/;
if (exists($ipa{$ans}) and $ipa{$ans} =~ m/$rule->[2]$/) {
$suffixrule = $rule;
return $ans;
}
}
}
return $t;
}
sub apply_ipa_rules {
(my $ipa) = @_;
if (defined($prefixrule)) {
my $tomatch = $prefixrule->[2];
my $repl = $prefixrule->[3];
$repl = '' if ($repl eq '0');
$ipa =~ s/^$tomatch/$repl/;
}
if (defined($suffixrule)) {
my $tomatch = $suffixrule->[2];
my $repl = $suffixrule->[3];
$repl = '' if ($repl eq '0');
$ipa =~ s/$tomatch$/$repl/;
}
return $ipa;
}
sub toipa {
(my $t) = @_;
my $orig = $t;
my $ans = '';
if (exists($ipa{$t})) {
$ans = $ipa{$t};
$found++;
}
else {
$prefixrule = undef;
$suffixrule = undef;
$t = strip_prefixes($t);
if (exists($ipa{$t})) {
$ans = apply_ipa_rules($ipa{$t});
$found++;
}
else {
$t = strip_suffixes($t);
if (exists($ipa{$t})) {
$ans = apply_ipa_rules($ipa{$t});
$found++;
}
else {
$ans = "<$orig>";
#$ans = "NO: $t (orig = $orig)";
}
}
}
return $ans;
}
# argument is a non-negative int ([0-9]+)
sub numtoipa {
(my $t, my $with_object_p) = @_;
my $ans = '';
my $milte = int($t/1000);
if ($milte > 0) {
my $word = 'míle';
$word = 'mhíle' if ($milte % 10 < 7);
$ans .= numtoipa($milte,1)."\n" if ($milte > 1);
$ans .= toipa($word);
}
$t = $t % 1000;
my $ceadta = int($t/100);
if ($ceadta > 0) {
$ans .= "\n" if ($ans ne '');
my $word = 'gcéad';
$word = 'chéad' if ($ceadta % 10 < 7);
$word = 'céad' if ($ceadta % 10 == 1);
$ans .= numtoipa($ceadta,1)."\n" if ($ceadta > 1);
$ans .= toipa($word);
}
$t = $t % 100;
my $deicheanna = int($t/10);
if ($deicheanna > 1) {
$ans .= "\n" if ($ans ne '');
my @w1 = ('','','fiche','tríocha','daichead','caoga','seasca','seachtó','ochtó','nócha');
$ans .= toipa($w1[$deicheanna]);
}
$t = $t % 10;
my @w2 = ('','haon','dó','trí','ceathair','cúig','sé','seacht','hocht','naoi','deich');
my @w3 = ('','','dhá','trí','ceithre','cúig','sé','seacht','ocht','naoi','deich');
if ($t == 0 and $deicheanna == 1) {
$t = 10;
$deicheanna = 0;
}
if ($t > 0) {
$ans .= "\n" if ($ans ne '');
if ($with_object_p) {
$ans .= toipa($w3[$t]);
}
else {
$ans .= toipa('a')."\n".toipa($w2[$t]);
}
if ($deicheanna==1) {
my $word = 'déag';
$word = 'dhéag' if ($t == 2);
$ans .= "\n".toipa('déag')
}
}
return $ans;
}
sub caighdeanu {
(my $t) = @_;
$t =~ s/ḃ/bh/g;
$t =~ s/ċ/ch/g;
$t =~ s/ḋ/dh/g;
$t =~ s/ḟ/fh/g;
$t =~ s/ġ/gh/g;
$t =~ s/ṁ/mh/g;
$t =~ s/ṗ/ph/g;
$t =~ s/ṡ/sh/g;
$t =~ s/ṫ/th/g;
return $t;
}
while (<STDIN>) {
chomp;
s/https?:[^ ]+//g;
#s/[A-Za-zÁÉÍÓÚáéíóú0-9.-]+\.(com|ie|uk|org)(\P{L})/$2/g;
#s/ *@[A-Za-z0-9_]+//g;
s/^RT @[A-Za-z0-9_]+: //;
s/^(@[A-Za-z0-9_]+ )+//;
s/([^0-9])([<:]3)+/$1/g;
s/^ *//g;
s/ */ /g;
my $orig = $_;
while (m/((\p{L}|[0-9’'-])+)/g) {
my $t = $1;
$t =~ s/[’'-]+$//;
next if ($t eq '');
$t =~ s/’/'/g;
$t =~ s/^([nt])([AEIOUÁÉÍÓÚ])/$1-$2/;
$t = lc($t);
$t = caighdeanu($t);
$total++;
if ($t =~ m/^[0-9]+$/) {
$found++;
print numtoipa($t,0)."\n";
}
else {
print toipa($t)."\n";
}
}
print "<BRISEADH>$orig\n";
}
my $ans = $found / (1.0*$total);
print "Found: $ans\n";
exit 0;