-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman_convert.js
173 lines (154 loc) · 4.5 KB
/
roman_convert.js
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
/**
* Created by Janaka on 2017-01-15.
* Modified to new JS standards on 2023-03-08
*/
// sinhala unicode, roman
const ro_specials = [
/* VOWELS */
['ඓ', 'ai'], // sinhala only begin - only kai and ai occurs in reality
['ඖ', 'au'], // ambiguous conversions e.g. k+au = ka+u = kau, a+u = au but only kau and au occurs in reality
['ඍ', 'ṛ'],
['ඎ', 'ṝ'],
//['ඏ', 'ḷ'], // removed because conflicting with ළ් and very rare
['ඐ', 'ḹ'], // sinhala only end
['අ', 'a'],
['ආ', 'ā'],
['ඇ', 'æ'], ['ඇ', 'Æ', 1],
['ඈ', 'ǣ'],
['ඉ', 'i'],
['ඊ', 'ī'],
['උ', 'u'],
['ඌ', 'ū'],
['එ', 'e'],
['ඒ', 'ē'],
['ඔ', 'o'],
['ඕ', 'ō'],
/* SPECIALS */
['ඞ්', 'ṅ'], // not used in combi
['ං', 'ṃ'], ['ං', 'ṁ', 1], // IAST, use both
['ඃ', 'ḥ'], ['ඃ', 'Ḥ', 1] // sinhala only
];
const ro_consonants = [
['ඛ', 'kh'],
['ඨ', 'ṭh'],
['ඝ', 'gh'],
['ඡ', 'ch'],
['ඣ', 'jh'],
['ඦ', 'ñj'], //ඤ්ජ
['ඪ', 'ḍh'],
['ඬ', 'ṇḍ'], ['ඬ', 'dh', 1], //ණ්ඩ
['ථ', 'th'],
['ධ', 'dh'],
['ඵ', 'ph'],
['භ', 'bh'],
['ඹ', 'mb'], // non pali
['ඳ', 'ṉd'], ['ඳ', 'd', 1], // non pali
['ඟ', 'ṉg'], ['ඟ', 'g', 1], // non pali
['ඥ', 'gn'], // non pali
['ක', 'k'],
['ග', 'g'],
['ච', 'c'],
['ජ', 'j'],
['ඤ', 'ñ'],
['ට', 'ṭ'],
['ඩ', 'ḍ'],
['ණ', 'ṇ'],
['ත', 't'],
['ද', 'd'],
['න', 'n'],
['ප', 'p'],
['බ', 'b'],
['ම', 'm'],
['ය', 'y'],
['ර', 'r'],
['ල', 'l'],
['ව', 'v'],
['ශ', 'ś'],
['ෂ', 'ş'], ['ෂ', 'Ṣ', 1], ['ෂ', 'ṣ', 1],
['ස', 's'],
['හ', 'h'],
['ළ', 'ḷ'],
['ෆ', 'f']
];
// sinh before, sinh after, roman after
const ro_combinations = [
['', '', '්'], //ක්
['', 'a', ''], //ක
['', 'ā', 'ා'], //කා
['', 'æ', 'ැ'], // non pali
['', 'ǣ', 'ෑ'], // non pali
['', 'i', 'ි'],
['', 'ī', 'ී'],
['', 'u', 'ු'],
['', 'ū', 'ූ'],
['', 'e', 'ෙ'],
['', 'ē', 'ේ'], // non pali
['', 'ai', 'ෛ'], // non pali
['', 'o', 'ො'],
['', 'ō', 'ෝ'], // non pali
['', 'ṛ', 'ෘ'], // sinhala only begin
['', 'ṝ', 'ෲ'],
['', 'au', 'ෞ'],
//['', 'ḷ', 'ෟ'], // conflicting with ළ් - might cause bugs - removed bcs very rare
['', 'ḹ', 'ෳ'] // sinhala only end
];
const ro_conso_combi = createConsoCombi(ro_combinations, ro_consonants);
export function romanToSinhalaConvert(text) {
text = genericConvert(text, 1);
// add zwj for yansa and rakaransa
text = replaceRe(text, '්ර','්ර'); // rakar
return replaceRe(text, '්ය','්ය'); // yansa
}
export function sinhalaToRomanConvert(text) {
// remove zwj since it does not occur in roman
text = replaceRe(text, '\u200D', '');
return genericConvert(text, 0);
}
function replaceRe(text, f, r) {
const re = new RegExp(f, "gi");
return text.replace(re, r);
}
function genericConvert(text, dir) {
ro_conso_combi.sort((a, b) => b[dir].length - a[dir].length)
ro_conso_combi.forEach(cc => {
if (cc.length < 3 || cc[2] == dir) {
text = replaceRe(text, cc[dir], cc[+!dir]);
}
});
ro_specials.sort((a, b) => b[dir].length - a[dir].length)
ro_specials.forEach(v => {
if (v.length < 3 || v[2] == dir) {
text = replaceRe(text, v[dir], v[+!dir]);
}
});
return text
}
// create permutations
function createConsoCombi(combinations, consonants) {
const conso_combi = [];
combinations.forEach(combi => {
consonants.forEach(conso => {
var cc = [conso[0] + combi[2], combi[0] + conso[1] + combi[1]];
if (conso.length > 2) { // add one-way direction if any
cc.push(conso[2]);
}
conso_combi.push(cc);
});
});
return conso_combi;
}
export function genTestPattern() {
let testSinh = '';
ro_conso_combi.forEach(cc => {
if (cc.length < 3 || cc[2] == 0) {
testSinh += cc[0] + ' ';
}
});
ro_specials.forEach(v => {
if (v.length < 3 || v[2] == 0) {
testSinh += v[0] + ' ';
}
});
return testSinh;
}
//module.exports = {romanToSinhalaConvert, sinhalaToRomanConvert, genTestPattern}