joshuarauh commited on
Commit
1c1a2f5
·
verified ·
1 Parent(s): e6857f6

Update app.py

Browse files

For Wolfram Verification, added specialized handling for different types of calculus problems and improved answer comparison

Files changed (1) hide show
  1. app.py +48 -13
app.py CHANGED
@@ -60,11 +60,32 @@ def parse_questions(content):
60
  def verify_solution(problem, answer):
61
  """Verify a mathematical solution using Wolfram Alpha"""
62
  try:
63
- # Clean up the problem for Wolfram Alpha
64
- query = problem.replace('$$', '').replace('$', '')
65
- # Remove any text instructions, keep only the mathematical expression
66
- query = re.sub(r'(?i)find|calculate|solve|evaluate|determine', '', query)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  query = query.strip()
 
68
 
69
  result = wolfram_client.query(query)
70
 
@@ -77,15 +98,28 @@ def verify_solution(problem, answer):
77
 
78
  # Look for numerical results in multiple pods
79
  for pod in result.pods:
80
- if pod.title in ['Result', 'Solution', 'Numerical result', 'Decimal approximation']:
81
  wolfram_answer = pod.text
82
- # Extract numerical value
83
- wolfram_nums = re.findall(r'[-+]?(?:\d*\.)?\d+', wolfram_answer)
84
- if wolfram_nums:
85
- wolfram_value = float(wolfram_nums[0])
86
- user_value = float(answer)
87
- # Allow for small numerical differences
88
- is_verified = abs(wolfram_value - user_value) < 0.01
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  return {
90
  'verified': is_verified,
91
  'wolfram_solution': wolfram_answer,
@@ -95,7 +129,7 @@ def verify_solution(problem, answer):
95
  return {
96
  'verified': False,
97
  'wolfram_solution': None,
98
- 'error': "No numerical solution found in Wolfram Alpha response"
99
  }
100
  except Exception as e:
101
  return {
@@ -104,6 +138,7 @@ def verify_solution(problem, answer):
104
  'error': f"Error during verification: {str(e)}"
105
  }
106
 
 
107
  def generate_test(subject):
108
  """Generate and verify a math test"""
109
  try:
 
60
  def verify_solution(problem, answer):
61
  """Verify a mathematical solution using Wolfram Alpha"""
62
  try:
63
+ # First, determine the type of problem
64
+ if "lim" in problem.lower():
65
+ # Format limit problem
66
+ # Extract the expression and point of evaluation
67
+ expr = re.search(r'lim.*?(\d+\/[x-]+\d+)', problem)
68
+ if expr:
69
+ query = f"limit of {expr.group(1)} as x approaches 2"
70
+ elif "integral" in problem.lower():
71
+ # Format integral problem
72
+ # Extract the integrand and limits
73
+ expr = re.search(r'\int_0\^1.*?([\dx+]+).*?dx', problem)
74
+ if expr:
75
+ query = f"integrate {expr.group(1)} from 0 to 1"
76
+ elif "derivative" in problem.lower() or "f(x)" in problem:
77
+ # Format derivative problem
78
+ # Extract the function to differentiate
79
+ expr = re.search(r'f\(x\)\s*=\s*([^\s]+)', problem)
80
+ if expr:
81
+ query = f"d/dx ({expr.group(1)})"
82
+ else:
83
+ # Default cleanup for other types of problems
84
+ query = problem.replace('$$', '').replace('$', '')
85
+ query = re.sub(r'(?i)find|calculate|solve|evaluate|determine', '', query)
86
+
87
  query = query.strip()
88
+ print(f"Sending query to Wolfram Alpha: {query}") # Debug print
89
 
90
  result = wolfram_client.query(query)
91
 
 
98
 
99
  # Look for numerical results in multiple pods
100
  for pod in result.pods:
101
+ if pod.title in ['Result', 'Solution', 'Numerical result', 'Decimal approximation', 'Limit', 'Definite integral', 'Derivative']:
102
  wolfram_answer = pod.text
103
+ print(f"Wolfram Alpha pod {pod.title}: {wolfram_answer}") # Debug print
104
+
105
+ # For numerical answers
106
+ if answer.isdigit() or answer.replace('.', '').isdigit():
107
+ wolfram_nums = re.findall(r'[-+]?(?:\d*\.)?\d+', wolfram_answer)
108
+ if wolfram_nums:
109
+ wolfram_value = float(wolfram_nums[0])
110
+ user_value = float(answer)
111
+ is_verified = abs(wolfram_value - user_value) < 0.01
112
+ return {
113
+ 'verified': is_verified,
114
+ 'wolfram_solution': wolfram_answer,
115
+ 'error': None
116
+ }
117
+ # For symbolic answers (like derivatives)
118
+ else:
119
+ # Clean up both answers for comparison
120
+ clean_wolfram = re.sub(r'\s+', '', wolfram_answer.lower())
121
+ clean_answer = re.sub(r'\s+', '', answer.lower())
122
+ is_verified = clean_wolfram == clean_answer
123
  return {
124
  'verified': is_verified,
125
  'wolfram_solution': wolfram_answer,
 
129
  return {
130
  'verified': False,
131
  'wolfram_solution': None,
132
+ 'error': "No suitable solution found in Wolfram Alpha response"
133
  }
134
  except Exception as e:
135
  return {
 
138
  'error': f"Error during verification: {str(e)}"
139
  }
140
 
141
+
142
  def generate_test(subject):
143
  """Generate and verify a math test"""
144
  try: