Varshith dharmaj commited on
Upload utils/explanation_generator.py with huggingface_hub
Browse files- utils/explanation_generator.py +137 -0
utils/explanation_generator.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Explanation Generation
|
| 3 |
+
Generates natural language explanations for errors found
|
| 4 |
+
Provides educational context and learning suggestions
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from typing import Dict, Any
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def generate_explanation(error: Dict[str, Any]) -> str:
|
| 11 |
+
"""
|
| 12 |
+
Generate human-readable explanation for an error.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
error: Error dictionary with type, found, correct, step_number, etc.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
Natural language explanation string (2-3 sentences)
|
| 19 |
+
"""
|
| 20 |
+
error_type = error.get("type", "unknown")
|
| 21 |
+
found = error.get("found", "")
|
| 22 |
+
correct = error.get("correct", "")
|
| 23 |
+
operation = error.get("operation", "")
|
| 24 |
+
step_number = error.get("step_number", 0)
|
| 25 |
+
|
| 26 |
+
# Extract numbers from found and correct
|
| 27 |
+
import re
|
| 28 |
+
found_nums = re.findall(r'\d+\.?\d*', found)
|
| 29 |
+
correct_nums = re.findall(r'\d+\.?\d*', correct)
|
| 30 |
+
|
| 31 |
+
# Generate explanation based on error type
|
| 32 |
+
if error_type == "calculation_error":
|
| 33 |
+
explanation = _explain_arithmetic_error(found, correct, operation, found_nums, correct_nums)
|
| 34 |
+
elif error_type == "logical_error":
|
| 35 |
+
explanation = _explain_logical_error(error, step_number)
|
| 36 |
+
elif error_type == "operation_mismatch":
|
| 37 |
+
explanation = _explain_operation_mismatch(error, found, correct)
|
| 38 |
+
elif error_type == "semantic_error":
|
| 39 |
+
explanation = _explain_semantic_error(error, found, correct)
|
| 40 |
+
else:
|
| 41 |
+
explanation = _explain_generic_error(found, correct)
|
| 42 |
+
|
| 43 |
+
# Add learning context
|
| 44 |
+
learning_tip = _get_learning_tip(error_type, operation)
|
| 45 |
+
if learning_tip:
|
| 46 |
+
explanation += f" {learning_tip}"
|
| 47 |
+
|
| 48 |
+
return explanation
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def _explain_arithmetic_error(found: str, correct: str, operation: str, found_nums: list, correct_nums: list) -> str:
|
| 52 |
+
"""Explain arithmetic calculation errors."""
|
| 53 |
+
if not found_nums or not correct_nums:
|
| 54 |
+
return f"You wrote '{found}', but the correct answer is '{correct}'."
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
found_result = float(found_nums[-1]) if found_nums else 0
|
| 58 |
+
correct_result = float(correct_nums[-1]) if correct_nums else 0
|
| 59 |
+
|
| 60 |
+
if len(found_nums) >= 2:
|
| 61 |
+
operand1 = float(found_nums[0])
|
| 62 |
+
operand2 = float(found_nums[1])
|
| 63 |
+
|
| 64 |
+
op_names = {
|
| 65 |
+
'+': 'add',
|
| 66 |
+
'-': 'subtract',
|
| 67 |
+
'*': 'multiply',
|
| 68 |
+
'/': 'divide'
|
| 69 |
+
}
|
| 70 |
+
op_name = op_names.get(operation, 'calculate')
|
| 71 |
+
|
| 72 |
+
explanation = f"You wrote {found_result}, but {operand1} {operation} {operand2} actually equals {correct_result}. "
|
| 73 |
+
|
| 74 |
+
if operation == '+':
|
| 75 |
+
explanation += f"When you add {operand2} to {operand1}, you get {correct_result}, not {found_result}."
|
| 76 |
+
elif operation == '-':
|
| 77 |
+
explanation += f"When you subtract {operand2} from {operand1}, you count down: {operand1}, {correct_result}. So {operand1} - {operand2} = {correct_result}, not {found_result}."
|
| 78 |
+
elif operation == '*':
|
| 79 |
+
explanation += f"When you multiply {operand1} by {operand2}, you get {correct_result}, not {found_result}."
|
| 80 |
+
elif operation == '/':
|
| 81 |
+
explanation += f"When you divide {operand1} by {operand2}, you get {correct_result}, not {found_result}."
|
| 82 |
+
else:
|
| 83 |
+
explanation += f"The correct calculation gives {correct_result}, not {found_result}."
|
| 84 |
+
|
| 85 |
+
return explanation
|
| 86 |
+
except:
|
| 87 |
+
pass
|
| 88 |
+
|
| 89 |
+
return f"You wrote '{found}', but the correct answer is '{correct}'."
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def _explain_logical_error(error: Dict[str, Any], step_number: int) -> str:
|
| 93 |
+
"""Explain logical consistency errors."""
|
| 94 |
+
description = error.get("description", "There is a logical inconsistency in your reasoning.")
|
| 95 |
+
|
| 96 |
+
explanation = f"In step {step_number}, {description.lower()} "
|
| 97 |
+
explanation += "This contradicts your earlier statements or creates circular reasoning."
|
| 98 |
+
|
| 99 |
+
return explanation
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def _explain_operation_mismatch(error: Dict[str, Any], found: str, correct: str) -> str:
|
| 103 |
+
"""Explain operation mismatch errors."""
|
| 104 |
+
description = error.get("description", "The operation doesn't match what you described.")
|
| 105 |
+
|
| 106 |
+
explanation = f"{description} "
|
| 107 |
+
explanation += "Make sure the mathematical operation matches what you're trying to do in the problem."
|
| 108 |
+
|
| 109 |
+
return explanation
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def _explain_semantic_error(error: Dict[str, Any], found: str, correct: str) -> str:
|
| 113 |
+
"""Explain semantic inconsistency errors."""
|
| 114 |
+
description = error.get("description", "The meaning doesn't match the mathematical expression.")
|
| 115 |
+
|
| 116 |
+
explanation = f"{description} "
|
| 117 |
+
explanation += "Review the step to ensure it follows logically from the previous steps."
|
| 118 |
+
|
| 119 |
+
return explanation
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def _explain_generic_error(found: str, correct: str) -> str:
|
| 123 |
+
"""Generic error explanation."""
|
| 124 |
+
return f"You wrote '{found}', but the correct answer should be '{correct}'. Please review your calculation."
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def _get_learning_tip(error_type: str, operation: str) -> str:
|
| 128 |
+
"""Get learning tip based on error type."""
|
| 129 |
+
tips = {
|
| 130 |
+
"calculation_error": "Try double-checking your arithmetic by working through the problem step by step.",
|
| 131 |
+
"logical_error": "Make sure each step follows logically from the previous one and doesn't contradict earlier statements.",
|
| 132 |
+
"operation_mismatch": "Before writing the math, think about which operation matches what you're trying to do.",
|
| 133 |
+
"semantic_error": "Review how this step connects to the overall problem and previous steps."
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
return tips.get(error_type, "Practice similar problems to improve your understanding.")
|
| 137 |
+
|