-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtajweed_classifier.py
433 lines (403 loc) · 17.2 KB
/
tajweed_classifier.py
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
from collections import deque, namedtuple
from tree import Exemplar, json2tree
import glob
import json
import multiprocessing
import os
import sys
import unicodedata
RangeAttributes = namedtuple("Attributes", "start end attributes")
def attributes_for(rule, txt, i, include_this=True, auxiliary_stream=None):
# Determine bounds of this letter.
start_i = i
while start_i and unicodedata.category(txt[start_i]) == "Mn" and (txt[start_i] != "ٰ" or txt[start_i - 1] == "ـ"):
start_i -= 1
end_i = start_i + 1
while end_i < len(txt) and unicodedata.category(txt[end_i]) == "Mn" and txt[end_i] != "ٰ":
end_i += 1
c = txt[i]
c_ext = txt[start_i:end_i]
c_base = txt[start_i]
# Build attributes dict.
res = {}
if auxiliary_stream:
res.update(auxiliary_stream[i])
if rule == "ghunnah":
if not include_this:
res.update({
"base_is_heavy": c_base in "هءحعخغ",
"base_is_noon_or_meem": c_base == "ن" or c_base == "م",
"has_shaddah": "ّ" in c_ext,
"has_tanween": any(s in c_ext for s in "ًٌٍ"),
})
if include_this:
res.update({
"is_noon_or_meem": c == "ن" or c == "م",
"is_initial": i - 1 < 0 or txt[i - 1] == " ",
})
elif rule == "hamzat_wasl":
if include_this:
res.update({
"is_alif_waslah": c == "ٱ",
})
elif rule == "idghaam_ghunnah":
if not include_this:
res.update({
"base_is_idghaam_ghunna_set": c_base in "يمون",
"has_implicit_sukoon": not any(s in c_ext for s in "ًٌٍَُِْ"),
})
if include_this:
res.update({
"is_noon": c == "ن",
"is_tanween": any(s == c for s in "ًٌٍ"),
"is_final": end_i >= len(txt) or txt[end_i] == " ",
})
elif rule == "idghaam_mutajanisayn":
if not include_this:
res.update({
"base_is_nateeyah_a": c_base in "تط",
"base_is_nateeyah_b": c_base in "تد",
"base_is_lathaweeyah_a": c_base in "ثذ",
"base_is_lathaweeyah_b": c_base in "ظذ",
"base_is_meem": c_base == "م",
"base_is_noon": c_base == "ب",
"has_implicit_sukoon": not any(s in c_ext for s in "ًٌٍَُِْ"),
})
elif rule == "idghaam_mutaqaribayn":
if not include_this:
res.update({
"has_implicit_sukoon": not any(s in c_ext for s in "ًٌٍَُِْ"),
"base_is_qaf_kaf": c_base in "كق",
"base_is_lam": c_base == "ل",
"base_is_rah": c_base == "ر",
})
elif rule == "idghaam_no_ghunnah":
if not include_this:
res.update({
"has_implicit_sukoon": not any(s in c_ext for s in "ًٌٍَُِْ"),
"base_is_noon_rah": c_base in "لر",
})
if include_this:
res.update({
"is_noon": c == "ن",
"is_tanween": any(s == c for s in "ًٌٍ"),
"is_final": end_i >= len(txt) or txt[end_i] == " ",
})
elif rule == "idghaam_shafawi":
if not include_this:
res.update({
"has_implicit_sukoon": not any(s in c_ext for s in "ًٌٍَُِْ"),
"base_is_meem": c_base == "م",
})
elif rule == "ikhfa":
if not include_this:
res.update({
"has_implicit_sukoon": not any(s in c_ext for s in "ًٌٍَُِْ"),
"base_is_ikhfa_set": c_base in "تثجدذزسشصضطظفقك",
})
if include_this:
res.update({
"is_noon": c == "ن",
"is_high_noon": c == "ۨ",
"is_tanween": any(s == c for s in "ًٌٍ"),
"is_final": end_i >= len(txt) or txt[end_i] == " ",
})
elif rule == "ikhfa_shafawi":
if not include_this:
res.update({
"has_implicit_sukoon": not any(s in c_ext for s in "ًٌٍَُِْ"),
"base_is_meem": c_base == "م",
})
elif rule == "iqlab":
if not include_this:
res.update({
"has_tanween": any(s in c_ext for s in "ًٌٍ"),
"has_small_meem": "ۢ" in c_ext or "ۭ" in c_ext,
})
if include_this:
res.update({
"is_tanween": c in "ًٌٍ",
"is_base": (unicodedata.category(c) != "Mn" and c != "ـ") or c == "ٰ",
})
elif rule == "lam_shamsiyyah":
if not include_this:
res.update({
"has_vowel_incl_tanween": any(s in c_ext for s in "ًٌٍَُِْ"),
"has_shaddah": "ّ" in c_ext,
})
if include_this:
res.update({
"is_alif_waslah": c == "ٱ",
"is_lam": c == "ل",
"is_allah_word_start": txt[start_i:start_i + 7] in ("للَّهِ ", "للَّهُ ", "للَّهَ "),
})
elif rule == "madd_2":
if not include_this:
res.update({
"has_maddah": "ٓ" in c_ext,
"has_hamza": any(s in c_ext for s in "ؤئٕإأٔ"),
"has_vowel_incl_tanween": any(s in c_ext for s in "ًٌٍَُِْ"),
"has_proc_sukoon": "۟" in c_ext or "ْ" in c_ext or not any(s in c_ext for s in "ًٌٍَُِْ"),
"is_final_letter_in_ayah": end_i >= len(txt),
})
if include_this:
res.update({
"is_dagger_alif": c == "ٰ",
"is_small_yeh": c == "ۦ",
"is_small_waw": c == "ۥ",
"is_final": end_i >= len(txt) or txt[end_i] == " ",
})
elif rule == "madd_246":
if not include_this:
res.update({
"has_maddah": "ٓ" in c_ext,
"has_fathah": "َ" in c_ext,
"has_dammah": "ُ" in c_ext,
"has_kasrah": "ِ" in c_ext,
"has_vowel_no_tanween": any(s in c_ext for s in "َُِْ"),
"has_tanween": any(s in c_ext for s in "ًٌٍ"),
})
if include_this:
res.update({
"is_alif": c == "ا",
"is_yeh": c == "ي",
"is_waw": c == "و",
})
elif rule == "madd_6":
if not include_this:
res.update({
"has_maddah": "ٓ" in c_ext,
"has_explicit_sukoon": "۟" in c_ext or "ْ" in c_ext,
"has_vowel_incl_tanween": any(s in c_ext for s in "ًٌٍَُِْ"),
"has_shaddah": "ّ" in c_ext,
"has_hamza": any(s in c_ext for s in "ؤئٕإأٔ"),
"base_is_alif_maksura": c_base == "ى",
})
if include_this:
res.update({
"is_hamza": c == "ء",
"is_base": (unicodedata.category(c) != "Mn" and c != "ـ") or c == "ٰ",
"is_final": end_i >= len(txt) or txt[end_i] == " ",
})
elif rule in ("madd_munfasil", "madd_muttasil"):
if not include_this:
res.update({
"has_maddah": "ٓ" in c_ext,
"has_explicit_sukoon": "۟" in c_ext or "ْ" in c_ext,
"has_non_initial_hamza": any(s in c_ext for s in "ؤئٕٔ"),
"base_is_isolated_hamza": c_base == "ء",
"has_initial_hamza": any(s in c_ext for s in "ٕإأ"),
# The following attributes permit this to work without inspecting for maddah(?):
# "has_implicit_sukoon": not any(s in c_ext for s in "ًٌٍَُِْ"),
# "has_explicit_sukoon_mod": "۟" in c_ext or "ْ" in c_ext or "۠" in c_ext,
# "has_fathah": "َ" in c_ext,
# "has_dammah": "ُ" in c_ext,
# "has_kasrah": "ِ" in c_ext,
})
if include_this:
res.update({
"is_base": (unicodedata.category(c) != "Mn" and c != "ـ") or c == "ٰ",
"is_alif": c == "ا",
"is_dagger_alif": c == "ٰ",
"is_alif_maksura": c == "ى",
"is_final": end_i >= len(txt) or txt[end_i] == " ",
"is_space": c == " ",
})
elif rule == "qalqalah":
if not include_this:
res.update({
"has_explicit_sukoon": "۟" in c_ext or "ْ" in c_ext,
"has_maddah": "ٓ" in c_ext,
})
if include_this:
res.update({
"is_muqalqalah": c in "بدجطق",
})
elif rule == "silent":
if not include_this:
res.update({
"has_silent_circle": "۟" in c_ext,
"has_vowel_incl_tanween": any(s in c_ext for s in "ًٌٍَُِْ"),
"base_is_dagger_alif": c_base == "ٰ",
})
if include_this:
res.update({
"precedes_high_seen": i + 1 < len(txt) and txt[i + 1] == "ۜ",
"is_alif": c == "ا",
"is_alif_maksura": c == "ى",
"is_waw": c == "و",
"is_yeh": c == "ي",
})
elif rule == "END":
if not include_this:
res.update({
"base_is_space": c_base == " ",
"has_no_diacritics": start_i + 1 == end_i,
"has_high_noon": "ۨ" in c_ext,
"has_explicit_sukoon": "۟" in c_ext or "ْ" in c_ext,
})
if include_this:
res.update({
"is_base": (unicodedata.category(c) != "Mn" and c != "ـ") or c == "ٰ",
"is_final_codepoint_in_letter": i + 1 == end_i,
"is_final_letter_in_ayah": end_i >= len(txt),
})
else:
raise RuntimeError("Unknown rule %s" % rule)
return RangeAttributes(start_i, end_i, res)
def exemplars_for(rule, txt, auxiliary_stream=None):
context_size_map = {
"ghunnah": (3, 1),
"hamzat_wasl": (1, 0),
"idghaam_ghunnah": (1, 3),
"idghaam_mutajanisayn": (0, 2),
"idghaam_mutaqaribayn": (1, 2),
"idghaam_no_ghunnah": (0, 3),
"idghaam_shafawi": (0, 2),
"ikhfa": (0, 3),
"ikhfa_shafawi": (0, 2),
"iqlab": (0, 2),
"lam_shamsiyyah": (1, 1),
"madd_2": (0, 1),
"madd_246": (1, 2),
"madd_6": (1, 1),
"madd_munfasil": (1, 2),
"madd_muttasil": (0, 3),
"qalqalah": (1, 1),
"silent": (0, 1),
"END": (1, 0)
}
lookbehind, lookahead = context_size_map[rule]
# Use a circular buffer to store the letter attributes.
# We calculate the codepoint attributes - which are slightly different - within the main loop.
# Pre-fill the buffer with empty data representing the initial lookbehind.
letter_attr_buffer = deque([RangeAttributes(-1, 0, None) for x in range(lookbehind)],
maxlen=lookbehind + 1 + lookahead)
# Prime with real present-letter & lookahead data.
for x in range(lookahead + 1):
start_idx = letter_attr_buffer[-1].end if letter_attr_buffer else 0
range_attrs = attributes_for(rule,
txt,
start_idx,
include_this=False,
auxiliary_stream=auxiliary_stream)
letter_attr_buffer.append(range_attrs)
if range_attrs.end == len(txt):
break
# If we ran out of letters before filling the lookahead, top it off.
for x in range(lookbehind + 1 + lookahead - len(letter_attr_buffer)):
letter_attr_buffer.append(RangeAttributes(len(txt), len(txt), None))
for i in range(len(txt)):
# Advance letter buffer if required.
if i >= letter_attr_buffer[lookbehind].end:
if letter_attr_buffer[-1].end == len(txt):
letter_attr_buffer.append(RangeAttributes(len(txt), len(txt), None))
else:
letter_attr_buffer.append(attributes_for(rule,
txt,
letter_attr_buffer[-1].end,
include_this=False,
auxiliary_stream=auxiliary_stream))
assert i < letter_attr_buffer[lookbehind].end, "Next letter did not advance"
# Build final attribute dictionary.
attr_full = {}
for off in range(lookbehind + 1 + lookahead):
if letter_attr_buffer[off].attributes is None:
attr_full.update({"%d_exists" % (off - lookbehind): False})
else:
attr_full.update({"%d_%s" % (off - lookbehind, k): v
for k, v in letter_attr_buffer[off].attributes.items()})
attr_full.update({"%d_exists" % (off - lookbehind): True})
attr_full.update({"0_%s" % k: v
for k, v in attributes_for(rule,
txt,
i,
include_this=True,
auxiliary_stream=auxiliary_stream).attributes.items()})
yield Exemplar(None, attr_full, 1)
def run_tree(tree, exemplar):
while not hasattr(tree, "label"):
if exemplar.attributes.get(tree.attribute, -1) >= tree.value:
tree = tree.gt
else:
tree = tree.lt
return tree.label
def label_ayah(params):
surah, ayah, text, rule_trees = params # Multiprocessing...
# We have to cut out the basmala since it is, in effect, a separate verse.
# Rules that depend on ayah start/end stop working if it's kept in place.
# But we remember the offset so we can put everything back where we found it.
offset = 0
if surah not in (1, 9) and ayah == 1:
old_text = text
text = " ".join(text.split(" ")[4:])
offset = len(old_text) - len(text)
# Initialize exemplar generators.
rules_start_exemplars = {
k: exemplars_for(k, text) for k in rule_trees
}
# All the rules use the same exemplars for making end decisions.
end_exemplars = exemplars_for("END", text)
annotations = []
annotations_run = {k: deque() for k in rule_trees}
letter_start = 0
last_letter_start = 0
for i in range(len(text)):
end_e = next(end_exemplars)
# We need some bookkeeping in here for the end-rule trees.
# I made a one-size-fits-all solution in exemplars_for's auxiliary stream
# that fits exactly one case - and it's not this one.
# Also, note that "-1_in_rule" refers to the first character in the letter group.
# If the rule starts with some harakat in the letter, it will still be false!
# There is 1 case where this happens: ikhfa on 21:88
if unicodedata.category(text[i]) != "Mn" or text[i] == "ٰ":
last_letter_start = letter_start
letter_start = i
for k, trees in rule_trees.items():
e = next(rules_start_exemplars[k])
if run_tree(trees["start"], e):
annotations_run[k].append(i)
# Hax - the exemplars_for auxiliary stream parameter must be random access.
# So just paste the value we need in here.
end_e.attributes.update({
"0_in_rule": len(annotations_run[k]) > 0,
"-1_in_rule": any(x <= last_letter_start for x in annotations_run[k])
})
if run_tree(trees["end"], end_e):
annotations.append({
"rule": k,
"start": annotations_run[k].popleft() + offset,
"end": i + 1 + offset
})
assert all(len(q) == 0 for q in annotations_run.values()), \
"Some rules left hanging at end of ayah @ %d: %s (%d:%d) %s" % \
(len(text), annotations_run, surah, ayah, annotations)
return {
"surah": surah,
"ayah": ayah,
"annotations": sorted(annotations, key=lambda x: x["start"])
}
if __name__ == "__main__":
# Load rules from incredibly high-tech datastore.
rule_trees = {}
rule_start_files = glob.glob("output/rule_trees/*.start.json")
for start_file in rule_start_files:
rule_name = os.path.basename(start_file).partition(".")[0]
end_file = start_file.replace(".start.", ".end.")
rule_trees[rule_name] = {
"start": json2tree(json.load(open(start_file))),
"end": json2tree(json.load(open(end_file))),
}
# Read in text to classify
tasks = []
for line in sys.stdin:
line = line.split("|")
if len(line) != 3:
continue
tasks.append((int(line[0]), int(line[1]), line[2].strip(), rule_trees))
# Perform classification.
with multiprocessing.Pool() as p:
results = p.map(label_ayah, tasks)
# Pretty-print output because disk space is cheap.
json.dump(results, sys.stdout, indent=2, sort_keys=True)
print("")