mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 15:39:33 +08:00
* fix(web): keep statistics notation as inline math plausibleMathBody judged inline math by Latin words, so $Var(N)$, $Cov(X,Y) and $Corr(X,Y)$ fell through escapeStrayDollars and displayed as source. Add the common statistics functions to MATH_WORDS. Also recover control characters left by JSON tool-argument decoding (U+0008/U+000C/U+000D followed by a letter) back into \b, \f and \r, so formulas already stored in notes render again. Tests: latex-normalize.test.ts. Co-Authored-By: Claude Code <noreply@anthropic.com> * fix(web): only restore a decoded escape that spells a KaTeX command A form feed or carriage return in front of a word is not always a mangled \frac or \right: text extracted from a PDF keeps a form feed between pages. Rewriting every one of them showed a stray "\f" in front of the next word. Restore the backslash only when the letters after it name a KaTeX command. --------- Co-authored-by: Claude Code <noreply@anthropic.com> Co-authored-by: zszz3 <91608029+zszz3@users.noreply.github.com>
319 lines
12 KiB
TypeScript
319 lines
12 KiB
TypeScript
import test from "node:test";
|
||
import assert from "node:assert/strict";
|
||
import {
|
||
hasBareLatexTrigger,
|
||
hasInlineMathCandidate,
|
||
normalizeMathMarkup,
|
||
} from "../lib/latex-normalize";
|
||
import { hasMarkdownMath } from "../lib/latex";
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// formulas the model did not delimit at all
|
||
// ---------------------------------------------------------------------------
|
||
|
||
test("bare LaTeX in Chinese prose is wrapped", () => {
|
||
assert.equal(
|
||
normalizeMathMarkup("反演律 \\overline{A+B}=\\bar{A}\\bar{B} 必须熟练。"),
|
||
"反演律 $\\overline{A+B}=\\bar{A}\\bar{B}$ 必须熟练。",
|
||
);
|
||
assert.equal(
|
||
normalizeMathMarkup("则 \\frac{a}{b} = c 成立。"),
|
||
"则 $\\frac{a}{b} = c$ 成立。",
|
||
);
|
||
assert.equal(
|
||
normalizeMathMarkup("答案是 \\boxed{42}。"),
|
||
"答案是 $\\boxed{42}$。",
|
||
);
|
||
});
|
||
|
||
test("bare LaTeX stops at the first English word", () => {
|
||
assert.equal(
|
||
normalizeMathMarkup("The value \\alpha is large here."),
|
||
"The value $\\alpha$ is large here.",
|
||
);
|
||
});
|
||
|
||
test("a control character left by JSON escape decoding becomes a backslash", () => {
|
||
// `\begin` in a tool argument decoded as JSON is U+0008 + "egin".
|
||
const corrupted = "\u0008egin{pmatrix} 0.5 & 0.5 \\\\ 0.5 & 0.5 \\end{pmatrix}";
|
||
|
||
const result = normalizeMathMarkup(corrupted);
|
||
|
||
assert.ok(result.includes("\\begin{pmatrix}"), result);
|
||
assert.ok(!result.includes("\u0008"), result);
|
||
assert.ok(result.includes("$$\n\\begin{pmatrix}"), result);
|
||
});
|
||
|
||
test("a control character that does not hide a LaTeX command is left alone", () => {
|
||
// A form feed between extracted PDF pages, not a mangled `\f…` command.
|
||
const text = "第一页结束\u000CChapter 2 begins here.";
|
||
assert.equal(normalizeMathMarkup(text), text);
|
||
});
|
||
|
||
test("a bare environment is promoted to display math exactly once", () => {
|
||
const result = normalizeMathMarkup(
|
||
"\\begin{aligned}\na &= b \\\\\nc &= d\n\\end{aligned}",
|
||
);
|
||
|
||
assert.ok(result.includes("$$\n\\begin{aligned}"));
|
||
assert.ok(
|
||
!result.includes("$\\begin{aligned}$"),
|
||
"the environment must not also be wrapped as inline math",
|
||
);
|
||
});
|
||
|
||
test("bare LaTeX inside a table cell stays inside its cell", () => {
|
||
assert.equal(
|
||
normalizeMathMarkup("| 公式 | 说明 |\n| --- | --- |\n| \\sqrt{x^2+1} | 模长 |"),
|
||
"| 公式 | 说明 |\n| --- | --- |\n| $\\sqrt{x^2+1}$ | 模长 |",
|
||
);
|
||
});
|
||
|
||
test("bare single-letter scripts in question stems and options render as math (#2805)", () => {
|
||
const cases = [
|
||
["谐振频率 f_0 = 20 MHz,品质因数 Q_0 = 100,通频带 B_{0.707} = 0.4 MHz。",
|
||
"谐振频率 $f_0$ = 20 MHz,品质因数 $Q_0$ = 100,通频带 $B_{0.707}$ = 0.4 MHz。"],
|
||
["R'_L = 0.3125 kΩ,p_2 = 1/4", "$R'_L$ = 0.3125 kΩ,$p_2$ = 1/4"],
|
||
["f_0", "$f_0$"],
|
||
["a_1 + b_2", "$a_1$ + $b_2$"],
|
||
["x^2 + y^{12}", "$x^2$ + $y^{12}$"],
|
||
["x_i^2", "$x_i^2$"],
|
||
["| B_{0.707} | Q_L |", "| $B_{0.707}$ | $Q_L$ |"],
|
||
];
|
||
for (const [input, expected] of cases) {
|
||
assert.equal(hasMarkdownMath(input), true, input);
|
||
assert.equal(normalizeMathMarkup(input), expected, input);
|
||
assert.equal(normalizeMathMarkup(expected), expected, input);
|
||
}
|
||
});
|
||
|
||
test("bare-script recovery preserves code, paths, links and ordinary identifiers", () => {
|
||
for (const input of [
|
||
"user_id report_2026 MAX_LEN snake_case foo_bar_baz",
|
||
"a_1.txt x^2.png ./a_1 ../a_1 /tmp/a_1 C:\\tmp\\a_1",
|
||
"https://example.com/a_1?q=x^2#b_2",
|
||
"[a_1](https://example.com/b_2) ",
|
||
"<https://example.com/a_1> <span data-id=\"a_1\">plain</span>",
|
||
"`f_0` and ``x_i^2``",
|
||
"```python\nf_0 = x_i ** 2\n```",
|
||
"~~~text\nf_0 x^2\n~~~",
|
||
" f_0 = x_i ** 2",
|
||
"> x_1 = 2",
|
||
"- ```js\n x_1 = 2\n ```",
|
||
"[label][link]\n\n[link]: a_1",
|
||
'[link]: foo.html "x_1"',
|
||
"a_1_more foo_a_1 a_12b a__1 a^^2",
|
||
"éx_1 αx_1 变量x_1 foo:x_1 x_1: value",
|
||
'{"x_1": 2} call f_0() or obj.x_1',
|
||
"x_{unfinished x_{a+b} x_{12",
|
||
"a\\_1",
|
||
]) {
|
||
assert.equal(normalizeMathMarkup(input), input, input);
|
||
assert.equal(hasMarkdownMath(input), false, input);
|
||
}
|
||
const mixed = "f_0, `x_i` and [a_1](https://example.com/b_2), user_id";
|
||
assert.equal(normalizeMathMarkup(mixed), "$f_0$, `x_i` and [a_1](https://example.com/b_2), user_id");
|
||
assert.equal(normalizeMathMarkup("@@DTMASKSCRIPT_CODE0@@ f_0"), "@@DTMASKSCRIPT_CODE0@@ $f_0$");
|
||
assert.equal(normalizeMathMarkup("`$5` and f_0"), "`$5` and $f_0$");
|
||
assert.equal(normalizeMathMarkup("$x_i$ and f_0"), "$x_i$ and $f_0$");
|
||
});
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// formulas the model put in a code span
|
||
// ---------------------------------------------------------------------------
|
||
|
||
test("a code span holding only LaTeX becomes maths", () => {
|
||
assert.equal(
|
||
normalizeMathMarkup("反演律 `\\overline{A+B}=\\bar{A}\\bar{B}` 必须熟练。"),
|
||
"反演律 $\\overline{A+B}=\\bar{A}\\bar{B}$ 必须熟练。",
|
||
);
|
||
assert.equal(
|
||
normalizeMathMarkup("公式 `$\\bar{A}$` 如上。"),
|
||
"公式 $\\bar{A}$ 如上。",
|
||
);
|
||
});
|
||
|
||
test("a code span holding code stays code", () => {
|
||
for (const input of [
|
||
"用 `$PATH` 变量。",
|
||
"运行 `npm run dev` 启动。",
|
||
"注意 `a[i] = b + 1` 这一行。",
|
||
]) {
|
||
assert.equal(normalizeMathMarkup(input), input);
|
||
}
|
||
});
|
||
|
||
test("a statistics name only reads as maths when it is called", () => {
|
||
// `var` is a keyword in a page that also has real maths, not Var(X).
|
||
const code = "设 $x$ 为变量,代码写成 `var x = 1`。";
|
||
assert.equal(normalizeMathMarkup(code), code);
|
||
assert.equal(normalizeMathMarkup("方差 $Var (N)$ 与 $Cov(X, Y)$。"), "方差 $Var (N)$ 与 $Cov(X, Y)$。");
|
||
});
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// backslashes that are not maths
|
||
// ---------------------------------------------------------------------------
|
||
|
||
test("paths, regexes and Markdown escapes are left alone", () => {
|
||
for (const input of [
|
||
"把文件放到 C:\\Users\\frank\\Desktop 目录。",
|
||
"用 \\d+ 匹配数字。",
|
||
"字面 \\*星号\\* 与 \\_下划线\\_。",
|
||
"参考文献 \\[1\\] 指出该结论。",
|
||
]) {
|
||
assert.equal(normalizeMathMarkup(input), input);
|
||
}
|
||
});
|
||
|
||
test("fenced code is never rewritten", () => {
|
||
const input = "```tex\n\\(x+y\\)\n```";
|
||
assert.equal(normalizeMathMarkup(input), input);
|
||
});
|
||
|
||
test("an info-bearing inner fence line does not close the block", () => {
|
||
// The inner ```tex line carries an info string, so per CommonMark it is code
|
||
// content, not a closing fence. The whole input is one code block and must
|
||
// pass through untouched.
|
||
const input = "```text\n```tex\n\\(x+y\\)\n```";
|
||
assert.equal(normalizeMathMarkup(input), input);
|
||
});
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// delimiters the model chose instead of $
|
||
// ---------------------------------------------------------------------------
|
||
|
||
test("backslash delimiters become dollars without padding", () => {
|
||
assert.equal(
|
||
normalizeMathMarkup("由此可得 \\( x + y = z \\) 成立。"),
|
||
"由此可得 $x + y = z$ 成立。",
|
||
);
|
||
assert.equal(
|
||
normalizeMathMarkup("由 \\(a\\) 到 \\(b\\),再到 \\(c\\)。"),
|
||
"由 $a$ 到 $b$,再到 $c$。",
|
||
);
|
||
});
|
||
|
||
test("a doubly-escaped delimiter is repaired", () => {
|
||
assert.equal(
|
||
normalizeMathMarkup("由此可得 \\\\(x+y\\\\) 成立。"),
|
||
"由此可得 $x+y$ 成立。",
|
||
);
|
||
});
|
||
|
||
test("an inline paren formula never merges two table cells", () => {
|
||
const input = "| \\(a | b\\) |\n| --- |\n| x |";
|
||
assert.equal(normalizeMathMarkup(input), input);
|
||
});
|
||
|
||
test("a loose $ pair around LaTeX is tightened, not wrapped again", () => {
|
||
// A fill-in-the-blank question: the model closes the formula before the
|
||
// blank, with a space. The bare-LaTeX pass used to wrap the body a second
|
||
// time, giving `$$\lim … =$ $`, a display block that never closes.
|
||
assert.equal(
|
||
normalizeMathMarkup("si ha $\\lim_{n \\to +\\infty} a_n = $ ____."),
|
||
"si ha $\\lim_{n \\to +\\infty} a_n =$ ____.",
|
||
);
|
||
assert.equal(
|
||
normalizeMathMarkup("Let $a_n = \\frac{1}{n}$, so $ \\lim a_n = $ ____."),
|
||
"Let $a_n = \\frac{1}{n}$, so $\\lim a_n =$ ____.",
|
||
);
|
||
});
|
||
|
||
test("a loose $ pair is tightened rather than dropped", () => {
|
||
assert.equal(
|
||
normalizeMathMarkup("由此 $ x + y $ 成立。"),
|
||
"由此 $x + y$ 成立。",
|
||
);
|
||
});
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// dollars that are money
|
||
// ---------------------------------------------------------------------------
|
||
|
||
test("currency is escaped so remark-math cannot swallow it", () => {
|
||
assert.equal(
|
||
normalizeMathMarkup("价格 $100 起,另加 $20 运费和 $5 税。"),
|
||
"价格 \\$100 起,另加 \\$20 运费和 $5 税。",
|
||
);
|
||
assert.equal(
|
||
normalizeMathMarkup("It costs $5 and the value of $x$ is 2."),
|
||
"It costs \\$5 and the value of $x$ is 2.",
|
||
);
|
||
// Statistics names are English words too: between two prices they are prose.
|
||
assert.equal(
|
||
normalizeMathMarkup("Salaries: $52k mean, $48k median."),
|
||
"Salaries: \\$52k mean, $48k median.",
|
||
);
|
||
});
|
||
|
||
test("real formulas survive the same pass", () => {
|
||
for (const input of [
|
||
"设 $x^2+y^2=z^2$ 成立。",
|
||
"设 $x$ 为变量,$y$ 为常量。",
|
||
"$\\text{速度} = \\frac{s}{t}$",
|
||
"已知 $速度 = 路程/时间$ 求解。",
|
||
"求 $N$ 的方差 $Var(N)$。",
|
||
"> 方差 $Var(N)$ 已知。",
|
||
"统计量 $Cov(X,Y)$ 与 $Corr(X,Y)$。",
|
||
"$$\n\\overline{A+B}=\\bar{A}\\bar{B}\n$$",
|
||
]) {
|
||
assert.equal(normalizeMathMarkup(input), input);
|
||
}
|
||
});
|
||
|
||
test("an unfinished formula is left alone while streaming", () => {
|
||
assert.equal(normalizeMathMarkup("设 $x^2"), "设 $x^2");
|
||
});
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// invariants
|
||
// ---------------------------------------------------------------------------
|
||
|
||
test("normalisation is idempotent", () => {
|
||
for (const input of [
|
||
"反演律 \\overline{A+B}=\\bar{A}\\bar{B} 必须熟练。",
|
||
"价格 $100 起,另加 $20 运费。",
|
||
"由此可得 \\(x+y\\) 成立。",
|
||
"\\begin{aligned}\na &= b\n\\end{aligned}",
|
||
"反演律 `\\bar{A}` 如上。",
|
||
]) {
|
||
const once = normalizeMathMarkup(input);
|
||
assert.equal(normalizeMathMarkup(once), once, input);
|
||
}
|
||
});
|
||
|
||
test("routing agrees with what normalisation produces", () => {
|
||
const shouldRender = [
|
||
"反演律 \\overline{A+B}=\\bar{A}\\bar{B} 必须熟练。",
|
||
"反演律 `\\overline{A+B}` 必须熟练。",
|
||
"由此 $ x + y $ 成立。",
|
||
"答案是 \\boxed{42}。",
|
||
];
|
||
for (const input of shouldRender) {
|
||
assert.equal(hasMarkdownMath(input), true, input);
|
||
assert.notEqual(normalizeMathMarkup(input), input, input);
|
||
}
|
||
// Already-delimited statistics notation must route to KaTeX and survive
|
||
// normalisation byte-for-byte.
|
||
assert.equal(hasMarkdownMath("求 $N$ 的方差 $Var(N)$。"), true);
|
||
|
||
const shouldNot = [
|
||
"价格是 $5 和 $10。",
|
||
"把文件放到 C:\\Users\\frank\\Desktop 目录。",
|
||
"运行 `npm run dev` 启动。",
|
||
];
|
||
for (const input of shouldNot) {
|
||
assert.equal(hasMarkdownMath(input), false, input);
|
||
}
|
||
});
|
||
|
||
test("the routing predicates answer on their own terms", () => {
|
||
assert.equal(hasInlineMathCandidate("设 $x=1$ 成立"), true);
|
||
assert.equal(hasInlineMathCandidate("Tickets cost $5 and $10."), false);
|
||
assert.equal(hasInlineMathCandidate("no dollars here"), false);
|
||
|
||
assert.equal(hasBareLatexTrigger("用 \\overline{A} 表示"), true);
|
||
assert.equal(hasBareLatexTrigger("C:\\Users\\frank"), false);
|
||
assert.equal(hasBareLatexTrigger("no backslash"), false);
|
||
});
|