test_latex_cleaner.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. use App\Services\LatexCleanerService;
  3. require __DIR__ . '/../vendor/autoload.php';
  4. $app = require_once __DIR__ . '/../bootstrap/app.php';
  5. $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
  6. $cleaner = new LatexCleanerService();
  7. // 测试用例
  8. $testCases = [
  9. 'f \left( x \right) = x ^ { a } ,' => 'f \left(x\right) = x^{a},',
  10. 'B . - \frac { 1 } { 2 }' => 'B . - \frac{1}{2}',
  11. 'f \left(x \right) = x^{a$} ,$$$' => 'f \left(x\right) = x^{a},',
  12. '\frac { 1 } { 2 } + \sqrt { 4 }' => '\frac{1}{2} + \sqrt{4}',
  13. 'x ^ { 2 } + y _ { 1 }' => 'x^{2} + y_{1}',
  14. ];
  15. echo "LaTeX Cleaner Service 测试\n";
  16. echo str_repeat('=', 80) . "\n\n";
  17. foreach ($testCases as $input => $expected) {
  18. $output = $cleaner->clean($input);
  19. $validation = $cleaner->validate($output);
  20. echo "输入: {$input}\n";
  21. echo "输出: {$output}\n";
  22. echo "期望: {$expected}\n";
  23. echo "匹配: " . ($output === $expected ? '✓ 是' : '✗ 否') . "\n";
  24. echo "有效: " . ($validation['valid'] ? '✓ 是' : '✗ 否');
  25. if (!$validation['valid']) {
  26. echo " (错误: " . implode(', ', $validation['errors']) . ")";
  27. }
  28. echo "\n";
  29. echo str_repeat('-', 80) . "\n\n";
  30. }
  31. echo "\n批量清理测试:\n";
  32. echo str_repeat('=', 80) . "\n\n";
  33. $questions = [
  34. ['content' => 'f \left( x \right) = x ^ { a }', 'student_answer' => 'A $'],
  35. ['content' => '\frac { 1 } { 2 }', 'student_answer' => 'B . '],
  36. ];
  37. $cleaned = $cleaner->cleanArray($questions);
  38. foreach ($cleaned as $i => $q) {
  39. echo "题目 " . ($i + 1) . ":\n";
  40. echo " 内容: {$q['content']}\n";
  41. echo " 答案: {$q['student_answer']}\n";
  42. echo "\n";
  43. }