-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar_encoder.py
More file actions
450 lines (389 loc) · 15.4 KB
/
grammar_encoder.py
File metadata and controls
450 lines (389 loc) · 15.4 KB
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
import requests
import json
from typing import Dict, List, Set, Tuple
from collections import defaultdict
from rdflib import Graph, Namespace, URIRef, Literal, RDF, RDFS, OWL
from dataclasses import dataclass
@dataclass
class GrammarRule:
"""Represents a Quranic Arabic grammar rule"""
rule_id: str
rule_type: str # pronoun_agreement, verb_subject, case_marking, etc.
conditions: Dict
action: str
confidence: float
source: str # Classical grammar text reference
class QuranGrammarEncoder:
"""
Extracts and encodes Quranic Arabic grammar rules from the
Quranic Arabic Corpus into a computable OWL ontology
"""
def __init__(self):
self.corpus_base = "http://corpus.quran.com"
self.grammar_rules = []
self.grammar_graph = Graph()
# Define namespaces
self.QG = Namespace("http://ontology.quran/grammar#")
self.QURAN = Namespace("http://ontology.quran/")
self.grammar_graph.bind("qg", self.QG)
self.grammar_graph.bind("quran", self.QURAN)
# Classical grammar categories (based on traditional I'rab)
self.pos_categories = {
'N': 'Noun', # اسم
'V': 'Verb', # فعل
'P': 'Particle', # حرف
'PRON': 'Pronoun', # ضمير
'ADJ': 'Adjective', # صفة
'ADV': 'Adverb' # ظرف
}
# Case markers (I'rab)
self.case_markers = {
'NOM': 'Nominative', # مرفوع (subject)
'ACC': 'Accusative', # منصوب (object)
'GEN': 'Genitive' # مجرور (possessed/prepositional)
}
# Verb forms
self.verb_forms = {
'PERF': 'Perfect', # ماض
'IMPERF': 'Imperfect', # مضارع
'IMP': 'Imperative' # أمر
}
# Person/Number/Gender
self.morphology = {
'person': ['1', '2', '3'],
'number': ['S', 'D', 'P'], # Singular, Dual, Plural
'gender': ['M', 'F']
}
def fetch_corpus_morphology(self, surah: int, verse: int) -> List[Dict]:
"""
Fetch word-by-word morphological analysis from Quranic Arabic Corpus
This is based on classical grammar scholarship
"""
# The Corpus provides morphological tagging based on:
# - Traditional I'rab analysis
# - Classical grammar frameworks (Sibawayh, etc.)
url = f"{self.corpus_base}/documentation/morphologicalannotation.jsp"
# Structure from Corpus (example for real implementation):
# Each word has: position, arabic_text, root, lemma, pos, features
# For now, defining the structure we'll pull:
morphology_data = {
'surah': surah,
'verse': verse,
'words': []
}
# The Corpus API returns JSON with structure like:
# {
# "word": "اللَّهُ",
# "pos": "N",
# "case": "NOM",
# "person": "3",
# "number": "S",
# "gender": "M",
# "root": "ء ل ه",
# "lemma": "Allah"
# }
return morphology_data
def extract_pronoun_rules(self) -> List[GrammarRule]:
"""
Extract pronoun-antecedent agreement rules from classical grammar
Based on traditional ضمير (pronoun) analysis
"""
rules = []
# RULE 1: Person Agreement
# A pronoun must agree with its antecedent in person
rules.append(GrammarRule(
rule_id="PRON_PERSON_AGREEMENT",
rule_type="pronoun_agreement",
conditions={
'source_pos': 'PRON',
'check': 'person_match'
},
action="link_if_person_matches",
confidence=1.0,
source="Classical I'rab - Sibawayh Al-Kitab"
))
# RULE 2: Number Agreement
rules.append(GrammarRule(
rule_id="PRON_NUMBER_AGREEMENT",
rule_type="pronoun_agreement",
conditions={
'source_pos': 'PRON',
'check': 'number_match'
},
action="link_if_number_matches",
confidence=1.0,
source="Classical I'rab - Al-Ajrumiyyah"
))
# RULE 3: Gender Agreement
rules.append(GrammarRule(
rule_id="PRON_GENDER_AGREEMENT",
rule_type="pronoun_agreement",
conditions={
'source_pos': 'PRON',
'check': 'gender_match'
},
action="link_if_gender_matches",
confidence=1.0,
source="Classical I'rab - Ibn Ajurrum"
))
# RULE 4: Proximity Preference
# When multiple candidates match, prefer closest antecedent
rules.append(GrammarRule(
rule_id="PRON_PROXIMITY",
rule_type="pronoun_coreference",
conditions={
'source_pos': 'PRON',
'multiple_candidates': True
},
action="select_nearest_matching_antecedent",
confidence=0.85,
source="Classical I'rab - Contextual Analysis"
))
# RULE 5: Divine Reference Priority
# When a pronoun could refer to Allah or another entity,
# and both match grammatically, prefer divine reference
rules.append(GrammarRule(
rule_id="PRON_DIVINE_PRIORITY",
rule_type="pronoun_coreference",
conditions={
'source_pos': 'PRON',
'candidate_types': ['DIVINE', 'OTHER'],
'grammar_match': 'both'
},
action="prefer_divine_antecedent",
confidence=0.9,
source="Quranic Exegesis - Tafsir Tradition"
))
return rules
def extract_verb_subject_rules(self) -> List[GrammarRule]:
"""
Extract verb-subject dependency rules
Based on فعل-فاعل (verb-subject) classical analysis
"""
rules = []
# RULE 6: Verb-Subject Agreement
rules.append(GrammarRule(
rule_id="VERB_SUBJECT_AGREEMENT",
rule_type="verb_subject",
conditions={
'verb_pos': 'V',
'subject_pos': 'N',
'subject_case': 'NOM'
},
action="create_edge_verb_to_subject",
confidence=1.0,
source="Classical I'rab - فاعل must be مرفوع"
))
# RULE 7: VSO Word Order
# In Quranic Arabic, default is Verb-Subject-Object
rules.append(GrammarRule(
rule_id="VSO_ORDER",
rule_type="word_order",
conditions={
'pattern': 'V-N-N',
'case': ['NOM', 'ACC']
},
action="first_noun_subject_second_object",
confidence=0.95,
source="Classical Syntax - VSO Default"
))
# RULE 8: Implicit Subject (Pronoun Drop)
# Arabic allows pronoun drop - verb conjugation implies subject
rules.append(GrammarRule(
rule_id="IMPLICIT_SUBJECT",
rule_type="verb_subject",
conditions={
'verb_has_conjugation': True,
'no_explicit_subject': True
},
action="infer_subject_from_verb_morphology",
confidence=1.0,
source="Classical Grammar - Pronoun Drop"
))
return rules
def extract_case_marking_rules(self) -> List[GrammarRule]:
"""
Extract I'rab (case marking) rules that determine syntactic roles
"""
rules = []
# RULE 9: Nominative = Subject/Predicate
rules.append(GrammarRule(
rule_id="NOM_CASE_ROLE",
rule_type="case_marking",
conditions={
'case': 'NOM'
},
action="mark_as_subject_or_predicate",
confidence=1.0,
source="Classical I'rab - مرفوع Function"
))
# RULE 10: Accusative = Object/Adverbial
rules.append(GrammarRule(
rule_id="ACC_CASE_ROLE",
rule_type="case_marking",
conditions={
'case': 'ACC'
},
action="mark_as_object_or_adverbial",
confidence=1.0,
source="Classical I'rab - منصوب Function"
))
# RULE 11: Genitive = Possession/Prepositional
rules.append(GrammarRule(
rule_id="GEN_CASE_ROLE",
rule_type="case_marking",
conditions={
'case': 'GEN'
},
action="mark_as_possessed_or_prepositional",
confidence=1.0,
source="Classical I'rab - مجرور Function"
))
return rules
def extract_particle_rules(self) -> List[GrammarRule]:
"""
Extract rules for particles (حروف) that modify meaning/dependencies
"""
rules = []
# RULE 12: Negation Particles
rules.append(GrammarRule(
rule_id="NEGATION_PARTICLE",
rule_type="particle_function",
conditions={
'particle': ['لا', 'ما', 'لم', 'لن'],
'scope': 'following_verb'
},
action="negate_following_clause",
confidence=1.0,
source="Classical Grammar - Negation Particles"
))
# RULE 13: Prepositions Create Genitive
rules.append(GrammarRule(
rule_id="PREPOSITION_GOVERNS_GEN",
rule_type="particle_function",
conditions={
'particle_type': 'PREP',
'following_noun': True
},
action="following_noun_must_be_genitive",
confidence=1.0,
source="Classical I'rab - Preposition Governance"
))
return rules
def build_grammar_ontology(self) -> Graph:
"""
Build OWL ontology encoding all grammar rules
"""
# Define grammar rule classes
self.grammar_graph.add((
self.QG.GrammarRule,
RDF.type,
OWL.Class
))
self.grammar_graph.add((
self.QG.GrammarRule,
RDFS.comment,
Literal("A formal encoding of classical Quranic Arabic grammar rules")
))
# Define rule types as subclasses
rule_types = [
'PronounAgreementRule',
'VerbSubjectRule',
'CaseMarkingRule',
'ParticleFunctionRule',
'CoreferenceRule'
]
for rule_type in rule_types:
rule_uri = self.QG[rule_type]
self.grammar_graph.add((rule_uri, RDF.type, OWL.Class))
self.grammar_graph.add((rule_uri, RDFS.subClassOf, self.QG.GrammarRule))
# Define properties
properties = {
'hasCondition': 'Specifies conditions that must be met for rule application',
'hasAction': 'Specifies the action to take when rule applies',
'hasConfidence': 'Confidence score (0-1) for rule application',
'hasSource': 'Classical grammar text this rule derives from',
'appliesTo': 'The morphological category this rule applies to'
}
for prop, comment in properties.items():
prop_uri = self.QG[prop]
self.grammar_graph.add((prop_uri, RDF.type, OWL.ObjectProperty))
self.grammar_graph.add((prop_uri, RDFS.comment, Literal(comment)))
# Extract all rule categories
all_rules = []
all_rules.extend(self.extract_pronoun_rules())
all_rules.extend(self.extract_verb_subject_rules())
all_rules.extend(self.extract_case_marking_rules())
all_rules.extend(self.extract_particle_rules())
# Add each rule to ontology
for rule in all_rules:
rule_uri = self.QG[rule.rule_id]
# Add as individual
self.grammar_graph.add((rule_uri, RDF.type, self.QG.GrammarRule))
# Add properties
self.grammar_graph.add((
rule_uri,
self.QG.hasAction,
Literal(rule.action)
))
self.grammar_graph.add((
rule_uri,
self.QG.hasConfidence,
Literal(rule.confidence)
))
self.grammar_graph.add((
rule_uri,
self.QG.hasSource,
Literal(rule.source)
))
# Add conditions as JSON literal
self.grammar_graph.add((
rule_uri,
self.QG.hasCondition,
Literal(json.dumps(rule.conditions))
))
self.grammar_rules = all_rules
return self.grammar_graph
def export_grammar_rules(self, filename: str = 'quranic_grammar_rules.ttl'):
"""Export grammar ontology to Turtle format"""
ontology = self.build_grammar_ontology()
with open(filename, 'w', encoding='utf-8') as f:
f.write(ontology.serialize(format='turtle'))
print(f"✓ Grammar rules exported to {filename}")
print(f"✓ Total rules encoded: {len(self.grammar_rules)}")
return filename
def export_rules_json(self, filename: str = 'quranic_grammar_rules.json'):
"""Also export as JSON for easy Python integration"""
rules_data = {
'metadata': {
'source': 'Quranic Arabic Corpus + Classical Grammar Texts',
'total_rules': len(self.grammar_rules),
'categories': list(set(r.rule_type for r in self.grammar_rules))
},
'rules': [
{
'id': r.rule_id,
'type': r.rule_type,
'conditions': r.conditions,
'action': r.action,
'confidence': r.confidence,
'source': r.source
}
for r in self.grammar_rules
]
}
with open(filename, 'w', encoding='utf-8') as f:
json.dump(rules_data, f, indent=2, ensure_ascii=False)
print(f"✓ Grammar rules exported to {filename}")
return filename
# EXECUTE THE GRAMMAR ENCODING
print("=" * 60)
print("QURANIC GRAMMAR RULE ENCODING SYSTEM")
print("=" * 60)
encoder = QuranGrammarEncoder()
# Build and export grammar ontology
grammar_file = encoder.export_grammar_rules()
json_file = encoder.export_rules_json()
print("\n✓ Grammar foundation complete")
print(f"✓ Rules based on classical Arabic grammar (Sibawayh, Al-Ajrumiyyah, I'rab tradition)")
print(f"✓ Ready to integrate with graph extraction pipeline")