File size: 20,726 Bytes
39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 7450f9c 39ca024 | 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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | """
Noggimigo AI - Adaptive Lesson Generator
Improved version with better architecture, type safety, error handling,
and extensibility for special education accommodations.
"""
import json
import os
import random
import sys
from dataclasses import dataclass, field, asdict
from enum import Enum, auto
from typing import Callable, Optional, Union
from pathlib import Path
class DisabilityProfile(Enum):
"""Supported learning disability profiles with their accommodation strategies."""
GENERAL = "general"
ADHD = "adhd"
AUTISM = "autism"
DYSLEXIA = "dyslexia"
@dataclass(frozen=True)
class AccommodationSettings:
"""Immutable configuration for learning accommodations."""
chunk_size: int = 3
reward_frequency: str = "normal"
timer_enabled: bool = False
visual_cues: bool = False
predictable_layout: bool = False
literal_language: bool = False
explicit_hints: bool = False
font_spacing: str = "normal"
reading_grade_level: str = "standard"
audio_speech_hook: bool = False
standard: bool = False
def to_dict(self) -> dict:
"""Return only non-default values for cleaner output."""
return {k: v for k, v in asdict(self).items() if v is not False and v != "normal" and v != "standard"}
@dataclass
class LessonNode:
"""Represents a single interactive step in a lesson."""
node_id: str
prompt: str
target_value: str
options: list[str]
transitions: dict[str, str]
micro_scaffold: str = ""
numeric_val: Optional[float] = None
common_errors: dict[str, tuple[str, str]] = field(default_factory=dict)
next_node: str = "complete"
@dataclass
class LessonPlan:
"""Complete lesson structure with metadata."""
domain: str
grade_level: int
disability_profile: str
accommodations: dict
initial_node: str
nodes: dict[str, LessonNode]
is_fallback: bool = False
class DisabilityScaffoldAdapter:
"""
Adapts lesson content for specific learning profiles using
evidence-based instructional design principles.
"""
# Profile-specific prompt prefixes and formatting
_PROMPT_STYLES = {
DisabilityProfile.ADHD: ("โก Quick Step", " [Focus Goal: 1 step at a time!]"),
DisabilityProfile.AUTISM: ("๐ Step-by-Step Guide", " (Direct Task)"),
DisabilityProfile.DYSLEXIA: ("๐", ""),
DisabilityProfile.GENERAL: ("", ""),
}
# Maximum words for dyslexia profile to reduce cognitive load
_DYSLEXIA_MAX_WORDS = 12
@classmethod
def adapt_prompt(cls, text: str, profile: Union[str, DisabilityProfile]) -> str:
"""Adapt text presentation based on learner profile."""
profile = cls._normalize_profile(profile)
prefix, suffix = cls._PROMPT_STYLES.get(profile, ("", ""))
if profile == DisabilityProfile.DYSLEXIA:
return cls._simplify_for_dyslexia(text, prefix)
if profile == DisabilityProfile.GENERAL:
return text
return f"{prefix}: {text}{suffix}" if prefix else text
@classmethod
def generate_accommodations(cls, profile: Union[str, DisabilityProfile]) -> dict:
"""Generate appropriate accommodation settings for profile."""
profile = cls._normalize_profile(profile)
configs = {
DisabilityProfile.ADHD: AccommodationSettings(
chunk_size=1,
reward_frequency="high",
visual_cues=True,
),
DisabilityProfile.AUTISM: AccommodationSettings(
predictable_layout=True,
literal_language=True,
explicit_hints=True,
),
DisabilityProfile.DYSLEXIA: AccommodationSettings(
font_spacing="wide",
reading_grade_level="simplified",
audio_speech_hook=True,
),
DisabilityProfile.GENERAL: AccommodationSettings(standard=True),
}
return configs.get(profile, AccommodationSettings()).to_dict()
@classmethod
def _normalize_profile(cls, profile: Union[str, DisabilityProfile]) -> DisabilityProfile:
"""Convert string to enum safely."""
if isinstance(profile, DisabilityProfile):
return profile
try:
return DisabilityProfile(profile.lower().strip())
except ValueError:
return DisabilityProfile.GENERAL
@classmethod
def _simplify_for_dyslexia(cls, text: str, prefix: str) -> str:
"""Reduce text complexity: shorter sentences, clear structure."""
words = text.split()
if len(words) <= cls._DYSLEXIA_MAX_WORDS:
simplified = text
else:
# Break at sentence boundary if possible
sentences = text.replace('! ', '|').replace('. ', '|').replace('? ', '|').split('|')
simplified = sentences[0] if sentences[0] else " ".join(words[:cls._DYSLEXIA_MAX_WORDS]) + "..."
return f"{prefix} {simplified}".strip()
class MathProblemGenerator:
"""Generates mathematically sound problems with validated constraints."""
@staticmethod
def generate_fraction_problem() -> dict:
"""Generate a valid fraction problem with non-trivial solution."""
# Ensure we have meaningful numbers (avoid trivial 0 or same values)
denominators = [4, 6, 8, 10, 12]
total = random.choice(denominators)
part = random.randint(1, total - 1)
# Ensure remainder is not 0 and problem is non-trivial
while part == 0 or part == total:
part = random.randint(1, total - 1)
rem = total - part
return {
"total": total,
"part": part,
"remainder": rem,
"fraction": f"{rem}/{total}",
"decimal": round(rem / total, 3),
}
@staticmethod
def generate_linear_equation() -> dict:
"""Generate solvable linear equation ax + b = c with integer solution."""
a = random.randint(2, 5)
x = random.randint(2, 8) # Ensure x > 1 for non-trivial problems
b = random.randint(2, 8)
c = (a * x) + b
return {
"a": a,
"b": b,
"c": c,
"x": x,
"first_step": f"subtract {b}",
}
class LessonGenerator:
"""
Procedurally generates validated adaptive lessons with proper
error handling and mathematical correctness checks.
"""
def __init__(self):
self._problem_generators = {
"math_fractions": MathProblemGenerator.generate_fraction_problem,
"math_algebra": MathProblemGenerator.generate_linear_equation,
}
self._builders = {
"math_fractions": self._build_fraction_node,
"math_algebra": self._build_algebra_node,
}
def generate_procedural_lesson(
self,
domain: str = "math_fractions",
grade_level: int = 4,
disability_profile: Union[str, DisabilityProfile] = DisabilityProfile.GENERAL,
num_nodes: int = 2,
) -> LessonPlan:
"""
Generate complete adaptive lesson with validation and fallbacks.
"""
try:
if domain not in self._builders:
raise ValueError(f"Unknown domain: {domain}. Available: {list(self._builders.keys())}")
if num_nodes < 1:
raise ValueError("num_nodes must be at least 1")
nodes = {}
for i in range(1, num_nodes + 1):
node_id = f"step_{i}"
next_node_id = "complete" if i == num_nodes else f"step_{i+1}"
problem_data = self._problem_generators[domain]()
node = self._builders[domain](
node_id=node_id,
next_node_id=next_node_id,
problem_data=problem_data,
disability_profile=disability_profile,
)
nodes[node_id] = node
return LessonPlan(
domain=domain,
grade_level=grade_level,
disability_profile=str(disability_profile),
accommodations=DisabilityScaffoldAdapter.generate_accommodations(disability_profile),
initial_node="step_1",
nodes=nodes,
)
except Exception as e:
# Log error for debugging but return functional fallback
print(f"[WARNING] Lesson generation failed: {e}. Returning fallback.", file=sys.stderr)
return self._create_fallback(domain, disability_profile)
def _build_fraction_node(
self,
node_id: str,
next_node_id: str,
problem_data: dict,
disability_profile: Union[str, DisabilityProfile],
) -> LessonNode:
"""Construct a fraction lesson node with distractor analysis."""
total = problem_data["total"]
part = problem_data["part"]
rem = problem_data["remainder"]
raw_text = (
f"Imagine a circle split into {total} equal parts. "
f"You shade in {part} parts. What fraction is left unshaded?"
)
target = problem_data["fraction"]
options = [target, f"{part}/{total}", f"1/{total}"]
random.shuffle(options)
# Build error feedback based on common misconceptions
common_errors = {}
if f"{part}/{total}" in options:
common_errors[f"{part}/{total}"] = (
"INVERSION_ERROR",
f"{part}/{total} is the shaded portion! We want unshaded parts ({rem}).",
)
if f"1/{total}" in options:
common_errors[f"1/{total}"] = (
"UNIT_ERROR",
f"Look at total unshaded parts ({rem}), not just 1 piece.",
)
return LessonNode(
node_id=node_id,
prompt=DisabilityScaffoldAdapter.adapt_prompt(raw_text, disability_profile),
target_value=target,
options=options,
transitions={opt: next_node_id if opt == target else node_id for opt in options},
micro_scaffold=f"Subtract shaded parts ({part}) from total parts ({total}). Remaining = {rem}.",
numeric_val=problem_data["decimal"],
common_errors=common_errors,
next_node=next_node_id,
)
def _build_algebra_node(
self,
node_id: str,
next_node_id: str,
problem_data: dict,
disability_profile: Union[str, DisabilityProfile],
) -> LessonNode:
"""Construct an algebra lesson node with step-by-step scaffolding."""
a, b, c = problem_data["a"], problem_data["b"], problem_data["c"]
raw_text = f"Solve for x: {a}x + {b} = {c}. What operation isolates {a}x?"
target = problem_data["first_step"]
options = [target, f"add {b}", f"divide {a}"]
random.shuffle(options)
common_errors = {
f"add {b}": (
"INVERSE_ERROR",
f"Adding {b} increases the constant! Use subtraction to undo +{b}.",
),
f"divide {a}": (
"ORDER_ERROR",
f"Subtract the constant {b} first, then divide by {a}.",
),
}
return LessonNode(
node_id=node_id,
prompt=DisabilityScaffoldAdapter.adapt_prompt(raw_text, disability_profile),
target_value=target,
options=options,
transitions={opt: next_node_id if opt == target else node_id for opt in options},
micro_scaffold=f"To undo +{b}, perform inverse operation: subtract {b} from both sides.",
common_errors=common_errors,
next_node=next_node_id,
)
def _create_fallback(
self,
domain: str,
disability_profile: Union[str, DisabilityProfile],
) -> LessonPlan:
"""Reliable fallback when procedural generation fails."""
prompt = DisabilityScaffoldAdapter.adapt_prompt(
"Imagine a circle split into 4 equal parts. You shade in 3 parts. What fraction of the circle is left unshaded?",
disability_profile,
)
node = LessonNode(
node_id="step_1",
prompt=prompt,
target_value="1/4",
options=["1/4", "3/4", "1/2"],
transitions={"1/4": "complete", "3/4": "step_1", "1/2": "step_1"},
micro_scaffold="Subtract shaded parts (3) from total (4).",
numeric_val=0.25,
common_errors={
"3/4": ("INVERSION_ERROR", "3/4 is shaded! Unshaded is 1/4."),
"1/2": ("HALVING_ERROR", "Count the unshaded parts carefully."),
},
next_node="complete",
)
return LessonPlan(
domain=domain,
grade_level=4,
disability_profile=str(disability_profile),
accommodations=DisabilityScaffoldAdapter.generate_accommodations(disability_profile),
initial_node="step_1",
nodes={"step_1": node},
is_fallback=True,
)
class NogginLessonBuilder:
"""
Interactive CLI for building custom lesson content with
improved UX and data validation.
"""
def __init__(self):
self.lesson_data: dict[str, dict] = {}
self.generator = LessonGenerator()
def create_node(self) -> None:
"""Interactive node creation with validation."""
print("\n" + "โ" * 40)
print(" CREATE NEW LESSON NODE")
print("โ" * 40)
node_id = self._get_input("Node ID (e.g., 'intro', 'stuck_half'): ", required=True)
if node_id in self.lesson_data:
overwrite = input(f"Node '{node_id}' exists. Overwrite? (y/n): ").lower().strip() == 'y'
if not overwrite:
print("Cancelled.")
return
text = self._get_input("Noggimigo's Question or Explanation:\n> ", required=True)
options, transitions = self._collect_options(node_id)
self.lesson_data[node_id] = {
"text": text,
"options": options,
"transitions": transitions,
}
print(f"\nโ
Node '{node_id}' saved! ({len(options)} option(s))")
def _collect_options(self, node_id: str) -> tuple[list[str], dict[str, str]]:
"""Collect choice options with validation."""
options: list[str] = []
transitions: dict[str, str] = {}
print("\nAdd choice buttons (type 'done' when finished):")
while True:
choice = self._get_input("Option text (e.g., '1/4'): ", allow_done=bool(options))
if choice == 'done':
break
# Prevent duplicate options
if choice in options:
print(" โ ๏ธ This option already exists. Use a unique value.")
continue
target = self._get_input(f"Target node for '{choice}': ", required=True)
options.append(choice)
transitions[choice] = target
print(f" โ
'{choice}' โ {target}")
return options, transitions
def export_to_json(self) -> None:
"""Export with safe file handling and path validation."""
if not self.lesson_data:
print("โ ๏ธ No nodes to export. Create some content first!")
return
filename = self._get_input("Filename (e.g., 'fractions_lesson.json'): ", required=True)
if not filename.endswith('.json'):
filename += '.json'
# Security: restrict to current directory
filepath = Path(filename).name
full_path = Path.cwd() / filepath
try:
with open(full_path, 'w', encoding='utf-8') as f:
json.dump(self.lesson_data, f, indent=2, ensure_ascii=False)
print(f"\n๐พ Saved: {full_path}")
print(f" {len(self.lesson_data)} node(s) exported")
except (OSError, PermissionError) as e:
print(f"โ Save failed: {e}")
def generate_adaptive_lesson(self) -> None:
"""Generate procedurally with profile selection."""
print("\nAvailable profiles: adhd, autism, dyslexia, general")
profile = self._get_input("Enter profile: ", default="general")
try:
lesson = self.generator.generate_procedural_lesson(disability_profile=profile)
print("\n" + "โ" * 40)
print("GENERATED ADAPTIVE LESSON")
print("โ" * 40)
# Pretty print with structure
output = {
"domain": lesson.domain,
"grade_level": lesson.grade_level,
"disability_profile": lesson.disability_profile,
"accommodations": lesson.accommodations,
"is_fallback": lesson.is_fallback,
"initial_node": lesson.initial_node,
"nodes": {
nid: {
"prompt": node.prompt,
"target_value": node.target_value,
"options": node.options,
"micro_scaffold": node.micro_scaffold,
"next_node": node.next_node,
}
for nid, node in lesson.nodes.items()
},
}
print(json.dumps(output, indent=2, ensure_ascii=False))
# Offer to save
save = input("\nSave this lesson to file? (y/n): ").lower().strip() == 'y'
if save:
self.lesson_data = output["nodes"]
self.export_to_json()
except Exception as e:
print(f"โ Generation error: {e}")
def _get_input(
self,
prompt: str,
required: bool = False,
default: Optional[str] = None,
allow_done: bool = False,
) -> str:
"""Robust input helper with validation."""
while True:
value = input(prompt).strip()
if value.lower() == 'done' and allow_done:
return 'done'
if not value and required:
print(" โ ๏ธ This field is required.")
continue
if not value and default is not None:
return default
return value
def main_menu(self) -> None:
"""Main interaction loop."""
commands = {
"1": ("Add Node", self.create_node),
"2": ("Export to JSON", self.export_to_json),
"3": ("Generate Adaptive Lesson", self.generate_adaptive_lesson),
"4": ("View Current Nodes", self._view_nodes),
"5": ("Clear All Nodes", self._clear_nodes),
"0": ("Exit", self._exit),
}
while True:
print("\n" + "=" * 40)
print(" NOGGIMIGO LESSON BUILDER")
print("=" * 40)
for key, (label, _) in commands.items():
print(f"{key}. {label}")
print("โ" * 40)
choice = input("Select: ").strip()
if choice in commands:
_, action = commands[choice]
if action() is False: # Exit returns False
break
else:
print("โ ๏ธ Invalid choice.")
def _view_nodes(self) -> None:
"""Display current lesson structure."""
if not self.lesson_data:
print("No nodes created yet.")
return
print(f"\nCurrent nodes ({len(self.lesson_data)}):")
for nid, data in self.lesson_data.items():
opts = len(data.get("options", []))
print(f" โข {nid}: {opts} option(s)")
def _clear_nodes(self) -> None:
"""Clear with confirmation."""
if not self.lesson_data:
print("Nothing to clear.")
return
confirm = input("Delete all nodes? (type 'yes'): ").strip().lower()
if confirm == 'yes':
self.lesson_data.clear()
print("All nodes cleared.")
def _exit(self) -> bool:
"""Clean exit with save reminder."""
if self.lesson_data:
unsaved = input("You have unsaved nodes. Save before exit? (y/n): ").lower().strip()
if unsaved == 'y':
self.export_to_json()
print("Goodbye!")
return False
def main():
"""Entry point with error handling."""
try:
builder = NogginLessonBuilder()
builder.main_menu()
except KeyboardInterrupt:
print("\n\nInterrupted by user.")
sys.exit(0)
except Exception as e:
print(f"\nUnexpected error: {e}")
sys.exit(1)
if __name__ == "__main__":
main() |