-
Notifications
You must be signed in to change notification settings - Fork 0
/
qurantext.service.ts
480 lines (394 loc) · 18.3 KB
/
qurantext.service.ts
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import { quranText as quranTextOldMadinah } from './quran_text_old_madinah'
export const enum SpaceType {
Simple = 1,
Aya,
}
interface WordInfo {
startIndex: number
endIndex: number
text: string
baseText: string,
}
export interface LineTextInfo {
ayaSpaceIndexes: number[];
simpleSpaceIndexes: number[];
spaces: Map<number, SpaceType>
wordInfos: WordInfo[],
}
class QuranTextService {
private quranInfo: any[];
private _outline: any[] = [];
private _quranText: string[][];
private _sajsdas: any[] = [];
private TafkhimRE!: RegExp;
private OthersRE!: RegExp;
private bases = new Set<number>();
public rightNoJoinLetters = "ادذرزوؤأٱإءة";
public dualJoinLetters = "بتثجحخسشصضطظعغفقكلمنهيئى"
private madinaLineWidths = new Map([
[600 * 15 + 9, 0.84],
[602 * 15 + 5, 0.61],
[602 * 15 + 15, 0.59],
[603 * 15 + 10, 0.68],
[604 * 15 + 4, 0.836],
[604 * 15 + 9, 0.836],
[604 * 15 + 14, 0.717],
[604 * 15 + 15, 0.54],
]);
private tajweedResuls: Map<number, Map<number, string>[]> = new Map()
private LineTextInfoCache: Map<number, LineTextInfo> = new Map()
constructor() {
this._quranText = quranTextOldMadinah;
this.initBases()
const start = performance.now();
const suraWord = "سُورَةُ";
const bism = "بِسْمِ ٱللَّهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ";
const surabismpattern = "^(?<sura>"
+ suraWord + " .*)|(?<bism>"
+ bism
+ "|" + "بِّسْمِ ٱللَّهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ"
+ ")$";
const sajdapatterns = "(وَٱسْجُدْ) وَٱقْتَرِب|(خَرُّوا۟ سُجَّدࣰا)|(وَلِلَّهِ يَسْجُدُ)|(يَسْجُدُونَ)۩|(فَٱسْجُدُوا۟ لِلَّهِ)|(وَٱسْجُدُوا۟ لِلَّهِ)|(أَلَّا يَسْجُدُوا۟ لِلَّهِ)|(وَخَرَّ رَاكِعࣰا)|(يَسْجُدُ لَهُ)|(يَخِرُّونَ لِلْأَذْقَانِ سُجَّدࣰا)|(ٱسْجُدُوا۟) لِلرَّحْمَٰنِ|ٱرْكَعُوا۟ (وَٱسْجُدُوا۟)"; // sajdapatterns.replace("\u0657", "\u08F0").replace("\u065E", "\u08F1").replace("\u0656", "\u08F2");
const sajdaRegExpr = new RegExp(sajdapatterns, "du")
const regexpr = new RegExp(surabismpattern, "u")
const ratio = 0.9;
for (let pageIndex = 0; pageIndex < 2; pageIndex++) {
const pageNumber = pageIndex + 1
this.madinaLineWidths.set(pageNumber * 15 + 2, ratio * 0.5)
this.madinaLineWidths.set(pageNumber * 15 + 3, ratio * 0.7)
this.madinaLineWidths.set(pageNumber * 15 + 4, ratio * 0.9)
this.madinaLineWidths.set(pageNumber * 15 + 5, ratio)
this.madinaLineWidths.set(pageNumber * 15 + 6, ratio * 0.9)
this.madinaLineWidths.set(pageNumber * 15 + 7, ratio * 0.7)
this.madinaLineWidths.set(pageNumber * 15 + 8, ratio * 0.4)
}
this.quranInfo = [];
for (let pageIndex = 0; pageIndex < this._quranText.length; pageIndex++) {
const pageInfo: any[] = []
this.quranInfo.push(pageInfo)
const page = this._quranText[pageIndex];
for (let lineIndex = 0; lineIndex < page.length; lineIndex++) {
const line = page[lineIndex];
const lineInfo: any = {}
pageInfo.push(lineInfo)
lineInfo.lineWidthRatio = this.madinaLineWidths.get((pageIndex + 1) * 15 + lineIndex + 1) || 1
lineInfo.lineType = 0;
const match = line.match(regexpr)
if (match?.groups?.sura) {
lineInfo.lineType = 1
this._outline.push({
name: match?.groups.sura,
pageIndex: pageIndex,
lineIndex: lineIndex
})
} else if (match?.groups?.bism) {
lineInfo.lineType = 2
}
const sajdaMatch = line.match(sajdaRegExpr)
if (sajdaMatch) {
for (let i = 1; i < sajdaMatch.length; i++) {
if (sajdaMatch[i]) {
var pos = (sajdaMatch as any).indices[i]
let startWordIndex = null;
let endWordIndex = null;
let currentWordIndex = 0;
for (let charIndex = 0; charIndex < line.length; charIndex++) {
const char = line.charAt(charIndex);
const isSpace = char === " "
if (startWordIndex == null && charIndex >= pos[0]) {
startWordIndex = currentWordIndex;
}
if (charIndex >= pos[1]) {
endWordIndex = currentWordIndex;
break;
}
if (isSpace) {
currentWordIndex++;
}
}
lineInfo.sajda = { startWordIndex, endWordIndex }
this._sajsdas.push({ pageIndex, lineIndex, startWordIndex, endWordIndex/*, words: sajdaMatch[i]*/ })
}
}
}
}
}
console.info(`sajdasMatched=${this._sajsdas.length}`);
console.log(`QuranTextService constructor=${performance.now() - start}`)
this.initTajweed()
}
private initBases() {
for (let i = 0; i < this.dualJoinLetters.length; i++) {
this.bases.add(this.dualJoinLetters.charCodeAt(i))
}
for (let i = 0; i < this.rightNoJoinLetters.length; i++) {
this.bases.add(this.rightNoJoinLetters.charCodeAt(i))
}
}
isLastBase(text: string, index: number) {
for (let charIndex = index + 1; charIndex < text.length; charIndex++) {
if (this.bases.has(text.charCodeAt(charIndex))) {
return false
}
}
return true
}
nbBases(text: string) {
let nb = 0;
for (let charIndex = 0; charIndex < text.length; charIndex++) {
if (this.bases.has(text.charCodeAt(charIndex))) {
nb++
}
}
return nb
}
initTajweed() {
// Tafkhim
let pattern = "(?<tafkhim1>[طقصخغضظ]\u0651?[\u0652\u064B\u064E\u08F0\u064D\u0650\u08F2\u064C\u064F\u08F1])"
// tafkhim Reh(8 possibilities http://www.quran-tajweed.net/tagweed/index.php/%D8%A7%D9%84%D8%B5%D9%81%D8%A7%D8%AA/%D8%AD%D8%A7%D9%84%D8%A7%D8%AA-%D8%AA%D9%81%D8%AE%D9%8A%D9%85-%D9%88%D8%AA%D8%B1%D9%82%D9%8A%D9%82-%D8%A7%D9%84%D8%B1%D8%A7%D8%A1)
// http://www.dar-alquran.com/Detail.aspx?ArticleID=365
// Dont Tafkhim
// pos kasra /^reh/'lookup black @marks ( @marks | NULL ) ( @marks | NULL ) space /^aya|endofaya/;
// pos /^ behshape / twodotsdown(sukun | NULL) /^ reh / 'lookup black @marks ( @marks | NULL ) ( @marks | NULL ) space /^aya|endofaya/;
pattern += "|(?:\u0650|\u064A\u0652?)ر\\p{Mn}+\\s"
// pos /^reh/'lookup tafkim (shadda | NULL)'lookup tafkim [fatha damma]'lookup tafkim ;
pattern += "|(?<tafkhim2>ر\u0651?[\u064E\u064F])"
// pos wasla (/^reh/ sukun)'lookup tafkim;
// pos [fatha damma] @bases (/dot/ | NULL) sukun (/^reh/ sukun)'lookup tafkim;
// pos [fatha damma] ([/^alef/ /^waw/] | NULL) (/^reh/ sukun)'lookup tafkim;
pattern += "|(?:ٱ|[\u0618\u0619](?:\\p{L}\u0652|[او]?))(?<tafkhim3>ر\u0652)"
// pos [fatha damma] @bases (/dot/ | NULL) sukun /^reh/'lookup tafkim @marks ( @marks | NULL ) ( @marks | NULL ) space /^aya|endofaya/;
// pos [fatha damma] ([/^alef/ /^waw/] | NULL) /^reh/'lookup tafkim @marks ( @marks | NULL ) ( @marks | NULL ) space /^aya|endofaya/;
pattern += "|[\u0618\u0619](?:\\p{L}\u0652|[او]?)(?<tafkhim4>ر)\\p{Mn}+\\s"
// pos kasra (/^reh/ sukun)'lookup tafkim [/^sad/ /^tah/] | [/^ain/ /^hah/] onedotup | /^fehshape/ twodotsup;
pattern += "|\u0650(?<tafkhim5>ر\u0652)[صطغخق]"
this.TafkhimRE = new RegExp(pattern, "gdu");
// gray
pattern = "\\p{L}\\p{Mn}*(?<gray1>ٱ)(?!\u0644\\p{Mn}*ل\\p{Mn}*ه\\p{Mn}*(?:\\s|$))" // همزة الوصل داخل الكلمة
pattern = pattern + "|ٱ(?<gray2>ل(?!\\p{Mn}*ل\\p{Mn}*ه\\p{Mn}*(?:\\s|$)))\\p{L}" // اللام الشمسية
pattern = pattern + "|(?<gray3>\\p{L}\u06DF)"
pattern = pattern + "|(?<gray4>[و])(?=\u0670)|(?<gray4_1>[ى])(?=\u0670\\p{L})"
pattern = pattern + "|(?<gray5>ن)(?= [\u0646يمورل])"
pattern = pattern + "|[\u064E\u064F\u0650](?<gray6>\\p{L})(?=\\s?\\p{L}\u0651)"
//tanween
pattern = pattern + "|(?<tanween1>\u0646[\u06E2\u06ED])|(?<tanween2>\u0646\u0651\\p{Mn})|(?<tanween3>[\u06E2\u06ED] \u0646)|(?<tanween4>\u0645 \u0628)|(?<tanween5>\u0646\\p{L})";
pattern = pattern + "|(?<tanween6>[\u08F0\u08F1\u08F2\u0646])\\p{L}? (?<tanween7>[\u064A\u0648\u0645]\\p{Mn}\\p{Mn}?)"
pattern = pattern + "|(?<tanween8>[\u08F0\u08F1\u08F2\u0646])\\p{L}? [\u0644\u0631]\u0651" // space [/^lam/ /^reh/] shadda
pattern = pattern + "|(?<tanween9>[\u08F0\u08F1\u08F2\u0646])\\p{L}? \\p{L}" // space @bases;
pattern = pattern + "|(?<tanween10>[من]\u0651\\p{Mn})" // /^meem|^noon/'lookup green shadda'lookup green @marks'lookup green;
//madd
pattern = pattern + "|(?<madd1>[او\u0670\u06E5\u06E6]\u0653)(?=\\p{L}\u0651)" // pos ([/^alef/ /^waw/ /^smallalef/ smallwaw smallyeh] maddahabove)'lookup red4 @bases (/dot/ | NULL) shadda;
pattern = pattern + "|(?<madd2>[\u0670\u06E5\u06E6])" // pos [smallwaw smallyeh smallalef.joined smallalef.isol smallalef.replacement]'lookup red1 @bases;
pattern = pattern + "|[او\u0670\u06E5\u06E6]\u0653\\s" // pos ([/^alef/ /^waw/ /^smallalef/ smallwaw smallyeh] maddahabove)' space /^aya|endofaya/;
pattern = pattern + "|(?<madd3>[لم]\u0653)" // pos ([/^lam/ /^meem/ ] maddahabove)'lookup red4;
pattern = pattern + "|(?<madd4>[اويى\u0670\u06E5\u06E6]\u0653)" // pos ([/^alef/ /^waw/ /^alefmaksura/ /^yehshape/ /^smallalef/ smallwaw smallyeh ] maddahabove)'lookup red3;
pattern = pattern + "|(?<madd5>[ياو\u0670]\u0652?)(?=\\p{L}\\p{Mn}\\p{Mn}?\\s)" // pos /^waw/'lookup red2 (sukun | NULL)'lookup red2 @bases @marks ( @marks | NULL ) space /^aya|endofaya/;
// kalkala
pattern = pattern + "|(?<kalkala1>[\u0637\u0642\u062F\u062C\u0628]\u0652)"
pattern = pattern + "|(?<kalkala2>[\u0637\u0642\u062F\u062C\u0628]) \u06DD"
this.OthersRE = new RegExp(pattern, "gdu");
}
getLineInfo(pageIndex: number, lineIndex: number): any {
return this.quranInfo[pageIndex][lineIndex]
}
get outline(): any[] {
return this._outline;
}
get nbPages(): number {
return this._quranText.length
}
get sajdas(): any[] {
return this._sajsdas
}
get quranText(): string[][] {
return this._quranText
}
getTajweed(pageIndex: number, plineIndex: number): Map<number, string> {
let pageResult = this.tajweedResuls.get(pageIndex)
if (pageResult == null) {
pageResult = []
this.tajweedResuls.set(pageIndex, pageResult)
} else {
return pageResult[plineIndex]
}
const pageText = this.quranText[pageIndex];
for (let lineIndex = 0; lineIndex < pageText.length; lineIndex++) {
const lineText = this.quranText[pageIndex][lineIndex]
// TODO
const preText = ""
const postText = ""
const text = preText + lineText + postText
let matches = text.matchAll(this.TafkhimRE);
let match: any
let group;
const result = new Map<number, string>();
pageResult.push(result)
for (match of matches) {
const groups = match.indices.groups
if (group = groups.tafkhim1) {
let firstPos = group[0] - preText.length;
result.set(firstPos, "tafkim")
result.set(firstPos + 1, "tafkim")
if (group[0] + 2 < group[1]) {
result.set(firstPos + 2, "tafkim")
}
} else if (group = groups.tafkhim2) {
let firstPos = group[0] - preText.length;
result.set(firstPos, "tafkim")
result.set(firstPos + 1, "tafkim")
if (group[0] + 2 < group[1]) {
result.set(firstPos + 2, "tafkim")
}
} else if (group = groups.tafkhim3) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "tafkim")
result.set(firstPos + 1, "tafkim")
} else if (group = groups.tafkhim4) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "tafkim")
} else if (group = groups.tafkhim5) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "tafkim")
result.set(firstPos + 1, "tafkim")
}
}
matches = text.matchAll(this.OthersRE);
for (match of matches) {
const groups = match.indices.groups
if (group = groups.tanween1) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lgray");
result.set(firstPos + 1, "green");
} else if (group = groups.tanween2) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "green");
result.set(firstPos + 1, "green");
result.set(firstPos + 2, "green");
} else if (group = groups.tanween3) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "green");
} else if (group = groups.tanween4) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "green");
} else if (group = groups.tanween5) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "green");
} else if (group = groups.tanween6) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lgray");
const tanween7Group = groups.tanween7
const greenPos = tanween7Group[0] - preText.length;
result.set(greenPos, "green");
result.set(greenPos + 1, "green");
if (tanween7Group[0] + 2 < tanween7Group[1]) {
result.set(greenPos + 2, "green");
}
} else if (group = groups.tanween8) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lgray");
} else if (group = groups.tanween9) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "green");
}
else if (group = groups.tanween10) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "green");
result.set(firstPos + 1, "green");
result.set(firstPos + 2, "green");
} else if (group = groups.gray1) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lgray");
} else if (group = groups.gray2) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lgray");
} else if (group = groups.gray3) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lgray");
result.set(firstPos + 1, "lgray");
} else if (group = groups.gray4 || groups.gray4_1) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lgray");
} else if (group = groups.gray5) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lgray");
} else if (group = groups.gray6) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lgray");
} else if (group = groups.kalkala1) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lkalkala");
result.set(firstPos + 1, "lkalkala");
} else if (group = groups.kalkala2) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "lkalkala");
} else if (group = groups.madd1) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "red4");
result.set(firstPos + 1, "red4");
} else if (group = groups.madd2) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "red1");
} else if (group = groups.madd3) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "red4");
result.set(firstPos + 1, "red4");
} else if (group = groups.madd4) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "red3");
result.set(firstPos + 1, "red3");
} else if (group = groups.madd5) {
const firstPos = group[0] - preText.length;
result.set(firstPos, "red2");
if (group[0] + 1 < group[1]) {
result.set(firstPos + 1, "red2");
}
}
}
}
return pageResult[plineIndex]
}
analyzeText(pageIndex: number, plineIndex: number): LineTextInfo {
const key = pageIndex * 15 + plineIndex
let lineTextInfo = this.LineTextInfoCache.get(key)
if (lineTextInfo) return lineTextInfo
lineTextInfo = {
ayaSpaceIndexes: [],
simpleSpaceIndexes: [],
wordInfos: [],
spaces: new Map()
}
this.LineTextInfoCache.set(key, lineTextInfo)
const pageText = this.quranText[pageIndex];
const lineText = this.quranText[pageIndex][plineIndex]
let currentWord: WordInfo = { text: "", startIndex: 0, endIndex: -1, baseText: "" };
lineTextInfo.wordInfos.push(currentWord);
for (let i = 0; i < lineText.length; i++) {
const char = lineText.charAt(i);
if (char === " ") {
if ((lineText.charCodeAt(i - 1) >= 0x0660 && lineText.charCodeAt(i - 1) <= 0x0669) || (lineText.charCodeAt(i + 1) === 0x06DD)) {
lineTextInfo.ayaSpaceIndexes.push(i)
lineTextInfo.spaces.set(i, SpaceType.Aya)
} else {
lineTextInfo.simpleSpaceIndexes.push(i)
lineTextInfo.spaces.set(i, SpaceType.Simple)
}
currentWord = { text: "", startIndex: i + 1, endIndex: i, baseText: "" }
lineTextInfo.wordInfos.push(currentWord);
} else {
currentWord.text += char;
if (this.bases.has(char.charCodeAt(0))) {
currentWord.baseText += char;
}
currentWord.endIndex++;
}
}
return lineTextInfo;
}
}
export const quranTextService = new QuranTextService();
export const PAGE_WIDTH = 17000
export const INTERLINE = 1800
export const TOP = 200
export const MARGIN = 400
export const FONTSIZE = 1000
export const SPACEWIDTH = 100