pyarud 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyarud/__init__.py ADDED
File without changes
pyarud/arudi.py ADDED
@@ -0,0 +1,343 @@
1
+ import re
2
+
3
+ from pyarabic.araby import strip_tashkeel
4
+
5
+
6
+ class ArudiConverter:
7
+ def __init__(self):
8
+ self.harakat = ["\u0650", "\u064e", "\u064f"] # kasra, fatha, damma
9
+ self.sukun = ["\u0652"] # sukun
10
+ self.mostly_saken = ["\u0627", "\u0648", "\u0649", "\u064a"] # alef, waw, alef maqsurah, ya'a
11
+ self.tnween_chars = ["\u064c", "\u064d", "\u064b"] # damm, kasra, fatha tanween
12
+ self.shadda_chars = ["\u0651"]
13
+ self.all_chars = list("إةابتثجحخدذرزسشصضطظعغفقكلمنهويىأءئؤ ")
14
+ self.prem_chars = (
15
+ self.harakat + self.sukun + self.mostly_saken + self.tnween_chars + self.shadda_chars + self.all_chars
16
+ )
17
+
18
+ # Word replacements for Arudi writing
19
+ self.CHANGE_LST = {
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
+ def _handle_space(self, plain_chars):
45
+ if plain_chars[-1] == " ":
46
+ return plain_chars[:-2]
47
+ else:
48
+ return plain_chars[:-1]
49
+
50
+ def _remove_extra_harakat(self, text):
51
+ out = ""
52
+ i = 0
53
+ while i < len(text):
54
+ if i < len(text) - 1:
55
+ if text[i] in self.harakat and text[i + 1] in self.harakat:
56
+ i += 1
57
+ continue
58
+ out += text[i]
59
+ i += 1
60
+ return out
61
+
62
+ def _process_specials_before(self, bait):
63
+ # Handle specific starting Alif cases
64
+ if bait and bait[0] == "ا":
65
+ # Heuristic: randomly choose or based on context. Bohour used random.
66
+ # We'll default to Fatha for consistency in deterministic output,
67
+ # or Hamza with Fatha.
68
+ bait = "أَ" + bait[1:]
69
+
70
+ bait = bait.replace("وا ", "و ")
71
+ if bait.endswith("وا"):
72
+ bait = bait[:-1]
73
+
74
+ bait = bait.replace("وْا", "و")
75
+ if bait.endswith("وْا"):
76
+ bait = bait[:-2] + "و"
77
+
78
+ # Common substitutions
79
+ bait = bait.replace("الله", "اللاه")
80
+ bait = bait.replace("اللّه", "الله")
81
+ bait = bait.replace("إلَّا", "إِلّا")
82
+ bait = bait.replace("نْ ال", "نَ ال")
83
+ bait = bait.replace("لْ ال", "لِ ال")
84
+ bait = bait.replace("إلَى", "إِلَى")
85
+ bait = bait.replace("إذَا", "إِذَا")
86
+ bait = bait.replace("ك ", "كَ ")
87
+ bait = bait.replace(" ال ", " الْ ")
88
+ bait = bait.replace("ْ ال", "ِ ال")
89
+
90
+ # Word replacements from CHANGE_LST
91
+ out = []
92
+ valid_prefixes = ["و", "ف", "ك", "ب", "ل", "وب", "فك", "ول", "فل"]
93
+
94
+ for word in bait.split(" "):
95
+ cleaned_word = strip_tashkeel(word)
96
+ found = False
97
+
98
+ # 1. Exact match check
99
+ for key, replacement in self.CHANGE_LST.items():
100
+ if cleaned_word == key:
101
+ out.append(replacement)
102
+ found = True
103
+ break
104
+
105
+ # 2. Prefix check if not found
106
+ if not found:
107
+ for key, replacement in self.CHANGE_LST.items():
108
+ if cleaned_word.endswith(key):
109
+ prefix = cleaned_word[:-len(key)]
110
+ if prefix in valid_prefixes:
111
+ # We found a prefixed match.
112
+ # We need to reconstruct the word with the original prefix's diacritics
113
+ # This is tricky because 'word' has diacritics intermixed.
114
+ # Simple heuristic: Take the original word string up to the match?
115
+ # No, diacritics make length differ.
116
+ # Better: Just prepend the prefix chars?
117
+ # "وَهَذَا" -> prefix "وَ" ?
118
+ # We know cleaned prefix is "و".
119
+ # Let's try to find where the key starts in the original word.
120
+
121
+ # Find the index of the key's first char in the original word (last occurrence)
122
+ # ... This assumes standard orthography.
123
+
124
+ # Simplest robust approach for Arudi:
125
+ # Just use the cleaned prefix + replacement?
126
+ # "وهذا" -> "و" + "هَاذَا" -> "وهَاذَا"
127
+ # But we lose the prefix's original harakat (e.g. "وَ").
128
+ # Ideally we want "وَ" + "هَاذَا".
129
+
130
+ # Hack: Since prefixes are usually 1-2 chars, we can assume they are at the start.
131
+ # But we don't know their length in the original string (due to harakat).
132
+
133
+ # Alternative: Use regex to find the suffix in the original word?
134
+ # Or just use the predefined "cleaned prefix" + standard haraka?
135
+ # "و" -> "وَ" (Fatha usually). "ب" -> "بِ" (Kasra). "ل" -> "لِ" (Kasra).
136
+ # "ك" -> "كَ" (Fatha). "ف" -> "فَ" (Fatha).
137
+
138
+ prefix_harakat = {
139
+ "و": "وَ", "ف": "فَ", "ك": "كَ", "ب": "بِ", "ل": "لِ"
140
+ }
141
+
142
+ # Construct new word
143
+ new_prefix = ""
144
+ for p_char in prefix:
145
+ new_prefix += prefix_harakat.get(p_char, p_char) # Default to char if no mapping
146
+
147
+ out.append(new_prefix + replacement)
148
+ found = True
149
+ break
150
+
151
+ if not found:
152
+ out.append(word)
153
+
154
+ bait = " ".join(out)
155
+
156
+ # Ensure second char isn't a bare letter if first is
157
+ if len(bait) > 1 and bait[1] in self.all_chars:
158
+ bait = bait[0] + self.harakat[1] + bait[1:]
159
+
160
+ # Filter trailing alif after tanween
161
+ final_chars = []
162
+ i = 0
163
+ while i < len(bait):
164
+ if bait[i] == "ا" and i > 0 and bait[i - 1] in self.tnween_chars:
165
+ i += 1
166
+ # skip following harakat if any
167
+ if i < len(bait) and bait[i] in self.harakat + self.sukun + self.tnween_chars + self.shadda_chars:
168
+ i += 1
169
+ continue
170
+ final_chars.append(bait[i])
171
+ i += 1
172
+
173
+ return "".join(final_chars)
174
+
175
+ def _process_specials_after(self, bait):
176
+ bait = bait.replace("ةن", "تن")
177
+ return bait
178
+
179
+ def _extract_pattern(self, text):
180
+ """
181
+ Core logic to extract binary pattern and arudi text.
182
+ Based on Bohour's extract_tf3eelav3.
183
+ """
184
+ text = self._remove_extra_harakat(text)
185
+ chars = list(text.replace("\u0622", "ءَا").strip()) # Replace Madda
186
+ chars = [c for c in chars if c in self.prem_chars]
187
+ chars = list(re.sub(" +", " ", "".join(chars).strip()))
188
+
189
+ out_pattern = ""
190
+ plain_chars = ""
191
+
192
+ i = 0
193
+ while i < len(chars) - 1:
194
+ char = chars[i]
195
+
196
+ if char in self.all_chars:
197
+ if char == " ":
198
+ plain_chars += char
199
+ i += 1
200
+ continue
201
+
202
+ # Lookahead
203
+ next_char = chars[i + 1]
204
+ if next_char == " " and i + 2 < len(chars):
205
+ next_char = chars[i + 2]
206
+
207
+ next_next_char = None
208
+ if i < len(chars) - 2:
209
+ next_next_char = chars[i + 2]
210
+
211
+ prev_digit = out_pattern[-1] if len(out_pattern) > 0 else ""
212
+
213
+ # Logic
214
+ if next_char in self.harakat:
215
+ out_pattern += "1"
216
+ plain_chars += char
217
+
218
+ elif next_char in self.sukun:
219
+ if prev_digit != "0":
220
+ out_pattern += "0"
221
+ plain_chars += char
222
+ elif (i + 1) == len(chars) - 1:
223
+ # End of line sukun handling
224
+ out_pattern = out_pattern[:-1] + "10"
225
+ plain_chars += char
226
+ else:
227
+ plain_chars = self._handle_space(plain_chars) + char
228
+
229
+ elif next_char in self.tnween_chars:
230
+ if char != "ا":
231
+ plain_chars += char
232
+ plain_chars += "ن"
233
+ out_pattern += "10"
234
+
235
+ elif next_char in self.shadda_chars:
236
+ if prev_digit != "0":
237
+ plain_chars += char + char
238
+ out_pattern += "01"
239
+ else:
240
+ plain_chars = self._handle_space(plain_chars) + char + char
241
+ out_pattern += "1"
242
+
243
+ # Check what follows Shadda
244
+ if i + 2 < len(chars):
245
+ if chars[i + 2] in self.harakat:
246
+ i += 1 # Skip harakat processing next loop (handled here implicitly?)
247
+ # Actually Bohour logic just increments i to skip processing the harakah as separate char?
248
+ # But we added '1' for the second letter of shadda.
249
+ pass
250
+ elif chars[i + 2] in self.tnween_chars:
251
+ i += 1
252
+ plain_chars += "ن"
253
+ out_pattern += "0" # Shadda(1) + Tanween(0) -> 10. Wait. Shadda is 01.
254
+ # If Shadda + Tanween:
255
+ # Letter + Shadda + Tanween
256
+ # 1. Letter Sakin (0)
257
+ # 2. Letter Mutaharrik (1)
258
+ # 3. Tanween (0)
259
+ # Result should be 010.
260
+ # Above logic adds 01, then adds 0. Correct.
261
+
262
+ elif next_char == "ا":
263
+ out_pattern += "10"
264
+ plain_chars += char + "ا"
265
+
266
+ elif next_char in self.all_chars:
267
+ # Letter followed by Letter (implies first is Sakin if no haraka in betweeen?)
268
+ # Or assumes implicit sukun?
269
+ if prev_digit != "0":
270
+ out_pattern += "0"
271
+ plain_chars += char
272
+ elif prev_digit == "0" and i + 1 < len(chars) and chars[i + 1] == " ":
273
+ # Special case from Bohour
274
+ out_pattern += "1"
275
+ plain_chars += char
276
+ else:
277
+ plain_chars = self._handle_space(plain_chars) + char
278
+ i -= 1 # Backtrack? This logic in Bohour is tricky.
279
+ # If we assumed it was a letter but it's followed by a letter, we treat current as sakin.
280
+ # The i -= 1 might be to re-process? No, i += 2 at end.
281
+
282
+ # Ha' al-Gha'ib (He) handling
283
+ if next_next_char == " ":
284
+ if char == "ه":
285
+ if next_char == self.harakat[0]: # Kasra
286
+ plain_chars += "ي"
287
+ out_pattern += "0"
288
+ if next_char == self.harakat[2]: # Damma
289
+ plain_chars += "و"
290
+ out_pattern += "0"
291
+
292
+ i += 2 # Advance past char and its diacritic/follower
293
+ elif char == "ا":
294
+ # Alef encountered as 'char' (e.g. after a diacritic consumed the previous letter)
295
+ out_pattern += "0"
296
+ plain_chars += char
297
+ i += 1
298
+ else:
299
+ i += 1
300
+
301
+ # Finalize
302
+ if out_pattern and out_pattern[-1] != "0":
303
+ out_pattern += "0" # Always end with sukun (Qafiyah)
304
+
305
+ # Ashba' (Saturation) of last letter
306
+ if chars:
307
+ last_char = chars[-1]
308
+ if last_char == self.harakat[0]: # Kasra
309
+ plain_chars += "ي"
310
+ elif last_char == self.tnween_chars[1]: # Kasr Tanween
311
+ plain_chars = plain_chars[:-1] + "ي"
312
+ elif last_char == self.harakat[1]: # Fatha
313
+ plain_chars += "ا"
314
+ elif last_char == self.harakat[2]: # Damma
315
+ plain_chars += "و"
316
+ elif last_char == self.tnween_chars[0]: # Damm Tanween
317
+ plain_chars = plain_chars[:-1] + "و"
318
+ elif last_char in "ىاوي" and len(chars) > 1 and chars[-2] not in self.tnween_chars:
319
+ plain_chars += last_char
320
+
321
+ return plain_chars, out_pattern
322
+
323
+ def prepare_text(self, text):
324
+ """
325
+ Converts standard Arabic text into Arudi style and extracts the binary pattern.
326
+
327
+ Args:
328
+ text (str): The input Arabic text (hemistich or line).
329
+
330
+ Returns:
331
+ tuple[str, str]: A tuple containing:
332
+ - `arudi_style` (str): The phonetic Arudi representation (e.g., "مُسْتَفْعِلُنْ").
333
+ - `pattern` (str): The binary pattern string (e.g., "1010110").
334
+ """
335
+ text = text.strip()
336
+ if not text:
337
+ return "", ""
338
+
339
+ preprocessed = self._process_specials_before(text)
340
+ arudi_style, pattern = self._extract_pattern(preprocessed)
341
+ arudi_style = self._process_specials_after(arudi_style)
342
+
343
+ return arudi_style, pattern