`\frac` and `\sqrt` shorthand with a command argument (`\frac\pi4`, `\sqrt\pi`) is mangled, which can verify a wrong answer

オープン 初心者向け
#102 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend

調査の方向性

math_normalization.pyを読み、特にリンク先の行にある_fix_fracsと_fix_sqrtを確認してください。提供された\frac\pi4と\sqrt\piの例で再現し、既存のテストが省略形の引数をどのようにカバーしているかを確認してください。両方の関数でコマンド引数が全体のまま保たれ、提供された検証ケースが期待どおりの結果を出せば完了です。

索引モデルが issue の本文から書いたものです。

説明

_fix_fracs and _fix_sqrt in latex2sympy2_extended's math_normalization.py treat the first character after \frac or \sqrt as a whole argument. When the argument is a command such as \pi, they split it: the backslash becomes one argument and the command's first letter the next. The result either fails to parse or parses to a different value.

Reproduction with math-verify 0.9.0 and latex2sympy2_extended 1.11.0:

from math_verify import parse, verify

P = lambda s: parse("$" + s + "$")

P(r"\frac\pi4")    # ['\\frac{\\}{p}i4']        no expression
P(r"\frac \pi4")   # ['\\frac{\\}{p}i4']        no expression
P(r"\frac 1\pi")   # [1, '\\frac{1}{\\}pi']     parses as 1
P(r"\sqrt\pi")     # ['\\sqrt{\\}pi']            no expression
P(r"\frac{\pi}{4}")  # [pi/4, ...]               braced form is fine

verify(P(r"\frac \pi4"), P(r"\frac{\pi}{4}"))         # False, should be True
verify(P(r"\frac \pi4 + 2 - \sqrt2"), P("2"))          # True, should be False

The last case is a wrong answer verified as correct. The gold parses to 2 because the mangled fraction drops out. That gold is a real one: it is the boxed answer of a problem in the MATH test set, written \frac \pi4 + 2 - \sqrt2.

Cause: in _fix_fracs, after substr.lstrip() the code takes a = substr[0] and b = substr[1] as single characters. _fix_sqrt does the same with a = split[0]. Both come from Hendrycks' MATH code (math_equivalence.py), which has the same limitation.

Suggested fix: when the next character is a backslash, take the whole command (\\[a-zA-Z]+) as the argument, in both functions. For example, read each argument with a pattern like \\[a-zA-Z]+|[^\s{}\\]. \frac\pi4 then becomes \frac{\pi}{4}, and \sqrt\pi becomes \sqrt{\pi}.

Posted by Claude Code on Matt's behalf.

主要言語
Python
スター
1.2k
フォーク
67
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

huggingface/Math-Verify のほかの issue

huggingface/Math-Verify の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。