Commit ·
e7c1175
1
Parent(s): be32617
Fix plural adjective agreement regression for broken plurals
Browse files
src/nlp/grammar/grammar_rules.py
CHANGED
|
@@ -688,8 +688,8 @@ class ArabicGrammarGuard:
|
|
| 688 |
else:
|
| 689 |
corrected_tokens[i+1] = base_adj + ('ان' if is_nom else 'ين')
|
| 690 |
|
| 691 |
-
# Plural Human Adjective Agreement
|
| 692 |
-
elif w1_num == 'p' and w1_pos in ['noun', 'unknown']:
|
| 693 |
base_adj = None
|
| 694 |
for suffix in ['ان', 'ين', 'تان', 'تين', 'ة', 'ون', 'ات', 'ين', '']:
|
| 695 |
stem = w2[:-len(suffix)] if suffix else w2
|
|
|
|
| 688 |
else:
|
| 689 |
corrected_tokens[i+1] = base_adj + ('ان' if is_nom else 'ين')
|
| 690 |
|
| 691 |
+
# Plural Human Adjective Agreement (Sound Plurals Only)
|
| 692 |
+
elif w1_num == 'p' and w1_pos in ['noun', 'unknown'] and (w1.endswith('ون') or w1.endswith('ين') or w1.endswith('ات')):
|
| 693 |
base_adj = None
|
| 694 |
for suffix in ['ان', 'ين', 'تان', 'تين', 'ة', 'ون', 'ات', 'ين', '']:
|
| 695 |
stem = w2[:-len(suffix)] if suffix else w2
|
src/nlp/test_chatgpt_cases.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
|
| 5 |
+
|
| 6 |
+
from src.nlp.grammar.grammar_rules import ArabicGrammarGuard
|
| 7 |
+
from src.nlp.spelling.araspell_rules import AraSpellPostProcessor
|
| 8 |
+
|
| 9 |
+
def process_text(text, grammar, spell):
|
| 10 |
+
# Pipeline simulation
|
| 11 |
+
text = grammar.smart_asmaa_khamsa_fix(text)
|
| 12 |
+
text = grammar.fix_subject_verb_agreement(text)
|
| 13 |
+
text = grammar.fix_gender_agreement(text)
|
| 14 |
+
text = grammar.fix_prepositions_advanced(text)
|
| 15 |
+
text = grammar.fix_initial_hamza(text)
|
| 16 |
+
text = grammar.fix_conditional_sentences(text)
|
| 17 |
+
text = grammar.fix_verbs_nasb_and_jazm(text)
|
| 18 |
+
text = grammar.fix_noun_adjective_agreement_advanced(text)
|
| 19 |
+
text = grammar.fix_number_and_gender_agreement(text)
|
| 20 |
+
text = grammar.fix_kana_and_inna(text)
|
| 21 |
+
text = grammar.regex_rules_fallback(text)
|
| 22 |
+
text = grammar.fix_tanween_fathah(text)
|
| 23 |
+
|
| 24 |
+
text = spell.fix_common_hamza(text)
|
| 25 |
+
text = spell.join_fragments(text)
|
| 26 |
+
text = spell.fix_hamza_conservative(text)
|
| 27 |
+
text = spell.remove_word_repetition_with_wa(text)
|
| 28 |
+
text = spell.remove_hallucinations(text)
|
| 29 |
+
return text.strip()
|
| 30 |
+
|
| 31 |
+
def run_tests():
|
| 32 |
+
grammar = ArabicGrammarGuard()
|
| 33 |
+
spell = AraSpellPostProcessor()
|
| 34 |
+
|
| 35 |
+
test_cases = [
|
| 36 |
+
# 1. Asmaa Khamsa
|
| 37 |
+
("رأيت أخاك في السوق.", "رأيت أخاك في السوق."),
|
| 38 |
+
("جاء أبوك اليوم.", "جاء أبوك اليوم."),
|
| 39 |
+
# 2. Non-human plural
|
| 40 |
+
("السيارات تسير بسرعة.", "السيارات تسير بسرعة."),
|
| 41 |
+
("المدارس كبيرة.", "المدارس كبيرة."),
|
| 42 |
+
# 3. Masc ends with Ta Marbuta
|
| 43 |
+
("أسامة بطل شجاع.", "أسامة بطل شجاع."),
|
| 44 |
+
("حضر القضاة الكبار.", "حضر القضاة الكبار."),
|
| 45 |
+
# 4. Words ending in ون not plural
|
| 46 |
+
("قرأت عن قانون جديد.", "قرأت عن قانون جديد."),
|
| 47 |
+
("زرعت زيتونًا في الأرض.", "زرعت زيتونًا في الأرض."),
|
| 48 |
+
# 5. Inna after Qawl
|
| 49 |
+
("قال إنه قادم.", "قال إنه قادم."),
|
| 50 |
+
("يقول إنه يعرف الحقيقة.", "يقول إنه يعرف الحقيقة."),
|
| 51 |
+
# 6. Ma Naafiya vs Conditional
|
| 52 |
+
("ما يذهب محمد إلى المدرسة.", "ما يذهب محمد إلى المدرسة."),
|
| 53 |
+
("ما فعلت ذلك.", "ما فعلت ذلك."),
|
| 54 |
+
# 7. Lam Al-Ta'leel
|
| 55 |
+
("ذهبت لأتعلم.", "ذهبت لأتعلم."),
|
| 56 |
+
("جاء ليساعد صديقه.", "جاء ليساعد صديقه."),
|
| 57 |
+
# 8. Broken Plural + Adjective
|
| 58 |
+
("جاء الرجال الطويلون.", "جاء الرجال الطويلون."),
|
| 59 |
+
("رأيت الرجال الأقوياء.", "رأيت الرجال الأقوياء."),
|
| 60 |
+
# 9. Defective verbs
|
| 61 |
+
("هم رموا الكرة.", "هم رموا الكرة."),
|
| 62 |
+
("دعوا الله.", "دعوا الله."),
|
| 63 |
+
# 10. Haa Milkiya vs Ta Marbuta
|
| 64 |
+
("هذا كتابه.", "هذا كتابه."),
|
| 65 |
+
("سمعت صوته.", "سمعت صوته."),
|
| 66 |
+
# 11. Merged words
|
| 67 |
+
("أحمد درس اليوم.", "أحمد درس اليوم."),
|
| 68 |
+
("يوم مشمس جميل.", "يوم مشمس جميل."),
|
| 69 |
+
# 12. Imma / Amma
|
| 70 |
+
("إما هذا أو ذاك.", "إما هذا أو ذاك."),
|
| 71 |
+
("أما بعد، فهذا أمر مهم.", "أما بعد، فهذا أمر مهم."),
|
| 72 |
+
# 13. Trailing Hamza
|
| 73 |
+
("قرأ الطالب الكتاب.", "قرأ الطالب الكتاب."),
|
| 74 |
+
("هذا مبدأ مهم.", "هذا مبدأ مهم."),
|
| 75 |
+
# 14. Fake Prepositions
|
| 76 |
+
("هذا كتاب منطقي.", "هذا كتاب منطقي."),
|
| 77 |
+
("أكلت فيتامينات كثيرة.", "أكلت فيتامينات كثيرة."),
|
| 78 |
+
# 15. Conditional In
|
| 79 |
+
("سأذهب إن جاء أحمد.", "سأذهب إن جاء أحمد."),
|
| 80 |
+
("إن تدرس تنجح.", "إن تدرس تنجح.")
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
print("Running ChatGPT Regression Test Suite...\n")
|
| 84 |
+
passed = 0
|
| 85 |
+
for idx, (input_text, expected) in enumerate(test_cases, 1):
|
| 86 |
+
final_text = process_text(input_text, grammar, spell)
|
| 87 |
+
|
| 88 |
+
if final_text == expected:
|
| 89 |
+
status = "✅ PASS"
|
| 90 |
+
passed += 1
|
| 91 |
+
else:
|
| 92 |
+
status = "❌ FAIL"
|
| 93 |
+
|
| 94 |
+
print(f"Test {idx}: {status}")
|
| 95 |
+
print(f"Input : {input_text}")
|
| 96 |
+
print(f"Output : {final_text}")
|
| 97 |
+
if final_text != expected:
|
| 98 |
+
print(f"Expected: {expected}")
|
| 99 |
+
print("-" * 40)
|
| 100 |
+
|
| 101 |
+
print(f"\nTotal Passed: {passed}/{len(test_cases)}")
|
| 102 |
+
|
| 103 |
+
if __name__ == '__main__':
|
| 104 |
+
run_tests()
|