Commit ·
1519f71
1
Parent(s): c29d4de
feat(nlp): implement case-checking rules for kana and inna sisters using camel_tools
Browse files
src/nlp/grammar/grammar_rules.py
CHANGED
|
@@ -301,6 +301,93 @@ class ArabicGrammarGuard:
|
|
| 301 |
text = re.sub(r'\b([وف]?ل)([أ-ي]{4,})(ون|ان)\b', _lam_prep_replace, text)
|
| 302 |
return text
|
| 303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
def fix_subject_verb_agreement(self, text):
|
| 305 |
"""
|
| 306 |
Fix G1: When a CONFIRMED plural noun PRECEDES a singular verb (SVO order),
|
|
@@ -683,6 +770,7 @@ class ArabicGrammarGuard:
|
|
| 683 |
('fix_noun_adjective_agreement_advanced', self.fix_noun_adjective_agreement_advanced),
|
| 684 |
('fix_prepositions_advanced', self.fix_prepositions_advanced),
|
| 685 |
('fix_subject_verb_agreement', self.fix_subject_verb_agreement),
|
|
|
|
| 686 |
('fix_conditional_sentences', self.fix_conditional_sentences),
|
| 687 |
('fix_tanween_fathah', self.fix_tanween_fathah),
|
| 688 |
('fix_initial_hamza', self.fix_initial_hamza),
|
|
|
|
| 301 |
text = re.sub(r'\b([وف]?ل)([أ-ي]{4,})(ون|ان)\b', _lam_prep_replace, text)
|
| 302 |
return text
|
| 303 |
|
| 304 |
+
def fix_kana_and_inna(self, text):
|
| 305 |
+
"""
|
| 306 |
+
Fix cases (Nominative/Accusative) of nouns after Inna and Kana sisters.
|
| 307 |
+
"""
|
| 308 |
+
tokens = simple_word_tokenize(text)
|
| 309 |
+
disambig_tokens = self.mle.disambiguate(tokens)
|
| 310 |
+
corrected_tokens = list(tokens)
|
| 311 |
+
|
| 312 |
+
INNA_SISTERS = {'إن', 'أن', 'كأن', 'لكن', 'ليت', 'لعل', 'ان'}
|
| 313 |
+
KANA_SISTERS = {'كان', 'أصبح', 'اصبح', 'أضحى', 'اضحى', 'ظل', 'أمسى', 'امسى', 'بات', 'صار', 'ليس'}
|
| 314 |
+
|
| 315 |
+
def get_corrected_case(word, target_case, num):
|
| 316 |
+
if target_case == 'a': # Mansoub
|
| 317 |
+
if word == 'أبو': return 'أبا'
|
| 318 |
+
if word == 'أخو': return 'أخا'
|
| 319 |
+
if word == 'ذو': return 'ذا'
|
| 320 |
+
if word == 'فو': return 'فا'
|
| 321 |
+
if word == 'حمو': return 'حما'
|
| 322 |
+
if word.endswith('ون'): return word[:-2] + 'ين'
|
| 323 |
+
if word.endswith('ان'): return word[:-2] + 'ين'
|
| 324 |
+
elif target_case == 'n': # Marfoo'
|
| 325 |
+
if word in ('أبا', 'أبي'): return 'أبو'
|
| 326 |
+
if word in ('أخا', 'أخي'): return 'أخو'
|
| 327 |
+
if word in ('ذا', 'ذي'): return 'ذو'
|
| 328 |
+
if word in ('فا', 'في'): return 'فو'
|
| 329 |
+
if word in ('حما', 'حمي'): return 'حمو'
|
| 330 |
+
if word.endswith('ين'):
|
| 331 |
+
if num == 'p': return word[:-2] + 'ون'
|
| 332 |
+
elif num == 'd': return word[:-2] + 'ان'
|
| 333 |
+
return word
|
| 334 |
+
|
| 335 |
+
state = None # 'inna' or 'kana'
|
| 336 |
+
noun_count = 0
|
| 337 |
+
subject_num = 's'
|
| 338 |
+
|
| 339 |
+
for i, t in enumerate(disambig_tokens):
|
| 340 |
+
word = corrected_tokens[i]
|
| 341 |
+
|
| 342 |
+
if word in INNA_SISTERS:
|
| 343 |
+
state = 'inna'
|
| 344 |
+
noun_count = 0
|
| 345 |
+
continue
|
| 346 |
+
elif word in KANA_SISTERS:
|
| 347 |
+
state = 'kana'
|
| 348 |
+
noun_count = 0
|
| 349 |
+
continue
|
| 350 |
+
|
| 351 |
+
if state and t.analyses:
|
| 352 |
+
analysis = t.analyses[0].analysis
|
| 353 |
+
pos = analysis.get('pos')
|
| 354 |
+
|
| 355 |
+
# Check for nouns/adjectives
|
| 356 |
+
if pos in ('noun', 'adj', 'noun_prop'):
|
| 357 |
+
num = analysis.get('num', 's')
|
| 358 |
+
noun_count += 1
|
| 359 |
+
|
| 360 |
+
new_word = word
|
| 361 |
+
if state == 'inna':
|
| 362 |
+
if noun_count == 1:
|
| 363 |
+
subject_num = num
|
| 364 |
+
new_word = get_corrected_case(word, 'a', num) # اسم إن منصوب
|
| 365 |
+
elif noun_count == 2:
|
| 366 |
+
new_word = get_corrected_case(word, 'n', subject_num) # خبر إن مرفوع
|
| 367 |
+
state = None
|
| 368 |
+
elif state == 'kana':
|
| 369 |
+
if noun_count == 1:
|
| 370 |
+
subject_num = num
|
| 371 |
+
new_word = get_corrected_case(word, 'n', num) # اسم كان مرفوع
|
| 372 |
+
elif noun_count == 2:
|
| 373 |
+
new_word = get_corrected_case(word, 'a', subject_num) # خبر كان منصوب
|
| 374 |
+
state = None
|
| 375 |
+
|
| 376 |
+
if new_word != word:
|
| 377 |
+
pattern = r'(?<![أ-يa-zA-Z])' + re.escape(word) + r'(?![أ-يa-zA-Z])'
|
| 378 |
+
text = re.sub(pattern, new_word, text, count=1)
|
| 379 |
+
corrected_tokens[i] = new_word
|
| 380 |
+
|
| 381 |
+
elif pos == 'verb':
|
| 382 |
+
# Verb encountered, predicate might be a verbal sentence, reset to avoid false positives
|
| 383 |
+
state = None
|
| 384 |
+
|
| 385 |
+
# Reset state on punctuation
|
| 386 |
+
if word in {'.', '،', ':', '؟', '!', '؛'}:
|
| 387 |
+
state = None
|
| 388 |
+
|
| 389 |
+
return text
|
| 390 |
+
|
| 391 |
def fix_subject_verb_agreement(self, text):
|
| 392 |
"""
|
| 393 |
Fix G1: When a CONFIRMED plural noun PRECEDES a singular verb (SVO order),
|
|
|
|
| 770 |
('fix_noun_adjective_agreement_advanced', self.fix_noun_adjective_agreement_advanced),
|
| 771 |
('fix_prepositions_advanced', self.fix_prepositions_advanced),
|
| 772 |
('fix_subject_verb_agreement', self.fix_subject_verb_agreement),
|
| 773 |
+
('fix_kana_and_inna', self.fix_kana_and_inna),
|
| 774 |
('fix_conditional_sentences', self.fix_conditional_sentences),
|
| 775 |
('fix_tanween_fathah', self.fix_tanween_fathah),
|
| 776 |
('fix_initial_hamza', self.fix_initial_hamza),
|
tests/test_grammar_kana_inna.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from nlp.grammar.grammar_rules import ArabicGrammarGuard
|
| 3 |
+
|
| 4 |
+
@pytest.fixture
|
| 5 |
+
def grammar_guard():
|
| 6 |
+
return ArabicGrammarGuard()
|
| 7 |
+
|
| 8 |
+
def test_inna_sisters(grammar_guard):
|
| 9 |
+
# Inna: Subject is Mansoub (Accusative), Predicate is Marfoo' (Nominative)
|
| 10 |
+
|
| 11 |
+
# 1. Subject is Plural
|
| 12 |
+
text = "إن المعلمون مجتهدون"
|
| 13 |
+
corrected = grammar_guard.fix_kana_and_inna(text)
|
| 14 |
+
assert "إن المعلمين مجتهدون" == corrected
|
| 15 |
+
|
| 16 |
+
text = "لعل العاملون قادمون"
|
| 17 |
+
corrected = grammar_guard.fix_kana_and_inna(text)
|
| 18 |
+
assert "لعل العاملين قادمون" == corrected
|
| 19 |
+
|
| 20 |
+
# 2. Subject is Dual
|
| 21 |
+
text = "كأن الفتاتان قمران"
|
| 22 |
+
corrected = grammar_guard.fix_kana_and_inna(text)
|
| 23 |
+
assert "كأن الفتاتين قمران" == corrected
|
| 24 |
+
|
| 25 |
+
# 3. Predicate is wrong
|
| 26 |
+
text = "إن المهندسين نائمين"
|
| 27 |
+
corrected = grammar_guard.fix_kana_and_inna(text)
|
| 28 |
+
assert "إن المهندسين نائمون" == corrected
|
| 29 |
+
|
| 30 |
+
# 4. Five Nouns
|
| 31 |
+
text = "إن أبو بكر رجل صالح"
|
| 32 |
+
corrected = grammar_guard.fix_kana_and_inna(text)
|
| 33 |
+
assert "إن أبا بكر رجل صالح" == corrected
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_kana_sisters(grammar_guard):
|
| 37 |
+
# Kana: Subject is Marfoo' (Nominative), Predicate is Mansoub (Accusative)
|
| 38 |
+
|
| 39 |
+
# 1. Subject is Plural
|
| 40 |
+
text = "كان المهندسين بارعين"
|
| 41 |
+
corrected = grammar_guard.fix_kana_and_inna(text)
|
| 42 |
+
assert "كان المهندسون بارعين" == corrected
|
| 43 |
+
|
| 44 |
+
# 2. Subject is Dual
|
| 45 |
+
text = "أصبح الرجلين غنيين"
|
| 46 |
+
corrected = grammar_guard.fix_kana_and_inna(text)
|
| 47 |
+
assert "أصبح الرجلان غنيين" == corrected
|
| 48 |
+
|
| 49 |
+
# 3. Predicate is wrong
|
| 50 |
+
text = "بات الحارسان نائمون"
|
| 51 |
+
corrected = grammar_guard.fix_kana_and_inna(text)
|
| 52 |
+
assert "بات الحارسان نائمين" == corrected
|
| 53 |
+
|
| 54 |
+
# 4. Five Nouns
|
| 55 |
+
text = "أصبح أبا محمد مريضا"
|
| 56 |
+
corrected = grammar_guard.fix_kana_and_inna(text)
|
| 57 |
+
assert "أصبح أبو محمد مريضا" == corrected
|
| 58 |
+
|
| 59 |
+
def test_no_false_positives(grammar_guard):
|
| 60 |
+
# Should not mess with correct sentences
|
| 61 |
+
text = "كان المعلمون بارعين"
|
| 62 |
+
assert grammar_guard.fix_kana_and_inna(text) == text
|
| 63 |
+
|
| 64 |
+
text = "إن المعلمين قادمون"
|
| 65 |
+
assert grammar_guard.fix_kana_and_inna(text) == text
|