make('Illuminate\Contracts\Console\Kernel')->bootstrap(); $cleaner = new LatexCleanerService(); // 测试用例 $testCases = [ 'f \left( x \right) = x ^ { a } ,' => 'f \left(x\right) = x^{a},', 'B . - \frac { 1 } { 2 }' => 'B . - \frac{1}{2}', 'f \left(x \right) = x^{a$} ,$$$' => 'f \left(x\right) = x^{a},', '\frac { 1 } { 2 } + \sqrt { 4 }' => '\frac{1}{2} + \sqrt{4}', 'x ^ { 2 } + y _ { 1 }' => 'x^{2} + y_{1}', ]; echo "LaTeX Cleaner Service 测试\n"; echo str_repeat('=', 80) . "\n\n"; foreach ($testCases as $input => $expected) { $output = $cleaner->clean($input); $validation = $cleaner->validate($output); echo "输入: {$input}\n"; echo "输出: {$output}\n"; echo "期望: {$expected}\n"; echo "匹配: " . ($output === $expected ? '✓ 是' : '✗ 否') . "\n"; echo "有效: " . ($validation['valid'] ? '✓ 是' : '✗ 否'); if (!$validation['valid']) { echo " (错误: " . implode(', ', $validation['errors']) . ")"; } echo "\n"; echo str_repeat('-', 80) . "\n\n"; } echo "\n批量清理测试:\n"; echo str_repeat('=', 80) . "\n\n"; $questions = [ ['content' => 'f \left( x \right) = x ^ { a }', 'student_answer' => 'A $'], ['content' => '\frac { 1 } { 2 }', 'student_answer' => 'B . '], ]; $cleaned = $cleaner->cleanArray($questions); foreach ($cleaned as $i => $q) { echo "题目 " . ($i + 1) . ":\n"; echo " 内容: {$q['content']}\n"; echo " 答案: {$q['student_answer']}\n"; echo "\n"; }