MathFormulaProcessor.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace App\Services;
  3. class MathFormulaProcessor
  4. {
  5. /**
  6. * 处理数学公式,确保有正确的 LaTeX 标记
  7. *
  8. * 优化策略:最小化干预,只修复真正需要修复的问题
  9. * 1. 检查是否已有正确的 LaTeX 标记,如有则直接返回
  10. * 2. 只在检测到明显错误时才进行修复
  11. * 3. 优先保护正确的数学表达式不被破坏
  12. */
  13. public static function processFormulas(string $content): string
  14. {
  15. if (empty($content)) {
  16. return $content;
  17. }
  18. // 0. 基础清理:解码 HTML 实体
  19. $decoded = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
  20. while ($decoded !== $content) {
  21. $content = $decoded;
  22. $decoded = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
  23. }
  24. $content = trim($content);
  25. // 1. 如果已有正确的定界符,只清理内部 HTML,不做其他修改
  26. if (self::hasDelimiters($content)) {
  27. return self::cleanInsideDelimiters($content);
  28. }
  29. // 2. 检测内容类型:纯数学、混合内容还是纯文本
  30. $contentType = self::detectContentType($content);
  31. // 3. 根据内容类型采取不同的处理策略
  32. switch ($contentType) {
  33. case 'pure_math':
  34. // 纯数学表达式,如 "4x^2 - 25y^2" 或 "f(x) = x^2 - 4x + 5"
  35. return self::wrapPureMath($content);
  36. case 'mixed_content':
  37. // 混合内容,如 "已知函数 f(x) = x^2 - 4x + 5,求最小值"
  38. return self::smartWrapMixedContent($content);
  39. case 'plain_text':
  40. default:
  41. // 纯文本,不需要处理
  42. return $content;
  43. }
  44. }
  45. /**
  46. * 检测内容类型
  47. * 优化:加入中文检测,避免包裹包含中文的混合内容
  48. */
  49. private static function detectContentType(string $content): string
  50. {
  51. // 检查是否有定界符
  52. if (self::hasDelimiters($content)) {
  53. return 'delimited';
  54. }
  55. // 检查是否包含数学特征
  56. $hasMathFeatures = self::containsMathFeatures($content);
  57. // 如果不包含数学特征,返回纯文本
  58. if (!$hasMathFeatures) {
  59. return 'plain_text';
  60. }
  61. // 检查是否包含中文字符
  62. if (preg_match('/[\x{4e00}-\x{9fa5}]/u', $content)) {
  63. // 包含中文 + 数学特征 = 混合内容,需要智能提取数学部分
  64. return 'mixed_content';
  65. }
  66. // 检查是否包含长文本(超过一定长度的字母组合)
  67. $hasLongText = preg_match('/[a-zA-Z]{8,}/', $content);
  68. if ($hasLongText) {
  69. // 包含长文本,可能是混合内容,但不包裹(保守策略)
  70. return 'plain_text';
  71. }
  72. // 检查是纯数学还是混合内容
  73. // 混合内容:同时包含数学特征和普通英文单词
  74. $hasPlainText = preg_match('/\b[a-zA-Z]{3,7}\b/', $content) &&
  75. !preg_match('/^[a-zA-Z0-9\+\-\*\/\=\s\.\^\(\)\_\{\}]+$/', $content);
  76. if ($hasPlainText) {
  77. return 'mixed_content';
  78. }
  79. return 'pure_math';
  80. }
  81. /**
  82. * 包裹纯数学表达式
  83. * 优化:只添加定界符,不修改内容本身
  84. */
  85. private static function wrapPureMath(string $content): string
  86. {
  87. // 已经是纯数学格式,直接用 $ 包裹
  88. return '$' . $content . '$';
  89. }
  90. /**
  91. * 清理定界符内部的 HTML 标签
  92. */
  93. private static function cleanInsideDelimiters(string $content): string
  94. {
  95. // 修复:使用更精确的正则表达式,避免模式冲突
  96. // 先处理 $$...$$ 模式,然后处理单个 $...$ 模式(但确保不被$$包含)
  97. // 1. 处理 $$...$$ 显示公式
  98. $content = preg_replace_callback('/\$\$([\s\S]*?)\$\$/', function ($matches) {
  99. $cleanContent = strip_tags($matches[1]);
  100. $cleanContent = html_entity_decode($cleanContent, ENT_QUOTES, 'UTF-8');
  101. $cleanContent = trim($cleanContent);
  102. return '$$' . $cleanContent . '$$';
  103. }, $content);
  104. // 2. 处理 \(...\) 行内公式
  105. $content = preg_replace_callback('/\\\\\(([\s\S]*?)\\\\\)/', function ($matches) {
  106. $cleanContent = strip_tags($matches[1]);
  107. $cleanContent = html_entity_decode($cleanContent, ENT_QUOTES, 'UTF-8');
  108. $cleanContent = trim($cleanContent);
  109. return '\\(' . $cleanContent . '\\)';
  110. }, $content);
  111. // 3. 处理 \[...\] 显示公式
  112. $content = preg_replace_callback('/\\\\\[([\s\S]*?)\\\\\]/', function ($matches) {
  113. $cleanContent = strip_tags($matches[1]);
  114. $cleanContent = html_entity_decode($cleanContent, ENT_QUOTES, 'UTF-8');
  115. $cleanContent = trim($cleanContent);
  116. return '\\[' . $cleanContent . '\\]';
  117. }, $content);
  118. // 4. 最后处理 $...$ 行内公式(避免与$$冲突)
  119. $content = preg_replace_callback('/(?<!\$)\$([^$\n]+?)\$(?!\$)/', function ($matches) {
  120. $cleanContent = strip_tags($matches[1]);
  121. $cleanContent = html_entity_decode($cleanContent, ENT_QUOTES, 'UTF-8');
  122. $cleanContent = trim($cleanContent);
  123. return '$' . $cleanContent . '$';
  124. }, $content);
  125. return $content;
  126. }
  127. /**
  128. * 智能识别并包裹富文本中的数学公式
  129. * 支持:函数定义、导数表达式、LaTeX命令、数学运算
  130. */
  131. private static function smartWrapMixedContent(string $content): string
  132. {
  133. // 匹配策略:只匹配明确的数学表达式,避免误判
  134. $tagPattern = '<[^>]+>';
  135. $existingDelimiterPattern = '(?:\$\$[\s\S]*?\$\$|\$[\s\S]*?\$|\\\\\([\s\S]*?\\\\\)|\\\\\[[\s\S]*?\\\\\])';
  136. // 数学公式模式(按优先级排列)
  137. $patterns = [
  138. // 1. 函数定义: f(x) = 2x^3 - 3x^2 + 4x - 5
  139. "[a-zA-Z]'?\\([a-zA-Z0-9,\\s]+\\)\\s*=\\s*[a-zA-Z0-9\\+\\-\\*\\/\\^\\s\\.\\(\\)\\_\\{\\}]+",
  140. // 2. 导数/函数调用: f'(1), g(5), sin(x)
  141. "[a-zA-Z]+'?\\([a-zA-Z0-9\\+\\-\\*\\/\\^\\s\\.]+\\)",
  142. // 3. LaTeX 命令: \frac{1}{2}
  143. "\\\\[a-zA-Z]+\\{[^}]*\\}(?:\\{[^}]*\\})?",
  144. // 4. 数学表达式: x^2 + y^2, 2x - 3
  145. "[a-zA-Z0-9]+[\\^_][a-zA-Z0-9\\{\\}]+(?:\\s*[\\+\\-\\*\\/]\\s*[a-zA-Z0-9\\^_\\{\\}\\.]+)*",
  146. ];
  147. $mathPattern = '(?:' . implode('|', $patterns) . ')';
  148. $pattern = "/($tagPattern)|($existingDelimiterPattern)|($mathPattern)/u";
  149. return preg_replace_callback($pattern, function ($matches) {
  150. // HTML 标签,原样返回
  151. if (!empty($matches[1])) {
  152. return $matches[1];
  153. }
  154. // 已有的定界符,原样返回
  155. if (!empty($matches[2])) {
  156. return $matches[2];
  157. }
  158. // 数学公式,添加 $ 包裹
  159. if (!empty($matches[3])) {
  160. $math = trim($matches[3]);
  161. // 再次检查是否已经包裹
  162. if (str_contains($math, '$')) {
  163. return $math;
  164. }
  165. return '$' . $math . '$';
  166. }
  167. return $matches[0];
  168. }, $content);
  169. }
  170. /**
  171. * 检查是否已有定界符
  172. */
  173. private static function hasDelimiters(string $content): bool
  174. {
  175. $content = trim($content);
  176. // 检查 $$...$$
  177. if (str_starts_with($content, '$$') && str_ends_with($content, '$$')) {
  178. return true;
  179. }
  180. // 检查 $...$
  181. if (str_starts_with($content, '$') && str_ends_with($content, '$')) {
  182. return true;
  183. }
  184. // 检查 \[...\]
  185. if (str_starts_with($content, '\\[') && str_ends_with($content, '\\]')) {
  186. return true;
  187. }
  188. // 检查 \(...\)
  189. if (str_starts_with($content, '\\(') && str_ends_with($content, '\\)')) {
  190. return true;
  191. }
  192. return false;
  193. }
  194. /**
  195. * 检测数学特征
  196. * 优化:更精确的检测,减少误判
  197. */
  198. private static function containsMathFeatures(string $content): bool
  199. {
  200. // 1. 检查是否有 LaTeX 命令
  201. if (preg_match('/\\\\[a-zA-Z]+\{?/', $content)) {
  202. return true;
  203. }
  204. // 2. 检查函数定义或等式(如 f(x) =, g(x) =)
  205. // 必须是:字母+括号+等号+数学内容
  206. if (preg_match('/[a-zA-Z]\([a-zA-Z0-9,\s]+\)\s*=\s*[a-zA-Z0-9\+\-\*\/\^\.\(\)\s\\\\_\{]+/', $content)) {
  207. return true;
  208. }
  209. // 3. 检查纯数学表达式(只包含数字、变量、运算符、括号)
  210. // 严格的数学表达式:必须包含字母和运算符,且没有中文字符
  211. if (preg_match('/^[a-zA-Z0-9\+\-\*\/\=\s\.\^\(\)\_\{\}]+$/', $content) &&
  212. preg_match('/[a-zA-Z]/', $content) &&
  213. preg_match('/[\+\-\*\/\=\^]/', $content)) {
  214. return true;
  215. }
  216. // 4. 检查包含变量的数学表达式(带约束)
  217. // 必须有明确的运算符连接,且周围是数学内容
  218. if (preg_match('/[a-zA-Z0-9\.\^\_\{\}]\s*[\+\-\*\/]\s*[a-zA-Z0-9\.\^\_\{\}\(\)]/', $content)) {
  219. return true;
  220. }
  221. // 5. 检查分数形式(如 \frac{}{})
  222. if (preg_match('/\\\\frac\{/', $content)) {
  223. return true;
  224. }
  225. // 6. 检查上标或下标(仅当与数字/字母组合时)
  226. if (preg_match('/[a-zA-Z0-9]\s*[\^_]\s*[a-zA-Z0-9]/', $content)) {
  227. return true;
  228. }
  229. return false;
  230. }
  231. /**
  232. * 批量处理
  233. */
  234. public static function processArray(array $data, array $fieldsToProcess): array
  235. {
  236. foreach ($data as $key => &$value) {
  237. if (in_array($key, $fieldsToProcess) && is_string($value)) {
  238. $value = self::processFormulas($value);
  239. } elseif (is_array($value)) {
  240. $value = self::processArray($value, $fieldsToProcess);
  241. }
  242. }
  243. return $data;
  244. }
  245. /**
  246. * 处理题目数据
  247. */
  248. public static function processQuestionData(array $question): array
  249. {
  250. $fieldsToProcess = [
  251. 'stem', 'content', 'question_text', 'answer',
  252. 'correct_answer', 'student_answer', 'explanation',
  253. 'solution', 'question_content'
  254. ];
  255. return self::processArray($question, $fieldsToProcess);
  256. }
  257. /**
  258. * 修复被污染的数学公式(包含重复的转义字符)
  259. */
  260. private static function fixCorruptedFormulas(string $content): string
  261. {
  262. // 简化的修复策略,只处理明确的问题
  263. // 1. 将超过2个连续的$符号减少为2个
  264. $content = preg_replace('/\${3,}/', '$$', $content);
  265. // 2. 修复$$B . - \frac{1}{2}$$ 这种格式,在选项前加空格
  266. $content = preg_replace('/\$\$([A-Z])\s*\.\s*/', '$$ $1. ', $content);
  267. // 3. 修复不完整的frac命令:\frac{1}{2} -> \frac{1}{2}
  268. $content = preg_replace('/\\\\frac\\\\({[^}]+)([^}]*)\\\\/', '\\\\frac$1}{$2}', $content);
  269. // 4. 移除孤立的反斜杠(在非LaTeX命令前的)
  270. $content = preg_replace('/\\\\(?![a-zA-Z{])/', '', $content);
  271. return $content;
  272. }
  273. }