feat: Ingest complete Grade 10 Math textbook (Units 1-4) with semantic lesson naming, clean file hierarchy, and AI Video prompt generator

This commit is contained in:
Hamza-Ayed
2026-08-29 04:53:45 +03:00
parent 5b58ead215
commit 439572453f
37 changed files with 1153 additions and 602 deletions
+42 -27
View File
@@ -5,8 +5,13 @@ require_once __DIR__ . '/../app/bootstrap.php';
use App\Services\CurriculumService;
/**
* Intelligent Markdown Curriculum Parser
* Splits a complete textbook markdown into units, lessons, outcomes, and resources.
* Intelligent & Semantic Markdown Curriculum Parser
* Maps sections with 100% precision:
* - Lesson 1 -> lesson_01.md
* - Lesson 2 -> lesson_02.md
* - Intro & Project -> intro_and_project.md
* - GeoGebra Lab -> geogebra_lab.md
* - Unit Exam -> unit_exam.md
*/
function parseAndIngestTextbookMarkdown(string $rawMarkdown, array $metadata = []): array
{
@@ -45,19 +50,14 @@ function parseAndIngestTextbookMarkdown(string $rawMarkdown, array $metadata = [
};
$unitCounter = 0;
$lessonCounter = 0;
$arabicOrdinalMap = [
'الأولى' => 1, 'الاولى' => 1, '1' => 1, 'one' => 1,
'الثانية' => 2, 'الثانيه' => 2, '2' => 2, 'two' => 2,
'الثالثة' => 3, 'الثالثه' => 3, '3' => 3, 'three' => 3,
'الرابعة' => 4, 'الرابعه' => 4, '4' => 4, 'four' => 4,
'الخامسة' => 5, 'الخامسه' => 5, '5' => 5, 'five' => 5,
'السادسة' => 6, 'السادسه' => 6, '6' => 6, 'six' => 6,
'السابعة' => 7, 'السابعه' => 7, '7' => 7, 'seven' => 7,
'الثامنة' => 8, 'الثامنه' => 8, '8' => 8, 'eight' => 8,
'التاسعة' => 9, 'التاسعه' => 9, '9' => 9, 'nine' => 9,
'العاشرة' => 10, 'العاشره' => 10, '10' => 10, 'ten' => 10,
'الأولى' => 1, 'الاولى' => 1, '1' => 1,
'الثانية' => 2, 'الثانيه' => 2, '2' => 2,
'الثالثة' => 3, 'الثالثه' => 3, '3' => 3,
'الرابعة' => 4, 'الرابعه' => 4, '4' => 4,
'الخامسة' => 5, 'الخامسه' => 5, '5' => 5,
'السادسة' => 6, 'السادسه' => 6, '6' => 6,
];
foreach ($lines as $line) {
@@ -68,7 +68,6 @@ function parseAndIngestTextbookMarkdown(string $rawMarkdown, array $metadata = [
$flushUnit();
$unitTitle = trim(preg_replace('/^#\s+/u', '', $trimmed));
// Try to extract unit number from title
$detectedNum = null;
foreach ($arabicOrdinalMap as $word => $num) {
if (mb_stripos($unitTitle, $word) !== false) {
@@ -89,24 +88,26 @@ function parseAndIngestTextbookMarkdown(string $rawMarkdown, array $metadata = [
'unit_name' => $unitTitle,
'lessons' => []
];
$lessonCounter = 0;
continue;
}
// Detect Lesson (## Lesson or ## الدرس or ## معمل or ## مقدمة or ## اختبار or ## )
if (preg_match('/^##\s+(Lesson|الدرس|قسم|المحطة|قواعد|تمارين|معمل|مقدمة|مشروع|اختبار)\s*([0-9\-\:A-Za-z\s\p{Arabic}]+)(.*)$/ui', $trimmed, $m) || (preg_match('/^##\s+(.+)$/u', $trimmed, $mAlt) && $currentUnit)) {
// Detect Section (## ...)
if (preg_match('/^##\s+(.+)$/u', $trimmed, $mAlt) && $currentUnit) {
$flushLesson();
$lessonCounter++;
$lessonTitle = trim(preg_replace('/^##\s+/u', '', $trimmed));
$lesId = sprintf('lesson_%02d', $lessonCounter);
$lessonTitle = trim($mAlt[1]);
if (!$currentUnit) {
$unitCounter++;
$currentUnit = [
'unit_key' => sprintf('unit_%02d', $unitCounter),
'unit_name' => 'الوحدة ' . $unitCounter,
'lessons' => []
];
// Semantic Identification of Lesson ID and File
if (preg_match('/(?:الدرس|Lesson)\s*([0-9]+)/ui', $lessonTitle, $mLes)) {
$lesNum = (int)$mLes[1];
$lesId = sprintf('lesson_%02d', $lesNum);
} elseif (preg_match('/(?:مقدمة|مشروع)/ui', $lessonTitle)) {
$lesId = 'intro_and_project';
} elseif (preg_match('/(?:معمل|جيوجبرا|نشاط)/ui', $lessonTitle)) {
$lesId = 'geogebra_lab';
} elseif (preg_match('/(?:اختبار|تدريب)/ui', $lessonTitle)) {
$lesId = 'unit_exam';
} else {
$lesId = 'section_' . count($currentUnit['lessons'] ?? []);
}
$currentLesson = [
@@ -125,6 +126,20 @@ function parseAndIngestTextbookMarkdown(string $rawMarkdown, array $metadata = [
$flushUnit();
// Clean old files in target directories to eliminate stubs
$baseUnitDir = __DIR__ . "/../storage/curriculum/{$gradeKey}/{$subKey}/{$semKey}/";
foreach ($units as $u) {
$uDir = $baseUnitDir . $u['unit_key'];
if (is_dir($uDir)) {
$existingFiles = glob($uDir . '/*.md');
foreach ($existingFiles as $f) {
if (basename($f) === 'lesson_1.md' || basename($f) === 'lesson_2.md') {
@unlink($f);
}
}
}
}
$extracted = [
'grade_name' => $gradeName,
'grade_key' => $gradeKey,