Spaces:
Running
Running
Download scripts/ebsi_tables.py from lilyjeongwon/math-tutor: direct link, hf CLI and curl.
- Browser
- Download file 4.02 kB
-
https://huggingface.co/spaces/lilyjeongwon/math-tutor/resolve/main/scripts/ebsi_tables.py
- Command line
-
hf download hf://spaces/lilyjeongwon/math-tutor/scripts/ebsi_tables.py
-
curl -L -o ebsi_tables.py https://huggingface.co/spaces/lilyjeongwon/math-tutor/resolve/main/scripts/ebsi_tables.py
4.02 kB
| r"""μ¦κ°νΒ·λΆνΈν μλ 볡μ (13 ν΄μ€, 12 λ¬Έμ λ³Έλ¬Έ 곡μ©). | |
| λ³ν μ (κΈμ μ’νλ‘ ν μΉΈμ΄ μ¬λΌμ§ λͺ¨μ): | |
| $x \cdots - 1 \cdots 3 \cdots$ $f '(x)+ 0 - 0 +$ $f(x)β 5 β - 27 β$ | |
| λ³ν ν: | |
| $\begin{array}{c|ccccc} x & \cdots & - 1 & \cdots & 3 & \cdots \\ \hline f '(x) & + & 0 & - & 0 & + \\ \hline | |
| f(x) & \nearrow & 5 & \searrow & - 27 & \nearrow \end{array}$ | |
| κ·μΉ | |
| - 머리 μ€: '$x β¦$' λλ '$t β¦$' λ‘ μμνκ³ \cdots κ° λ€μ΄ μλ μμ. μΉΈ = \cdots νλμ©, κ·Έ μ¬μ΄ κΈμ λ¬Άμ νλμ©. | |
| - λ€μ μ€λ€: 'f(x)', "f '(x)", "g ''(t)" μ²λΌ μ΄λ¦(λ³μ) λ‘ μμνλ μμ. | |
| - νμ΄ν(β β β β λ±)κ° μμΌλ©΄: νμ΄ν νλ = ν μΉΈ, νμ΄ν μ¬μ΄ κΈμ λ¬Άμ = ν μΉΈ. | |
| - μμΌλ©΄: 곡백μΌλ‘ λλ '+', '-', '0' νλ = ν μΉΈ (λΆνΈν). | |
| - μ΄λ μ€μ΄λ μΉΈ μκ° λ¨Έλ¦¬ μ€κ³Ό λ€λ₯΄λ©΄ κ·Έ νλ λ°κΎΈμ§ μλλ€(μμΌλ‘ κ³ μΉ¨). μΆμΈ‘ν΄μ μΉΈμ μ±μ°μ§ μμ. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| ARROWS = {"β": r"\nearrow", "β": r"\searrow", "β": r"\nearrow", "β": r"\searrow", "β": r"\swarrow", "β": r"\nwarrow"} | |
| HEAD = re.compile(r"\$(?P<var>[a-z]) (?P<body>[^$]*\\cdots[^$]*)\$") | |
| ROW = re.compile(r"^(?P<name>[a-z](?: *')*\([^()$]*\))(?P<body>.*)$") | |
| def _head_cols(body: str): | |
| cols, cur = [], [] | |
| for tok in body.split(): | |
| if tok == r"\cdots": | |
| if cur: | |
| cols.append(" ".join(cur)) | |
| cur = [] | |
| cols.append(r"\cdots") | |
| else: | |
| cur.append(tok) | |
| if cur: | |
| cols.append(" ".join(cur)) | |
| return cols | |
| def _row_cols(body: str): | |
| body = body.strip() | |
| if any(a in body for a in ARROWS): | |
| cols, cur = [], [] | |
| # νμ΄ν μλ€λ‘ 곡백μ λ£μ΄ λλλ€ | |
| for a in ARROWS: | |
| body = body.replace(a, f" {a} ") | |
| for tok in body.split(): | |
| if tok in ARROWS: | |
| if cur: | |
| cols.append(" ".join(cur)) | |
| cur = [] | |
| cols.append(ARROWS[tok]) | |
| else: | |
| cur.append(tok) | |
| if cur: | |
| cols.append(" ".join(cur)) | |
| return cols | |
| toks = body.split() | |
| if toks and all(t in ("+", "-", "0") for t in toks): | |
| return toks | |
| return None | |
| MERGE = re.compile(r"(\$[^$]*[ββββ][^$]*)\$ ?(κ·Ήλ|κ·Ήμ|μ΅λ|μ΅μ) ?\$") | |
| def convert_tables(text: str) -> str: | |
| # νμ΄ν μ€ μ¬μ΄μ λΌμ΄ μμ λ°μΌλ‘ λκ° 'κ·Ήλ'Β·'κ·Ήμ' λ₯Ό μΉΈμΌλ‘ ν©μΉ¨: '$f(x)β$κ·Ήλ $β$' -> '$f(x)β \text{κ·Ήλ} β$' | |
| while True: | |
| t2 = MERGE.sub(lambda m: m.group(1) + r" \text{" + m.group(2) + "} ", text) | |
| if t2 == text: | |
| break | |
| text = t2 | |
| out, pos = [], 0 | |
| for m in HEAD.finditer(text): | |
| if m.start() < pos: | |
| continue | |
| head = _head_cols(m.group("body")) | |
| rows, end = [], m.end() | |
| # μ΄μ΄μ§λ '$μ΄λ¦(x)β¦$' μμλ€ | |
| while True: | |
| m2 = re.match(r" ?\$([^$]*)\$", text[end:]) | |
| if not m2: | |
| break | |
| r = ROW.match(m2.group(1).strip()) | |
| if not r: | |
| break | |
| cols = _row_cols(r.group("body")) | |
| if cols is None: | |
| break | |
| rows.append((r.group("name"), cols)) | |
| end += m2.end() | |
| if not rows or any(len(c) != len(head) for _, c in rows): | |
| continue | |
| lines = [m.group("var") + " & " + " & ".join(head)] + [n + " & " + " & ".join(c) for n, c in rows] | |
| tex = r"$\begin{array}{c|" + "c" * len(head) + "} " + r" \\ \hline ".join(lines) + r" \end{array}$" | |
| out.append(text[pos:m.start()]) | |
| out.append(tex) | |
| pos = end | |
| out.append(text[pos:]) | |
| return "".join(out) | |
| if __name__ == "__main__": | |
| s = r"νλ‘ λνλ΄λ©΄ $x \cdots - 1 \cdots 3 \cdots$ $f '(x)+ 0 - 0 +$ $f(x)β 5 β - 27 β$ μ΄κ³ " | |
| print(convert_tables(s)) | |