-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlphone.go
252 lines (218 loc) · 5.2 KB
/
mlphone.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
package mlphone
import (
"regexp"
"strings"
)
var vowels = map[string]string{
"അ": "A",
"ആ": "A",
"ഇ": "I",
"ഈ": "I",
"ഉ": "U",
"ഊ": "U",
"ഋ": "R",
"എ": "E",
"ഏ": "E",
"ഐ": "AI",
"ഒ": "O",
"ഓ": "O",
"ഔ": "O",
}
var consonants = map[string]string{
"ക": "K",
"ഖ": "K",
"ഗ": "K",
"ഘ": "K",
"ങ": "NG",
"ച": "C",
"ഛ": "C",
"ജ": "J",
"ഝ": "J",
"ഞ": "NJ",
"ട": "T",
"ഠ": "T",
"ഡ": "T",
"ഢ": "T",
"ണ": "N1",
"ത": "0",
"ഥ": "0",
"ദ": "0",
"ധ": "0",
"ന": "N",
"പ": "P",
"ഫ": "F",
"ബ": "B",
"ഭ": "B",
"മ": "M",
"യ": "Y",
"ര": "R",
"ല": "L",
"വ": "V",
"ശ": "S1",
"ഷ": "S1",
"സ": "S",
"ഹ": "H",
"ള": "L1",
"ഴ": "Z",
"റ": "R1",
}
var chillus = map[string]string{
"ൽ": "L",
"ൾ": "L1",
"ൺ": "N1",
"ൻ": "N",
"ർ": "R1",
"ൿ": "K",
}
var compounds = map[string]string{
"ക്ക": "K2",
"ഗ്ഗ": "K",
"ങ്ങ": "NG",
"ച്ച": "C2",
"ജ്ജ": "J",
"ഞ്ഞ": "NJ",
"ട്ട": "T2",
"ണ്ണ": "N2",
"ത്ത": "0",
"ദ്ദ": "D",
"ദ്ധ": "D",
"ന്ന": "NN",
"ന്ത": "N0",
"ങ്ക": "NK",
"ണ്ട": "N1T",
"ബ്ബ": "B",
"പ്പ": "P2",
"മ്മ": "M2",
"യ്യ": "Y",
"ല്ല": "L2",
"വ്വ": "V",
"ശ്ശ": "S1",
"സ്സ": "S",
"ള്ള": "L12",
"ഞ്ച": "NC",
"ക്ഷ": "KS1",
"മ്പ": "MP",
"റ്റ": "T",
"ന്റ": "NT",
"്രി": "R",
"്രു": "R",
}
var modifiers = map[string]string{
"ാ": "",
"ഃ": "",
"്": "",
"ൃ": "R",
"ം": "3",
"ി": "4",
"ീ": "4",
"ു": "5",
"ൂ": "5",
"െ": "6",
"േ": "6",
"ൈ": "7",
"ൊ": "8",
"ോ": "8",
"ൌ": "9",
"ൗ": "9",
}
var (
rxKey0, _ = regexp.Compile(`[1,2,4-9]`)
rxKey1, _ = regexp.Compile(`[2,4-9]`)
rxNonMalayalam, _ = regexp.Compile(`[\P{Malayalam}]`)
rxAlphaNum, _ = regexp.Compile(`[^0-9A-Z]`)
rxSpecialCase, _ = regexp.Compile(`^(A|V|T|S|U|M|O)L(K|S)`)
)
// MLPhone is the Malayalam-phone tokenizer.
type MLPhone struct {
modCompounds *regexp.Regexp
modConsonants *regexp.Regexp
modVowels *regexp.Regexp
}
// New returns a new instance of the KNPhone tokenizer.
func New() *MLPhone {
var (
glyphs []string
mods []string
ml = &MLPhone{}
)
// modifiers.
for m := range modifiers {
mods = append(mods, m)
}
// compounds.
for c := range compounds {
glyphs = append(glyphs, c)
}
ml.modCompounds, _ = regexp.Compile(`((` + strings.Join(glyphs, "|") + `)(` + strings.Join(mods, "|") + `))`)
// consonants.
glyphs = []string{}
for k := range consonants {
glyphs = append(glyphs, k)
}
ml.modConsonants, _ = regexp.Compile(`((` + strings.Join(glyphs, "|") + `)(` + strings.Join(mods, "|") + `))`)
// vowels.
glyphs = []string{}
for k := range vowels {
glyphs = append(glyphs, k)
}
ml.modVowels, _ = regexp.Compile(`((` + strings.Join(glyphs, "|") + `)(` + strings.Join(mods, "|") + `))`)
return ml
}
// Encode encodes a unicode Malayalm string to its Roman MLPhone hash.
// Ideally, words should be encoded one at a time, and not as phrases
// or sentences.
func (ml *MLPhone) Encode(input string) (string, string, string) {
// key2 accounts for hard and modified sounds.
key2 := ml.process(input)
// key1 loses numeric modifiers that denote phonetic modifiers.
key1 := rxKey1.ReplaceAllString(key2, "")
// key0 loses numeric modifiers that denote hard sounds, doubled sounds,
// and phonetic modifiers.
key0 := rxKey0.ReplaceAllString(key2, "")
return key0, key1, key2
}
func (ml *MLPhone) process(input string) string {
// Remove all non-malayalam characters.
input = rxNonMalayalam.ReplaceAllString(strings.Trim(input, ""), "")
// All character replacements are grouped between { and } to maintain
// separatability till the final step.
// Replace and group modified compounds.
input = ml.replaceModifiedGlyphs(input, compounds, ml.modCompounds)
// Replace and group unmodified compounds.
for k, v := range compounds {
input = strings.ReplaceAll(input, k, `{`+v+`}`)
}
// Replace and group modified consonants and vowels.
input = ml.replaceModifiedGlyphs(input, consonants, ml.modConsonants)
input = ml.replaceModifiedGlyphs(input, vowels, ml.modVowels)
// Replace and group unmodified consonants.
for k, v := range consonants {
input = strings.ReplaceAll(input, k, `{`+v+`}`)
}
// Replace and group unmodified vowels.
for k, v := range vowels {
input = strings.ReplaceAll(input, k, `{`+v+`}`)
}
// Replace chillu.
for k, v := range chillus {
input = strings.ReplaceAll(input, k, `{`+v+`}`)
}
// Replace all modifiers.
for k, v := range modifiers {
input = strings.ReplaceAll(input, k, v)
}
// Remove non alpha numeric characters (losing the bracket grouping).
input = rxAlphaNum.ReplaceAllString(input, "")
// phonetic exceptions (uthsavam - ulsavam...)
return rxSpecialCase.ReplaceAllString(input, "${1}0$2")
}
func (ml *MLPhone) replaceModifiedGlyphs(input string, glyphs map[string]string, r *regexp.Regexp) string {
for _, matches := range r.FindAllStringSubmatch(input, -1) {
for _, m := range matches {
if rep, ok := glyphs[m]; ok {
input = strings.ReplaceAll(input, m, rep)
}
}
}
return input
}