-
Notifications
You must be signed in to change notification settings - Fork 0
/
vowel_converter.go
323 lines (278 loc) · 9.44 KB
/
vowel_converter.go
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
package kanatrans
import (
"strings"
)
type vowelConverter struct {
vowels string
vowsyms string
}
func newVowelConverter() *vowelConverter {
return &vowelConverter{
vowels: "aeiou",
vowsyms: "aɑʌɚæeɛɪijɔoʊu",
}
}
// ajRule applies the rule for "aj" or "ɑj" && returns the replacement string.
func (vc *vowelConverter) ajRule(word string, wIdx int) string {
return "ai"
}
// arRule applies the rule for "ɑɹ" && returns the replacement string.
func (vc *vowelConverter) arRule(word string, wIdx int) string {
return "aa"
}
// awRule applies the rule for "aw" or "ɑw" && returns the replacement string.
func (vc *vowelConverter) awRule(word string, wIdx int) string {
return "au" // oo?
}
// aShortRule applies the rule for "ɑ" && returns the replacement string.
func (vc *vowelConverter) aShortRule(word string, wIdx int) string {
if wIdx < len(word) && word[wIdx] == 'o' {
return "o"
}
return "a"
}
// aLongRule applies the rule for "ɚ" && returns the replacement string.
func (vc *vowelConverter) aLongRule(word string, wIdx int) string {
return "aa"
}
// aeRule applies the rule for "æ" && returns the replacement string.
func (vc *vowelConverter) aeRule(word string, wIdx int) string {
if wIdx < len(word) && wIdx >= 1 && (word[wIdx-1] == 'c' || word[wIdx-1] == 'g') {
return "ya"
}
return "a"
}
// hatRule applies the rule for "ʌ" && returns the replacement string.
func (vc *vowelConverter) hatRule(word string, wIdx int) string {
if wIdx+1 < len(word) && word[wIdx] == 'o' && word[wIdx+1] == 'u' {
return "a"
} else if wIdx+1 < len(word) && word[wIdx] == 'i' && word[wIdx+1] == 'o' {
return "o"
} else if wIdx > 0 && wIdx < len(word) && word[wIdx] == 'e' && word[wIdx-1] == 'l' {
if wIdx-2 >= 0 && (word[wIdx-2] == 'd' || word[wIdx-2] == 't') {
return "o"
} else if wIdx+1 < len(word) && word[wIdx+1] == 't' {
return "e"
} else {
return "u"
}
} else if wIdx > 0 && wIdx < len(word) && word[wIdx] == 'u' && word[wIdx-1] == 'j' {
return "ya"
} else if wIdx < len(word) && word[wIdx] == 'o' && word != "mother" {
return "o"
} else if wIdx < len(word) && word[wIdx] == 'e' {
return "e"
} else if wIdx < len(word) && word[wIdx] == 'i' {
return "i"
} else {
return "a"
}
}
// ejRule applies the rule for "ej" && returns the replacement string.
func (vc *vowelConverter) ejRule(word string, wIdx int) string {
return "ei"
}
// erRule applies the rule for "ɛɹ" && returns the replacement string.
func (vc *vowelConverter) erRule(word string, wIdx int) string {
return "eaa"
}
// eRule applies the rule for "ɛ" && returns the replacement string.
func (vc *vowelConverter) eRule(word string, wIdx int) string {
return "e"
}
// irRule applies the rule for "ɪɹ" && returns the replacement string.
func (vc *vowelConverter) irRule(word string, wIdx int) string {
return "iaa"
}
// iirLongRule applies the rule for "iɹ" && returns the replacement string.
func (vc *vowelConverter) iirLongRule(word string, wIdx int) string {
return "iaa"
}
// iLongRule applies the rule for "i" && returns the replacement string.
func (vc *vowelConverter) iLongRule(word string, wIdx int) string {
return "ii"
}
// iShortRule applies the rule for "ɪ" && returns the replacement string.
func (vc *vowelConverter) iShortRule(word string, wIdx int) string {
return "i"
}
// jaRule applies the rule for "jʌ" && returns the replacement string.
func (vc *vowelConverter) jaRule(word string, wIdx int) string {
return "yaa"
}
// jaShortRule applies the rule for "jɑ" or "jæ" && returns the replacement string.
func (vc *vowelConverter) jaShortRule(word string, wIdx int) string {
return "ya"
}
// jawRule applies the rule for "jaw" && returns the replacement string.
func (vc *vowelConverter) jawRule(word string, wIdx int) string {
return "yoo"
}
// juRule applies the rule for "ju" && returns the replacement string.
func (vc *vowelConverter) juRule(word string, wIdx int) string {
return "yuu"
}
// juShortRule applies the rule for "jɚ" or "jʊ" && returns the replacement string.
func (vc *vowelConverter) juShortRule(word string, wIdx int) string {
return "yu"
}
// jiRule applies the rule for "ji" && returns the replacement string.
func (vc *vowelConverter) jiRule(word string, wIdx int) string {
return "ii"
}
// jeRule applies the rule for "jɛ" or "jɪ" && returns the replacement string.
func (vc *vowelConverter) jeRule(word string, wIdx int) string {
return "ie"
}
// jejRule applies the rule for "jej" && returns the replacement string.
func (vc *vowelConverter) jejRule(word string, wIdx int) string {
return "yei"
}
// jowRule applies the rule for "jow" && returns the replacement string.
func (vc *vowelConverter) jowRule(word string, wIdx int) string {
return "yoo"
}
// joRule applies the rule for "jɔ" && returns the replacement string.
func (vc *vowelConverter) joRule(word string, wIdx int) string {
return "yo"
}
// ojRule applies the rule for "ɔj" or "ʌj" && returns the replacement string.
func (vc *vowelConverter) ojRule(word string, wIdx int) string {
return "oi"
}
// orRule applies the rule for "ɔɹ" && returns the replacement string.
func (vc *vowelConverter) orRule(word string, wIdx int) string {
return "oo"
}
// owRule applies the rule for "ow" && returns the replacement string.
func (vc *vowelConverter) owRule(word string, wIdx int) string {
return "oo"
}
// oRule applies the rule for "ɔ" && returns the replacement string.
func (vc *vowelConverter) oRule(word string, wIdx int) string {
if wIdx+1 < len(word) && word[wIdx] == 'a' && word[wIdx+1] == 'u' {
return "oo"
}
return "o"
}
// jurRule applies the rule for "jʊɹ" && returns the replacement string.
func (vc *vowelConverter) jurRule(word string, wIdx int) string {
return "yuaa"
}
// jsiRule applies the rule for "jsi" && returns the replacement string.
func (vc *vowelConverter) jsiRule(word string, wIdx int) string {
return "ji"
}
// urRule applies the rule for "ʊɹ" && returns the replacement string.
func (vc *vowelConverter) urRule(word string, wIdx int) string {
return "uaa"
}
// uShortRule applies the rule for "ʊ" && returns the replacement string.
func (vc *vowelConverter) uShortRule(word string, wIdx int) string {
return "u"
}
// uLongRule applies the rule for "u" && returns the replacement string.
func (vc *vowelConverter) uLongRule(word string, wIdx int) string {
return "uu"
}
// jRule applies the rule for the last "j" && returns the replacement string.
func (vc *vowelConverter) jRule(word string, wIdx int) string {
return "ji"
}
// ConvertVowel converts vowels in a word according to phonetic rules && returns the converted string.
func (vc *vowelConverter) ConvertVowel(word, ph string) string {
vowelMap := map[string]func(word string, wIdx int) string{
"aj": vc.ajRule,
"ɑj": vc.ajRule,
"ɑɹ": vc.arRule,
"aw": vc.awRule,
"ɑw": vc.arRule,
"ɑ": vc.aShortRule,
"ɚ": vc.aLongRule,
"æ": vc.aeRule,
"ʌ": vc.hatRule,
"ej": vc.ejRule,
"ɛɹ": vc.erRule,
"ɛ": vc.eRule,
"ɪɹ": vc.irRule,
"i": vc.iLongRule,
"iɹ": vc.irRule,
"ɪ": vc.iShortRule,
"jʌ": vc.jaRule,
"jɑ": vc.jaShortRule,
"jæ": vc.jaShortRule,
"jaw": vc.jawRule,
"ju": vc.juRule,
"jɚ": vc.juShortRule,
"jʊ": vc.juShortRule,
"ji": vc.jiRule,
"jɪ": vc.jeRule,
"jɛ": vc.jeRule,
"jej": vc.jejRule,
"jow": vc.jowRule,
"jɔ": vc.joRule,
"jsi": vc.jsiRule,
"ɔj": vc.ojRule,
"ʌj": vc.ojRule,
"ɔɹ": vc.orRule,
"ow": vc.owRule,
"ɔ": vc.oRule,
"jʊɹ": vc.jurRule,
"ʊɹ": vc.urRule,
"ʊ": vc.uShortRule,
"u": vc.uLongRule,
"j": vc.jRule,
}
var result strings.Builder
wIdx, pIdx := 0, 0
phS, wordS := newRuneString(ph), newRuneString(word)
for pIdx < phS.Len() {
// skips consnant in a word
// in some cases, y is mapped to vowel sound
for wIdx < wordS.Len() && wordS.CharAt(wIdx) != "y" && wordS.CharAt(wIdx) != "'" && !strings.Contains(vc.vowels, wordS.CharAt(wIdx)) {
wIdx++
}
// adds consonant phonetics to the result, but does nothing for now
for pIdx < phS.Len() && !strings.Contains(vc.vowsyms, phS.CharAt(pIdx)) {
result.WriteString(phS.CharAt(pIdx))
pIdx++
}
// convert vowel phonetics
for pIdx < phS.Len() && strings.Contains(vc.vowsyms, phS.CharAt(pIdx)) {
if wIdx+1 < wordS.Len() && wordS.CharAt(wIdx) == "u" && strings.Contains(vc.vowels, wordS.CharAt(wIdx+1)) {
wIdx++
}
if pIdx+3 <= phS.Len() && stringInMapKey(phS.Substring(pIdx,pIdx+3),vowelMap) {
result.WriteString(vowelMap[phS.Substring(pIdx,pIdx+3)](wordS.String(),wIdx))
pIdx += 3
wIdx++
} else if pIdx+2 <= phS.Len() && stringInMapKey(phS.Substring(pIdx,pIdx+2),vowelMap) && (pIdx+2 == phS.Len() || phS.CharAt(pIdx+1) != "ɹ" || !strings.Contains(vc.vowsyms, phS.CharAt(pIdx+2)) ) {
result.WriteString(vowelMap[phS.Substring(pIdx,pIdx+2)](wordS.String(),wIdx))
pIdx += 2
wIdx++
} else if pIdx < phS.Len() && stringInMapKey(phS.CharAt(pIdx),vowelMap) {
result.WriteString(vowelMap[phS.CharAt(pIdx)](wordS.String(),wIdx))
pIdx++
wIdx++
} else if pIdx == phS.Len()-1 && phS.CharAt(pIdx) == "j"{
result.WriteString(vowelMap[phS.CharAt(pIdx)](wordS.String(),wIdx))
pIdx++
wIdx++
}
}
// skips rest of vowel chars
for wIdx < wordS.Len() && strings.Contains(vc.vowels,wordS.CharAt(wIdx)) {
wIdx++
}
}
// adds rest of consonant symbols
for pIdx < phS.Len() {
result.WriteString(phS.CharAt(pIdx))
pIdx++
}
return result.String()
}
func stringInMapKey(str string, m map[string]func(string, int)string) bool {
_, ok := m[str]
return ok
}