feat: 分数四则精确判分(Fraction)+ 分数解析三步
This commit is contained in:
+87
-9
@@ -11,6 +11,7 @@ from __future__ import annotations
|
||||
import ast
|
||||
import re
|
||||
import unicodedata
|
||||
from fractions import Fraction
|
||||
|
||||
FW = str.maketrans({
|
||||
"0": "0", "1": "1", "2": "2", "3": "3", "4": "4", "5": "5",
|
||||
@@ -58,25 +59,95 @@ def safe_eval(expr: str) -> float | None:
|
||||
if isinstance(node, ast.Constant) and not isinstance(node.value, (int, float)):
|
||||
return None
|
||||
try:
|
||||
value = eval(compile(tree, "<calc>", "eval"), {"__builtins__": {}}, {}) # noqa: S307
|
||||
value = _eval_fraction(tree.body)
|
||||
except Exception:
|
||||
return None
|
||||
if isinstance(value, complex):
|
||||
if value is None:
|
||||
return None
|
||||
return float(value)
|
||||
return value
|
||||
|
||||
|
||||
def _num(text: str | None) -> float | None:
|
||||
def _eval_fraction(node):
|
||||
"""用 Fraction 求值:3/4*2/5 要得 3/10,不能变成 0.30000000000000004。"""
|
||||
if isinstance(node, ast.Expression):
|
||||
return _eval_fraction(node.body)
|
||||
if isinstance(node, ast.Constant):
|
||||
v = node.value
|
||||
if isinstance(v, bool) or not isinstance(v, (int, float)):
|
||||
return None
|
||||
return Fraction(str(v)) if isinstance(v, float) else Fraction(v)
|
||||
if isinstance(node, ast.UnaryOp):
|
||||
v = _eval_fraction(node.operand)
|
||||
if v is None:
|
||||
return None
|
||||
return -v if isinstance(node.op, ast.USub) else v
|
||||
if isinstance(node, ast.BinOp):
|
||||
a = _eval_fraction(node.left)
|
||||
b = _eval_fraction(node.right)
|
||||
if a is None or b is None:
|
||||
return None
|
||||
if isinstance(node.op, ast.Add):
|
||||
return a + b
|
||||
if isinstance(node.op, ast.Sub):
|
||||
return a - b
|
||||
if isinstance(node.op, ast.Mult):
|
||||
return a * b
|
||||
if isinstance(node.op, ast.Div):
|
||||
return None if b == 0 else a / b
|
||||
if isinstance(node.op, ast.Mod):
|
||||
return None if b == 0 else a % b
|
||||
if isinstance(node.op, ast.Pow):
|
||||
if b.denominator != 1 or abs(b) > 8:
|
||||
return None
|
||||
return a ** int(b)
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
_MIXED = re.compile(r"^(-?\d+)(?:又|\s)(\d+)\s*/\s*(\d+)$")
|
||||
_FRAC = re.compile(r"^(-?\d+)\s*/\s*(\d+)$")
|
||||
|
||||
|
||||
def _num(text: str | None):
|
||||
"""把答案读成一个数(可能是 Fraction)。读不出返回 None。"""
|
||||
s = normalize_answer(text)
|
||||
m = re.fullmatch(r"-?\d+(?:\.\d+)?", s)
|
||||
if m:
|
||||
return float(s)
|
||||
if not s:
|
||||
return None
|
||||
q = to_qty(s)
|
||||
if q is not None:
|
||||
return q
|
||||
return safe_eval(s)
|
||||
|
||||
|
||||
def to_qty(text: str | None):
|
||||
"""支持 3/4、1又1/2、0.75、-3/4、2/8 这几种写法。"""
|
||||
raw = unicodedata.normalize("NFKC", str(text or "")).translate(FW).strip()
|
||||
raw = re.sub(r"\s+", " ", raw)
|
||||
m = _MIXED.match(raw)
|
||||
if m:
|
||||
whole, num, den = int(m.group(1)), int(m.group(2)), int(m.group(3))
|
||||
if den == 0:
|
||||
return None
|
||||
sign = -1 if whole < 0 else 1
|
||||
return sign * (abs(whole) + Fraction(num, den))
|
||||
s = normalize(text)
|
||||
if not s:
|
||||
return None
|
||||
m = _FRAC.match(s)
|
||||
if m:
|
||||
den = int(m.group(2))
|
||||
if den == 0:
|
||||
return None
|
||||
return Fraction(int(m.group(1)), den)
|
||||
if re.fullmatch(r"-?\d+(?:\.\d+)?", s):
|
||||
return Fraction(s)
|
||||
return None
|
||||
|
||||
|
||||
def _dirty(text: str | None) -> bool:
|
||||
"""算式里混进了非数字字符(认不出的笔画、字母、问号)→ 不判。"""
|
||||
raw = unicodedata.normalize("NFKC", str(text or "")).translate(FW)
|
||||
raw = raw.replace("又", "")
|
||||
return bool(re.search(r"[^0-9+\-*/%().=\s]", raw))
|
||||
|
||||
|
||||
@@ -88,11 +159,18 @@ def judge_calc(expected: str | None, given: str | None, tol: float = 1e-6):
|
||||
g = _num(given)
|
||||
if e is None or g is None:
|
||||
return None, "算式无法解析,已拒答"
|
||||
ok = abs(e - g) <= tol
|
||||
if isinstance(e, Fraction) and isinstance(g, Fraction):
|
||||
ok = e == g
|
||||
else:
|
||||
ok = abs(float(e) - float(g)) <= tol
|
||||
return ok, "数值一致" if ok else f"应为 {_fmt(e)},你写的是 {_fmt(g)}"
|
||||
|
||||
|
||||
def _fmt(x: float) -> str:
|
||||
def _fmt(x) -> str:
|
||||
if isinstance(x, Fraction):
|
||||
if x.denominator == 1:
|
||||
return str(x.numerator)
|
||||
return f"{x.numerator}/{x.denominator}"
|
||||
return str(int(x)) if abs(x - round(x)) < 1e-9 else f"{x:g}"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user