-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathG2P.cs
458 lines (419 loc) · 19.9 KB
/
G2P.cs
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Automated Grapheme-to-Phoneme Conversion for Central Kurdish based on Optimality Theory
// Copyright (C) 2019 Aso Mahmudi, Hadi Veisi
// Maintainer: Aso Mahmudi (aso.mehmudi@gmail.com)
// Demo: https://asosoft.github.io/g2p/
// Source Code: https://github.com/AsoSoft/AsoSoft-Library
// Test-set: https://github.com/AsoSoft/Kurdish-G2P-dataset
// Paper: https://www.sciencedirect.com/science/article/abs/pii/S0885230821000292
// Cite:
// @article{mahmudi2021automated,
// title={Automated grapheme-to-phoneme conversion for Central Kurdish based on optimality theory},
// author={Mahmudi, Aso and Veisi, Hadi},
// journal={Computer Speech \& Language},
// volume={70},
// pages={101222},
// year={2021},
// publisher={Elsevier}
// }
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace AsoSoftLibrary
{
public static partial class AsoSoft
{
private static Dictionary<string, string> History = new Dictionary<string, string>();
/// <summary>Converts Central Kurdish text in standard Arabic script into syllabified phonemic Latin script (i.e. graphemes to phonems)</summary>
public static string G2P(string text,
bool convertNumbersToWord = false,
bool backMergeConjunction = true,
bool singleOutputPerWord = true)
{
var sb = new StringBuilder();
text = UnifyNumerals(text, "en");
if (convertNumbersToWord)
text = Number2Word(text);
text = g2pNormalize(text.Trim());
//
var ku = "ئابپتجچحخدرڕزژسشعغفڤقکگلڵمنوۆەهیێ" + "ۋۉۊڎڴݵݸ";
var wordss = Regex.Matches(text, "([" + ku + "]+|[^" + ku + "]+)");
for (int i = 0; i < wordss.Count; i++)
{
var word = wordss[i].Value;
if (Regex.IsMatch(word, "[" + ku + "]") && word != "و")
sb.Append(WordG2P(Regex.Replace(word, "[^" + ku + "]+", ""), singleOutputPerWord));
else
sb.Append(word);
}
var output = sb.ToString();
// conjunction و
output = Regex.Replace(output, "(^|[?!.] ?)" + "و", "$1ˈwe");
if (!backMergeConjunction)
output = Regex.Replace(output, "و", "û");
else
{
// if there are candidates preceeding conjunction (e.g ˈbîst¶ˈbîˈsit و)
output = Regex.Replace(output, "(\\w+)¶(\\w+)¶(\\w+) و"
, "$1 و¶$2 و¶$3 و");
output = Regex.Replace(output, "(\\w+)¶(\\w+) و"
, "$1 و¶$2 و");
// ('bi'ra + w => bi'raw)
output = Regex.Replace(output, "([aeêouûiî]) و", "$1w");
// ('be'fir + û => 'bef'rû)
output = Regex.Replace(output, "(?<=\\w)ˈ([^aeêouûiî])i([^aeêouûiî]) و", "$1ˈ$2û");
// ('ser + û => 'se'rû)
// ('sard + û => 'sar'dû)
// ('min + û => 'mi'nû)
// ('bi'gir + û => 'bi'gi'rû)
// ('gir'tin + û => 'gir'ti'nû)
output = Regex.Replace(output, "([^aeêouûiî]) و", "ˈ$1û");
// if conjunction makes candidates the same (e.g ˈbîsˈtû¶ˈbîsˈtû)
output = Regex.Replace(output, "(?<w>\\w+)¶\\k<w>(?=\\s|$)", "$1");
}
return output.TrimEnd();
}
// chooses the best candidates for the word
private static string Evaluator(string gr, List<string> Candidates)
{
var Output = new List<string>();
var evaluatedCandidates = EVAL(Candidates);
if (evaluatedCandidates.Count() > 0)
{
var LowestPenalt = evaluatedCandidates.First().Value;
foreach (var item in evaluatedCandidates)
if (item.Value < LowestPenalt + 5)
Output.Add(item.Key);
}
return (Output.Count() == 0) ? gr : string.Join('¶', Output);
}
// Normalizion
private static string g2pNormalize(string text)
{
var s = new string[]
{
" +", " " ,
"دٚ", "ڎ",
"گٚ", "ڴ",
@"(^|\s)چ بکە", "$1چبکە",
"َ", "ە", // فتحه
"ِ", "ی", // کسره
"ُ", "و", // ضمه
"ء", "ئ", // Hamza
"أ", "ئە",
"إ", "ئی",
"آ", "ئا",
"ظ|ذ|ض", "ز",
"ص|ث", "س",
"ط", "ت",
"ك", "ک",
"ي|ى", "ی",
"ه", "ە",
"ھ", "ه",
"ـ", "", // tatweel
"؟", "?",
"،", ",",
"؛", ";",
"\r", "",
};
for (int i = 0; i < s.Length; i += 2)
text = Regex.Replace(text, s[i], s[i + 1]);
return text;
}
private static string WordG2P(string gr, bool SingleOutputPerWord)
{
// Check history for speed up
if (!History.ContainsKey(gr))
History.Add(gr, Evaluator(gr, Generator(gr)));
return SingleOutputPerWord ? History[gr].Split('¶')[0] : History[gr];
}
// GEN: generates all possible candidates:
// e.g. بوون => bûn, buwn, bwun
private static List<string> Generator(string gr)
{
// Converting exceptional words
var G2PExceptions = resFiles.G2PExceptions.Split('\n');
for (int i = 1; i < G2PExceptions.Length; i++)
{
var item = G2PExceptions[i].Split(',');
gr = Regex.Replace(gr, item[0], item[1]);
}
// Converting certain characters
var G2PCertain = resFiles.G2PCertain.Split('\n');
for (int i = 1; i < G2PCertain.Length; i++)
{
var item = G2PCertain[i].Split(',');
gr = Regex.Replace(gr, item[0], item[1]);
}
// Uncertainty in "و" and "ی"
var CandList1 = new List<string> { "" };
while (gr.Length > 0)
{
var temp = new List<string>();
if (Regex.IsMatch(gr, "^ووووو"))
{
temp.AddRange(new List<string>
{ "uwuwu", "uwuww", "uwwuw", "uwûw",
"wuwwu", "wuwuw", "wuwû", "wûww", "wwuwu", "wwuww", "wwûw", "wûwu",
"ûwwu", "ûwuw", "ûwû"});
gr = gr.Substring(5);
}
else if (Regex.IsMatch(gr, "^وووو"))
{
temp.AddRange(new List<string>
{ "uwwu", "uwuw", "uwû",
"wwuw", "wwû", "wuww", "wuwu", "wûw",
"ûwu", "ûww", });
gr = gr.Substring(4);
}
else if (Regex.IsMatch(gr, "^ووو"))
{
temp.AddRange(new List<string>
{ "wuw", "wwu", "wû",
"uww", "uwu",
"ûw" });
gr = gr.Substring(3);
}
else if (Regex.IsMatch(gr, "^وو"))
{
temp.AddRange(new List<string> { "wu", "uw", "ww", "û" });
gr = gr.Substring(2);
}
else if (Regex.IsMatch(gr, "^و"))
{
temp.AddRange(new List<string> { "u", "w" });
gr = gr.Substring(1);
}
else if (Regex.IsMatch(gr, "^یی"))
{
temp.AddRange(new List<string> { "îy", "yî" });
gr = gr.Substring(2);
}
else if (Regex.IsMatch(gr, "^ی"))
{
temp.AddRange(new List<string> { "y", "î" });
gr = gr.Substring(1);
}
else
{
temp.Add(gr[0].ToString());
gr = gr.Substring(1);
}
var Count = CandList1.Count;
var TempList = new List<string>();
foreach (var item in CandList1)
TempList.Add(item);
CandList1.Clear();
for (int i = 0; i < Count; i++)
{
for (int j = 0; j < temp.Count; j++)
{
var WW = Regex.IsMatch(temp[j], "^ww");
var IsPreviousVowel = Regex.IsMatch(TempList[i], "[aeêouûiîüȯė]$");
var IsNowVowel = Regex.IsMatch(temp[j], "^[aeêouûiîüȯė]");
var ConsonantBeforeWW = !IsPreviousVowel && WW;
var hiatus = IsPreviousVowel && IsNowVowel;
if (!hiatus && !ConsonantBeforeWW)
CandList1.Add(TempList[i] + temp[j]);
}
}
}
// Adding "i" between Consonant Clusters
var Candidates = iInsertion(CandList1);
// ======= Syllabification for each candidate
var OutputCandidates = Syllabification(Candidates);
// for speed up: remove candidates that has 1) syllable without vowel or 2) more than 3 consonants in coda
var cCount = OutputCandidates.Count;
if (cCount > 1)
{
for (int i = cCount - 1; i > -1; i--)
if (Regex.IsMatch(OutputCandidates[i], "ˈ[^aeêouûiîüȯė]+(ˈ|$)")
|| Regex.IsMatch(OutputCandidates[i], "[aeêouûiîüȯė][^aeêouûiîüȯėˈ]{4,}"))
OutputCandidates.RemoveAt(i);
}
return OutputCandidates;
}
// insertion of hidden /i/ vowel
// e.g. brd => bird, brid, birid
private static List<string> iInsertion(List<string> Cands)
{
var Candidates = new List<string>();
for (int i = 0; i < Cands.Count; i++)
{
var ThisCand = new List<string>();
if (!string.IsNullOrEmpty(Cands[i]))
{
ThisCand.Add(Cands[i][0].ToString());
for (int j = 1; j < Cands[i].Length; j++)
{
var Count = ThisCand.Count;
var TempList = new List<string>();
foreach (var item in ThisCand)
TempList.Add(item);
ThisCand.Clear();
for (int k = 0; k < Count; k++)
{
ThisCand.Add(TempList[k] + Cands[i][j]);
if (Regex.IsMatch(Cands[i].Substring(j - 1, 2), @"[^aeêouûiîüȯė][^aeêouûiîüȯė]"))
ThisCand.Add(TempList[k] + "i" + Cands[i][j]);
}
}
}
else
ThisCand.Add(Cands[i]);
foreach (var item in ThisCand)
Candidates.Add(item);
}
return Candidates;
}
// Syllabification of candidates
// e.g. dexom => ˈdeˈxom
private static List<string> Syllabification(List<string> Candidates)
{
var cCount = Candidates.Count;
for (int i = 0; i < cCount; i++)
{
// Onset C(C)V
Candidates[i] = Regex.Replace(Candidates[i],
"([^aeêouûiîȯėwy][wy]|[^aeêouûiîȯė])([aeêouûiîȯė])", "ˈ$1$2");
// if no ˈ at beginig (grˈtin => ˈgrˈtin)
Candidates[i] = Regex.Replace(Candidates[i],
"^([^ˈ])", "ˈ$1");
// add candidate ( 'be'sye => + 'bes'ye)
if (Regex.IsMatch(Candidates[i], "[aeêouûiîȯė][^aeêouûiîȯė]?ˈ[^aeêouûiîȯėwy][wy]"))
Candidates.Add(Regex.Replace(Candidates[i], "([aeêouûiîȯė][^aeêouûiîȯė]?)ˈ([^aeêouûiîȯėwy])([wy])", "$1$2ˈ$3"));
}
return Candidates;
}
// EVAL: specifies a penalty number for each syllabified candidate
private static Dictionary<string, int> EVAL(List<string> Candidates)
{
var output = new Dictionary<string, int>();
if (Candidates.Count > 0)
{
var Penalty = new Dictionary<string, int>();
for (int i = 0; i < Candidates.Count; i++)
{
var P = 0;
// ================= types of penalties ============
// Complex Onset
P += Regex.Matches(Candidates[i], "ˈ([^aeêouûiîȯėˈ]{2,}[wy]|[^aeêouûiîȯėˈ]+[^wy])[aeêouûiîȯė]").Count * 20;
// Complex Coda
if (Candidates[i] != "ˈpoynt")
P += Regex.Matches(Candidates[i], "[aeêouûiîȯė][^aeêouûiîȯėˈ]{3}").Count * 10;
P += Regex.Matches(Candidates[i], "[^aeêouûiîȯėˈ][wy][aeêouûiîȯė][wy][^aeêouûiîȯėˈ]").Count * 20;
// SSP: ascending Sonority in coda
var codas = Regex.Matches(Candidates[i], "(?<=[aeêouûiîȯė])[^aeêouûiîȯėˈ]{2,}");
foreach (var coda in codas)
{
var chars = coda.ToString();
for (int j = 0; j < chars.Length - 1; j++)
if (SonorityIndex(chars[j]) <= SonorityIndex(chars[j + 1]))
P += 10;
}
// DEP: i insertion
P += Regex.Matches(Candidates[i], "i").Count * 2;
//===========================
P += Regex.Matches(Candidates[i], "kˈr").Count * 3;
// ('kurd'si'tan => 'kur'dis'tan)
P += Regex.Matches(Candidates[i], "[^aeêouûiîȯėˈ]ˈsiˈtaˈ?n").Count * 3;
//"(kewt|newt|ḧewt|rext|sext|dest|pest|řast|mest|pişt|wîst|hest|bîst|heşt|şest)"
// suffix /it/ and /im/ ('sert => 'se'rit) ('xewt !! 'xe'wit / 'xewt)
if (!Regex.IsMatch(Candidates[i],
"(rift|neft|kurt|girt|xirt|germ|term|port)"))
P += Regex.Matches(Candidates[i], "[aeêouûiîȯė]([^aeêouûiîyȯėˈ]m|[^aeêouûiîysşxwˈ]t)$").Count * 3;
// (ˈdyu/ => ˈdîw) and (ˈkwiř => ˈkuř)
P += Regex.Matches(Candidates[i], "yu").Count * 5;
P += Regex.Matches(Candidates[i], "uy").Count * 5;
P += Regex.Matches(Candidates[i], "yi").Count * 5;
P += Regex.Matches(Candidates[i], "iˈ?y").Count * 5; // bes'ti'yan
P += Regex.Matches(Candidates[i], "wu").Count * 5;
P += Regex.Matches(Candidates[i], "uˈ?w").Count * 2; // 'bi'bu'wî
P += Regex.Matches(Candidates[i], "wi").Count * 2;
P += Regex.Matches(Candidates[i], "iw").Count * 2;
P += Regex.Matches(Candidates[i], "wû").Count * 5;
// ˈdiˈrêˈjayˈyî => ˈdiˈrêˈjaˈyîy (not heyyî and teyyî)
// ˈdiˈrêjˈyî => ˈdiˈrêˈjîy
// (NOT ˈḧeyˈyî teyˈyî")
P += Regex.Matches(Candidates[i], "[^aeêouûiîȯė]ˈyî").Count * 3;
// [CV]'CyV => [CV]C'yV (ˈdiˈrêˈjyî => ˈdiˈrêˈjîy) ('bes'tye'tî => 'best'ye'tî)
P += Regex.Matches(Candidates[i], "(?<!^)ˈ[^aeêouûiî][wy]").Count * 3;
// C'CyV => CC'yV (bir'dyan => bird'yan) ˈswênˈdyan
P += Regex.Matches(Candidates[i], "[^aeêouûiî]ˈ[^aeêouûiî][y][aeêouûî]").Count * 2;
// twîˈwur => tu'yûr
P += Regex.Matches(Candidates[i], "[^aeêouûiî]wîˈw").Count * 3;
//===========================
// Cix (řê'kix'raw => řêk'xi'raw
P += Regex.Matches(Candidates[i], "[^aeêouûiî]ixˈ").Count * 2;
// ^'hełC' => ^'heł'C
P += Regex.Matches(Candidates[i], "^ˈhe(ł[^aeêouûiîˈ]ˈ|ˈłi)").Count * 3;
// (he'jarn => 'he'ja'rin)
P += Regex.Matches(Candidates[i], "rn").Count * 5;
// ('xawn => 'xa'win) ('pyawn => pya'win)
P += Regex.Matches(Candidates[i], "[aêoûî][w][^aeêouûiîˈ]").Count * 5;
//===========================
// ('lab'ri'di'nî => 'la'bir'di'nî)
P += Regex.Matches(Candidates[i], "[aeêouûiî][^aeêouûiîˈ]ˈriˈ").Count * 5;
//
// 'ser'nic, 'dek'rid, gir'fit => 'se'rinc, 'de'kird, 'gi'rift (NOT gir'tin)
var pat = Regex.Match(Candidates[i], "([^aeêouûiîˈ])ˈ([^aeêouûiîˈ])i([^aeêouûiîˈ])");
if (pat.Success)
{
var C = Regex.Replace(pat.Value, "[iˈ]", "");
if (SonorityIndex(C[1]) > SonorityIndex(C[2]))
P += 3; //
}
// ('sern'cê => 'se'rin'cê)
pat = Regex.Match(Candidates[i], "([^aeêouûiîˈ])([^aeêouûiîˈ])ˈ([^aeêouûiîˈ])");
if (pat.Success)
{
var C = Regex.Replace(pat.Value, "[iˈ]", "");
if (SonorityIndex(C[0]) > SonorityIndex(C[1]))
P += 3;
}
// ('ser'ni'cê => 'se'rin'cê)
pat = Regex.Match(Candidates[i], "([^aeêouûiîˈ])ˈ([^aeêouûiîˈ])iˈ([^aeêouûiîˈ])");
if (pat.Success)
{
var C = Regex.Replace(pat.Value, "[iˈ]", "");
if (SonorityIndex(C[0]) > SonorityIndex(C[1]) && SonorityIndex(C[1]) > SonorityIndex(C[2]))
P += 3;
}
// ('gi'rit'nê => 'gir'ti'nê) ('ku'şit'ne => 'kuş'ti'ne)
pat = Regex.Match(Candidates[i], "[aeêouûiî]ˈ([^aeêouûiîˈ])i([^aeêouûiîˈ])ˈ([^aeêouûiîˈ])");
if (pat.Success)
{
var C = Regex.Replace(pat.Value, "[aeêouûiîˈ]", "");
if (SonorityIndex(C[2]) >= SonorityIndex(C[1]))
P += 3;
}
Penalty.Add(Candidates[i], P);
}
output = Penalty.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
}
return output;
}
// Sonority Sequencing Principle in EVAL needs phoneme ranking
private static int SonorityIndex(char ch)
{
var c = ch.ToString();
if (Regex.IsMatch(c, "[wy]")) // Approximant
return 6;
if (Regex.IsMatch(c, "[lłrř]")) // lateral
return 5;
if (Regex.IsMatch(c, "[mn]")) // nasal
return 4;
if (Regex.IsMatch(c, "[fvszşjxẍƹḧh]")) // fricative
return 3;
if (Regex.IsMatch(c, "[cç]")) // affricate
return 2;
else // stop
return 1;
}
/// <summary>only for tests.</summary>
public static Dictionary<string, int> AllCandidates(string grapheme)
{
return EVAL(Generator(g2pNormalize(grapheme)));
}
}
}