MathFormulaProcessor.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. // 0.5 将自定义 <image> 标签转换为标准 <img> 标签
  26. $content = self::convertImageTags($content);
  27. // 1. 【关键修复】处理公式内的双反斜杠 -> 单反斜杠
  28. // 数据库存储时 \sqrt 变成 \\sqrt,需要还原
  29. $content = self::normalizeBackslashesInDelimiters($content);
  30. // 2. 如果内容中包含定界符,清理内部 HTML
  31. if (self::containsDelimiters($content)) {
  32. $content = self::cleanInsideDelimiters($content);
  33. }
  34. // 3. 检测内容类型:纯数学、混合内容还是纯文本
  35. $contentType = self::detectContentType($content);
  36. // 4. 根据内容类型采取不同的处理策略
  37. switch ($contentType) {
  38. case 'pure_math':
  39. // 纯数学表达式,如 "4x^2 - 25y^2" 或 "f(x) = x^2 - 4x + 5"
  40. return self::wrapPureMath($content);
  41. case 'mixed_content':
  42. // 混合内容,如 "已知函数 f(x) = x^2 - 4x + 5,求最小值"
  43. return self::smartWrapMixedContent($content);
  44. case 'delimited':
  45. // 已包含定界符的内容($...$, $$...$$, \(...\), \[...\])
  46. // 直接返回,cleanInsideDelimiters() 已经清理了内部内容
  47. // 渲染工作由客户端 KaTeX 或服务端 KatexRenderer 完成
  48. return $content;
  49. case 'plain_text':
  50. default:
  51. // 纯文本,不需要处理
  52. return $content;
  53. }
  54. }
  55. /**
  56. * 将自定义 <image> 标签转换为标准 <img> 标签
  57. * 例如:<image src="https://example.com/1.png"/> => <img src="https://example.com/1.png" />
  58. */
  59. private static function convertImageTags(string $content): string
  60. {
  61. // 匹配 <image src="..." /> 或 <image src="..."></image> 格式
  62. return preg_replace(
  63. '/<image\s+src=["\']([^"\']+)["\'](?:\s*\/>|><\/image>)/i',
  64. '<img src="$1" />',
  65. $content
  66. );
  67. }
  68. /**
  69. * 【新增】将公式定界符内被JSON双重转义的LaTeX命令还原
  70. * 例如:\\sqrt -> \sqrt, \\frac -> \frac
  71. * 但保留矩阵换行符 \\ (后面不跟字母的情况)
  72. */
  73. private static function normalizeBackslashesInDelimiters(string $content): string
  74. {
  75. // 只替换 \\+小写字母 的情况(被JSON转义的LaTeX命令,如 \\sqrt -> \sqrt)
  76. // 保留 \\+大写字母 的情况(换行符后跟文本,如 \\CD 应保持为 \\CD)
  77. // 保留 \\+数字 或 \\+空白 的情况(矩阵换行符)
  78. $fixEscapedCommands = function ($tex) {
  79. // \\sqrt -> \sqrt, \\frac -> \frac, 但 \\CD 或 \\2 保持不变
  80. // 【修复】只匹配小写字母,因为 LaTeX 命令都是小写
  81. return preg_replace('/\\\\\\\\([a-z])/', '\\\\$1', $tex);
  82. };
  83. // 1. 处理 $$...$$ 块级公式
  84. $content = preg_replace_callback('/\$\$([\s\S]*?)\$\$/', function ($matches) use ($fixEscapedCommands) {
  85. return '$$'.$fixEscapedCommands($matches[1]).'$$';
  86. }, $content);
  87. // 2. 处理 $...$ 行内公式(避免与$$冲突)
  88. $content = preg_replace_callback('/(?<!\$)\$([^$\n]+?)\$(?!\$)/', function ($matches) use ($fixEscapedCommands) {
  89. return '$'.$fixEscapedCommands($matches[1]).'$';
  90. }, $content);
  91. // 3. 处理 \(...\) 行内公式
  92. $content = preg_replace_callback('/\\\\\(([\s\S]*?)\\\\\)/', function ($matches) use ($fixEscapedCommands) {
  93. return '\\('.$fixEscapedCommands($matches[1]).'\\)';
  94. }, $content);
  95. // 4. 处理 \[...\] 块级公式
  96. $content = preg_replace_callback('/\\\\\[([\s\S]*?)\\\\\]/', function ($matches) use ($fixEscapedCommands) {
  97. return '\\['.$fixEscapedCommands($matches[1]).'\\]';
  98. }, $content);
  99. return $content;
  100. }
  101. /**
  102. * 【新增】检查内容中是否包含任意定界符(不要求整个字符串被包裹)
  103. */
  104. private static function containsDelimiters(string $content): bool
  105. {
  106. // 检查是否包含 $...$, $$...$$, \(...\), \[...\]
  107. return preg_match('/\$\$[\s\S]*?\$\$|\$[^$\n]+?\$|\\\\\([\s\S]*?\\\\\)|\\\\\[[\s\S]*?\\\\\]/', $content) === 1;
  108. }
  109. /**
  110. * 检测内容类型
  111. * 优化:加入中文检测,避免包裹包含中文的混合内容
  112. */
  113. private static function detectContentType(string $content): string
  114. {
  115. // 优先检查是否包含定界符(使用 containsDelimiters 检测混合内容中的公式)
  116. if (self::containsDelimiters($content)) {
  117. return 'delimited';
  118. }
  119. // 检查是否包含数学特征
  120. $hasMathFeatures = self::containsMathFeatures($content);
  121. // 如果不包含数学特征,返回纯文本
  122. if (! $hasMathFeatures) {
  123. return 'plain_text';
  124. }
  125. // 检查是否包含中文字符
  126. if (preg_match('/[\x{4e00}-\x{9fa5}]/u', $content)) {
  127. // 包含中文 + 数学特征 = 混合内容,需要智能提取数学部分
  128. return 'mixed_content';
  129. }
  130. // 检查是否包含长文本(超过一定长度的字母组合)
  131. $hasLongText = preg_match('/[a-zA-Z]{8,}/', $content);
  132. if ($hasLongText) {
  133. // 包含长文本,可能是混合内容,但不包裹(保守策略)
  134. return 'plain_text';
  135. }
  136. // 检查是纯数学还是混合内容
  137. // 混合内容:同时包含数学特征和普通英文单词
  138. $hasPlainText = preg_match('/\b[a-zA-Z]{3,7}\b/', $content) &&
  139. ! preg_match('/^[a-zA-Z0-9\+\-\*\/\=\s\.\^\(\)\_\{\}]+$/', $content);
  140. if ($hasPlainText) {
  141. return 'mixed_content';
  142. }
  143. return 'pure_math';
  144. }
  145. /**
  146. * 包裹纯数学表达式
  147. * 优化:只添加定界符,不修改内容本身
  148. */
  149. private static function wrapPureMath(string $content): string
  150. {
  151. // 已经是纯数学格式,直接用 $ 包裹
  152. return '$'.$content.'$';
  153. }
  154. /**
  155. * 清理定界符内部的 HTML 标签
  156. */
  157. private static function cleanInsideDelimiters(string $content): string
  158. {
  159. // 修复:使用更精确的正则表达式,避免模式冲突
  160. // 先处理 $$...$$ 模式,然后处理单个 $...$ 模式(但确保不被$$包含)
  161. // 1. 处理 $$...$$ 显示公式
  162. $content = preg_replace_callback('/\$\$([\s\S]*?)\$\$/', function ($matches) {
  163. $cleanContent = strip_tags($matches[1]);
  164. $cleanContent = html_entity_decode($cleanContent, ENT_QUOTES, 'UTF-8');
  165. $cleanContent = trim($cleanContent);
  166. return '$$'.$cleanContent.'$$';
  167. }, $content);
  168. // 2. 处理 \(...\) 行内公式
  169. $content = preg_replace_callback('/\\\\\(([\s\S]*?)\\\\\)/', function ($matches) {
  170. $cleanContent = strip_tags($matches[1]);
  171. $cleanContent = html_entity_decode($cleanContent, ENT_QUOTES, 'UTF-8');
  172. $cleanContent = trim($cleanContent);
  173. return '\\('.$cleanContent.'\\)';
  174. }, $content);
  175. // 3. 处理 \[...\] 显示公式
  176. $content = preg_replace_callback('/\\\\\[([\s\S]*?)\\\\\]/', function ($matches) {
  177. $cleanContent = strip_tags($matches[1]);
  178. $cleanContent = html_entity_decode($cleanContent, ENT_QUOTES, 'UTF-8');
  179. $cleanContent = trim($cleanContent);
  180. return '\\['.$cleanContent.'\\]';
  181. }, $content);
  182. // 4. 最后处理 $...$ 行内公式(避免与$$冲突)
  183. $content = preg_replace_callback('/(?<!\$)\$([^$\n]+?)\$(?!\$)/', function ($matches) {
  184. $cleanContent = strip_tags($matches[1]);
  185. $cleanContent = html_entity_decode($cleanContent, ENT_QUOTES, 'UTF-8');
  186. $cleanContent = trim($cleanContent);
  187. return '$'.$cleanContent.'$';
  188. }, $content);
  189. return $content;
  190. }
  191. /**
  192. * 智能识别并包裹富文本中的数学公式
  193. * 支持:函数定义、导数表达式、LaTeX命令、数学运算
  194. */
  195. private static function smartWrapMixedContent(string $content): string
  196. {
  197. // 匹配策略:只匹配明确的数学表达式,避免误判
  198. $tagPattern = '<[^>]+>';
  199. $existingDelimiterPattern = '(?:\$\$[\s\S]*?\$\$|\$[\s\S]*?\$|\\\\\([\s\S]*?\\\\\)|\\\\\[[\s\S]*?\\\\\])';
  200. // 数学公式模式(按优先级排列)
  201. $patterns = [
  202. // 1. 函数定义: f(x) = 2x^3 - 3x^2 + 4x - 5
  203. "[a-zA-Z]'?\\([a-zA-Z0-9,\\s]+\\)\\s*=\\s*[a-zA-Z0-9\\+\\-\\*\\/\\^\\s\\.\\(\\)\\_\\{\\}]+",
  204. // 2. 导数/函数调用: f'(1), g(5), sin(x)
  205. "[a-zA-Z]+'?\\([a-zA-Z0-9\\+\\-\\*\\/\\^\\s\\.]+\\)",
  206. // 3. LaTeX 命令: \frac{1}{2}
  207. '\\\\[a-zA-Z]+\\{[^}]*\\}(?:\\{[^}]*\\})?',
  208. // 4. 数学表达式: x^2 + y^2, 2x - 3
  209. '[a-zA-Z0-9]+[\\^_][a-zA-Z0-9\\{\\}]+(?:\\s*[\\+\\-\\*\\/]\\s*[a-zA-Z0-9\\^_\\{\\}\\.]+)*',
  210. ];
  211. $mathPattern = '(?:'.implode('|', $patterns).')';
  212. $pattern = "/($tagPattern)|($existingDelimiterPattern)|($mathPattern)/u";
  213. return preg_replace_callback($pattern, function ($matches) {
  214. // HTML 标签,原样返回
  215. if (! empty($matches[1])) {
  216. return $matches[1];
  217. }
  218. // 已有的定界符,原样返回
  219. if (! empty($matches[2])) {
  220. return $matches[2];
  221. }
  222. // 数学公式,添加 $ 包裹
  223. if (! empty($matches[3])) {
  224. $math = trim($matches[3]);
  225. // 再次检查是否已经包裹
  226. if (str_contains($math, '$')) {
  227. return $math;
  228. }
  229. return '$'.$math.'$';
  230. }
  231. return $matches[0];
  232. }, $content);
  233. }
  234. /**
  235. * 检查是否已有定界符
  236. */
  237. private static function hasDelimiters(string $content): bool
  238. {
  239. $content = trim($content);
  240. // 检查 $$...$$
  241. if (str_starts_with($content, '$$') && str_ends_with($content, '$$')) {
  242. return true;
  243. }
  244. // 检查 $...$
  245. if (str_starts_with($content, '$') && str_ends_with($content, '$')) {
  246. return true;
  247. }
  248. // 检查 \[...\]
  249. if (str_starts_with($content, '\\[') && str_ends_with($content, '\\]')) {
  250. return true;
  251. }
  252. // 检查 \(...\)
  253. if (str_starts_with($content, '\\(') && str_ends_with($content, '\\)')) {
  254. return true;
  255. }
  256. return false;
  257. }
  258. /**
  259. * 检测数学特征
  260. * 优化:更精确的检测,减少误判
  261. */
  262. private static function containsMathFeatures(string $content): bool
  263. {
  264. // 1. 检查是否有 LaTeX 命令
  265. if (preg_match('/\\\\[a-zA-Z]+\{?/', $content)) {
  266. return true;
  267. }
  268. // 2. 检查函数定义或等式(如 f(x) =, g(x) =)
  269. // 必须是:字母+括号+等号+数学内容
  270. if (preg_match('/[a-zA-Z]\([a-zA-Z0-9,\s]+\)\s*=\s*[a-zA-Z0-9\+\-\*\/\^\.\(\)\s\\\\_\{]+/', $content)) {
  271. return true;
  272. }
  273. // 3. 检查纯数学表达式(只包含数字、变量、运算符、括号)
  274. // 严格的数学表达式:必须包含字母和运算符,且没有中文字符
  275. if (preg_match('/^[a-zA-Z0-9\+\-\*\/\=\s\.\^\(\)\_\{\}]+$/', $content) &&
  276. preg_match('/[a-zA-Z]/', $content) &&
  277. preg_match('/[\+\-\*\/\=\^]/', $content)) {
  278. return true;
  279. }
  280. // 4. 检查包含变量的数学表达式(带约束)
  281. // 必须有明确的运算符连接,且周围是数学内容
  282. if (preg_match('/[a-zA-Z0-9\.\^\_\{\}]\s*[\+\-\*\/]\s*[a-zA-Z0-9\.\^\_\{\}\(\)]/', $content)) {
  283. return true;
  284. }
  285. // 5. 检查分数形式(如 \frac{}{})
  286. if (preg_match('/\\\\frac\{/', $content)) {
  287. return true;
  288. }
  289. // 6. 检查上标或下标(仅当与数字/字母组合时)
  290. if (preg_match('/[a-zA-Z0-9]\s*[\^_]\s*[a-zA-Z0-9]/', $content)) {
  291. return true;
  292. }
  293. return false;
  294. }
  295. /**
  296. * 批量处理
  297. */
  298. public static function processArray(array $data, array $fieldsToProcess): array
  299. {
  300. foreach ($data as $key => &$value) {
  301. if (in_array($key, $fieldsToProcess)) {
  302. if (is_string($value)) {
  303. $value = self::processFormulas($value);
  304. } elseif (is_array($value)) {
  305. // 【修复】当字段在处理列表中且值是数组时(如 options),处理数组中的每个字符串元素
  306. $value = self::processArrayValues($value);
  307. }
  308. } elseif (is_array($value)) {
  309. $value = self::processArray($value, $fieldsToProcess);
  310. }
  311. }
  312. return $data;
  313. }
  314. /**
  315. * 【新增】递归处理数组中的所有字符串值
  316. * 用于处理 options 等数组类型的字段
  317. */
  318. private static function processArrayValues(array $arr): array
  319. {
  320. foreach ($arr as $key => &$value) {
  321. if (is_string($value)) {
  322. $value = self::processFormulas($value);
  323. } elseif (is_array($value)) {
  324. $value = self::processArrayValues($value);
  325. }
  326. }
  327. return $arr;
  328. }
  329. /**
  330. * 处理题目数据
  331. */
  332. public static function processQuestionData(array $question): array
  333. {
  334. $fieldsToProcess = [
  335. 'stem', 'content', 'question_text', 'answer',
  336. 'correct_answer', 'student_answer', 'explanation',
  337. 'solution', 'question_content', 'options',
  338. ];
  339. return self::processArray($question, $fieldsToProcess);
  340. }
  341. /**
  342. * 修复被污染的数学公式(包含重复的转义字符)
  343. */
  344. private static function fixCorruptedFormulas(string $content): string
  345. {
  346. // 简化的修复策略,只处理明确的问题
  347. // 1. 将超过2个连续的$符号减少为2个
  348. $content = preg_replace('/\${3,}/', '$$', $content);
  349. // 2. 修复$$B . - \frac{1}{2}$$ 这种格式,在选项前加空格
  350. $content = preg_replace('/\$\$([A-Z])\s*\.\s*/', '$$ $1. ', $content);
  351. // 3. 修复不完整的frac命令:\frac{1}{2} -> \frac{1}{2}
  352. $content = preg_replace('/\\\\frac\\\\({[^}]+)([^}]*)\\\\/', '\\\\frac$1}{$2}', $content);
  353. // 4. 移除孤立的反斜杠(在非LaTeX命令前的)
  354. $content = preg_replace('/\\\\(?![a-zA-Z{])/', '', $content);
  355. return $content;
  356. }
  357. }