-
Notifications
You must be signed in to change notification settings - Fork 9
/
gurmukhi.dart
178 lines (162 loc) · 5.78 KB
/
gurmukhi.dart
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
extension GurmukhiExtensions on String {
/// Remove vishraams from Gurmukhi text.
///
/// ```dart
/// 'ਸਭਨਾ ਜੀਆ ਕਾ, ਇਕੁ ਦਾਤਾ; ਸੋ. ਮੈ ਵਿਸਰਿ ਨ ਜਾਈ ॥੫॥'.removeVishraams() == 'ਸਭਨਾ ਜੀਆ ਕਾ ਇਕੁ ਦਾਤਾ ਸੋ ਮੈ ਵਿਸਰਿ ਨ ਜਾਈ ॥੫॥'
/// ```
String removeVishraams() => replaceAll(RegExp('[.,;]'), '');
/// Get first letters from Gurmukhi text.
///
/// ```dart
/// 'ਜਿਸਨੋ ਕ੍ਰਿਪਾ ਕਰਹਿ ਤਿਨਿ ਨਾਮ ਰਤਨੁ ਪਾਇਆ ॥'.firstGurmukhiLetters() == ['ਜ', 'ਕ', 'ਕ', 'ਤ', 'ਨ', 'ਰ', 'ਪ']
/// ```
Iterable<String> firstGurmukhiLetters() {
return split(' ')
.where((word) => word.isNotEmpty && _isGurmukhiChar(word.codeUnitAt(0)))
.map((word) => word[0]);
}
/// Split Gurmukhi text into letters.
///
/// ```dart
/// 'ਕੈਸੀ ਆਰਤੀ ਹੋਇ ॥'.splitGurmukhi() == ['ਕੈ', 'ਸੀ', ' ', 'ਆ', 'ਰ', 'ਤੀ', ' ', 'ਹੋ', 'ਇ', ' ', '॥']
/// ```
///
/// Enable [splitVirama] to split chars joined by virama.
///
/// ```dart
/// 'ਅੰਮ੍ਰਿਤ'.splitGurmukhi() == ['ਅੰ', 'ਮ੍ਰਿ', 'ਤ']
/// 'ਅੰਮ੍ਰਿਤ'.splitGurmukhi(splitVirama: true) == ['ਅੰ', 'ਮ੍', 'ਰਿ', 'ਤ']
/// ```
///
/// Enable [extensions] to support SantLipi modifiers.
///
/// ```dart
/// 'ਸ︁ਯਾਮ'.splitGurmukhi(extensions: true) == ['ਸ', '︁ਯਾ', 'ਮ']
/// ```
Iterable<String> splitGurmukhi({
bool splitVirama = false,
bool extensions = false,
}) sync* {
var br = 0; // starting index of gurmukhi letter break
for (var i = 0; i < length; i++) {
final curr = codeUnitAt(i);
final next = i + 1 < length ? codeUnitAt(i + 1) : null;
if ((next != null && _isGurmukhiDiacritic(next)) ||
(!splitVirama && _isGurmukhiVirama(curr)) ||
(extensions && _isSantLipiYayyaModifier(curr))) {
continue;
}
yield substring(br, i + 1);
br = i + 1;
}
}
/// Unicode normalization of Gurmukhi text.
///
/// Fix Matras of 'ੳ', 'ਅ', and 'ੲ':
/// ```dart
/// 'ਅਾਦਿ'.normalizeGurmukhi() == 'ਆਦਿ'
/// ```
///
/// Fix Order of Diacritics:
/// ```dart
/// 'ਕੰੁਚਰ'.normalizeGurmukhi() == 'ਕੁੰਚਰ'
/// ```
///
/// Remove SantLipi modifiers if [extensions] is not enabled:
/// ```dart
/// 'ਮਧ︀ਯ'.normalizeGurmukhi() == 'ਮਧ੍ਯ'
/// ```
String normalizeGurmukhi({bool extensions = false}) {
var text = this;
// Use SantLipi modifier for Addha Yayya.
text = text.replaceAll('੍ਯ', '\ufe00ਯ');
return _sanitizeGurmukhi(
_sortGurmukhiDiacritics(text),
extensions: extensions,
);
}
}
bool _isGurmukhiChar(int char) => char >= 0x0A00 && char <= 0x0A7F;
bool _isGurmukhiVirama(int char) => char == 0x0A4D;
bool _isGurmukhiDiacritic(int char) => _getGurmukhiDiacriticOrder(char) != 0;
bool _isSantLipiYayyaModifier(int char) {
return (char == 0xFE00) // ︀
||
(char == 0xFE01); // ︁
}
int _getGurmukhiDiacriticOrder(int char) {
// nukta, virama, consonant after virama, sign, vowel (left, top, bottom, right), addak, nasal, visarga
return const {
0x0A3C: -2, // ਼
0x0A4D: -1, // ੍
// consonant after virama: 0
0x0A51: 1, // ੑ
0x0A75: 1, // ੵ
0x0A3F: 2, // ਿ
0x0A47: 3, // ੇ
0x0A48: 3, // ੈ
0x0A4B: 3, // ੋ
0x0A4C: 3, // ੌ
0x0A41: 4, // ੁ
0x0A42: 4, // ੂ
0x0A3E: 5, // ਾ
0x0A40: 5, // ੀ
0x0A71: 6, // ੱ
0x0A01: 7, // ਁ
0x0A02: 7, // ਂ
0x0A70: 7, // ੰ
0x0A03: 8, // ਃ
}[char] ??
0;
}
int _gurmukhiDiacriticComparator(int a, int b) {
return _getGurmukhiDiacriticOrder(a) - _getGurmukhiDiacriticOrder(b);
}
String _sortGurmukhiDiacritics(String text) {
return text.splitGurmukhi(extensions: true).map((letter) {
if (letter.length > 1) {
final diacritics = letter.codeUnits.sublist(1);
diacritics.sort(_gurmukhiDiacriticComparator);
return String.fromCharCodes([letter.codeUnitAt(0), ...diacritics]);
} else {
return letter;
}
}).join();
}
String _sanitizeGurmukhi(String text, {bool extensions = false}) {
const map = {
'\u0A05\u0A3E': '\u0A06', // ਅਾ -> ਆ
'\u0A05\u0A48': '\u0A10', // ਅੈ -> ਐ
'\u0A05\u0A4C': '\u0A14', // ਅੌ -> ਔ
'\u0A72\u0A3F': '\u0A07', // ੲਿ -> ਇ
'\u0A72\u0A40': '\u0A08', // ੲੀ -> ਈ
'\u0A72\u0A47': '\u0A0F', // ੲੇ -> ਏ
'\u0A73\u0A4B': '\u0A13', // ੳੋ -> ਓ
'\u0A73\u0A41': '\u0A09', // ੳੁ -> ਉ
'\u0A73\u0A42': '\u0A0A', // ੳੂ -> ਊ
'\u0A16\u0A3C': '\u0A59', // ਖ਼ -> ਖ਼
'\u0A17\u0A3C': '\u0A5A', // ਗ਼ -> ਗ਼
'\u0A1C\u0A3C': '\u0A5B', // ਜ਼ -> ਜ਼
'\u0A2B\u0A3C': '\u0A5E', // ਫ਼ -> ਫ਼
'\u0A32\u0A3C': '\u0A33', // ਲ਼ -> ਲ਼
'\u0A38\u0A3C': '\u0A36', // ਸ਼ -> ਸ਼
'\u0A71\u0A02': '\u0A01', // ੱਂ -> ਁ
'\uFE00\u0A2F': '\u0A4D\u0A2F', // ︀ਯ︀ -> ੍ਯ
'\uFE01\u0A2F': '\u0A2F', // ︁ਯ︀ -> ਯ︀
'\uFE00\uFE01\u0A2F': '\u0A4D\u0A2F', // ︀︁ਯ︀ -> ੍ਯ
'ਂ\u200Dੀ': 'ੀਂ', // bindi before bihari -> after bihari
};
return text.replaceAllMapped(
RegExp([
'\u0A05[\u0A3E\u0A48\u0A4C]',
'\u0A72[\u0A3F\u0A40\u0A47]',
'\u0A73[\u0A4B\u0A41\u0A42]',
'[\u0A16\u0A17\u0A1C\u0A2B\u0A32\u0A38]\u0A3C',
'\u0A71\u0A02',
if (!extensions) '[\uFE00\uFE01]\u0A2F',
if (!extensions) '\uFE00\uFE01\u0A2F',
if (!extensions) 'ਂ\u200Dੀ',
].join('|')),
(match) => map[text.substring(match.start, match.end)]!,
);
}