import sys sys.path.append('/app') from sqlmodel import Session, select from database import engine import models import re def fix_text(text): if not text: return text # The DB currently has string literals with backslash caret 3 d backslash dollar. # Like '\\^3d\\$' and '\\^3d^2\\$' text = text.replace('\\^3d^2\\$', '$sp^3d^2$') text = text.replace('\\^3d\\$', '$sp^3d$') text = text.replace('\\^3\\$', '$sp^3$') text = text.replace('\\^2\\$', '$sp^2$') text = text.replace('^3d^2$', '$sp^3d^2$') text = text.replace('^3d$', '$sp^3d$') text = text.replace('^3$', '$sp^3$') text = text.replace('^2$', '$sp^2$') # Original targets in case we run on fresh seed text = re.sub(r'\bsp3d2\b', r'$sp^3d^2$', text) text = re.sub(r'\bsp3d\b', r'$sp^3d$', text) text = re.sub(r'\bsp3\b', r'$sp^3$', text) text = re.sub(r'\bsp2\b', r'$sp^2$', text) mols = ['PCl5', 'SF6', 'H2O', 'NH3', 'CO2', 'CH4', 'C2H4', 'C2H2', 'BF3', 'BeCl2', 'O2', 'N2', 'F2'] for mol in mols: formatted = '$' + re.sub(r'(\d+)', r'_\1', mol) + '$' text = re.sub(r'\b' + mol + r'\b', formatted, text) text = text.replace('z1 = 1 + i', '$z_1 = 1 + i$') text = text.replace('z2 = 2 − i', '$z_2 = 2 - i$') text = text.replace('z1\nz2', '$\\frac{z_1}{z_2}$') text = text.replace('z−1\nz+1', '$\\frac{z-1}{z+1}$') text = text.replace('z2 = ¯z', '$z^2 = \\bar{z}$') text = text.replace('x2 − 3x + 5 = 0', '$x^2 - 3x + 5 = 0$') text = text.replace('x2 + 2x + 5 = 0', '$x^2 + 2x + 5 = 0$') text = text.replace('1+i\n1−i', '$\\frac{1+i}{1-i}$') text = text.replace('z + 1\nz', '$\\frac{z+1}{z}$') text = text.replace('z − 1\nz i', '$\\frac{z-1}{z i}$') text = text.replace('30◦', '30°') text = text.replace('⃗v = 3', '$\\vec{v} = 3$') text = text.replace('σ bonds', '$\\sigma$ bonds') text = text.replace('π bonds', '$\\pi$ bonds') return text with Session(engine) as session: questions = session.exec(select(models.Question)).all() count = 0 for q in questions: old_text = q.question_text new_text = fix_text(old_text) if old_text != new_text: q.question_text = new_text session.add(q) count += 1 print(f'Updated Q {q.id}') session.commit() print(f'Successfully updated {count} questions.')