Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

Accept valid GeoGebra Text alignment forms

Bingxi Zhao (Frank)317675eb8376
diff --git a/deeptutor/tools/vision/ggb_validator.py b/deeptutor/tools/vision/ggb_validator.py
index c4602b20b..e9c259eec 100644
--- a/deeptutor/tools/vision/ggb_validator.py
+++ b/deeptutor/tools/vision/ggb_validator.py
@@ -229,12 +229,17 @@ def _is_scalar_argument(arg: str) -> bool:
return True
+def _is_explicit_scalar_expression(arg: str) -> bool:
+ """Only repair coordinates when the third argument cannot be a Boolean name."""
+ stripped = arg.strip()
+ return _is_scalar_argument(stripped) and not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", stripped)
+
+
def validate_text_command(command: str) -> tuple[str, list[str], list[str]]:
"""Validate a ``Text`` command for arity and LaTeX balance.
- GeoGebra accepts ``Text[<object>, <point>]`` (2 args),
- ``Text[<object>, <point>, <bool>]`` (3 args), or
- ``Text[<object>, <point>, <bool>, <bool>]`` (4 args). The common invented
+ GeoGebra also accepts a one-argument form, a two-argument substitution
+ Boolean, and a six-argument form with horizontal/vertical alignment. The common invented
form ``Text[<string>, <x>, <y>]`` is repaired into a two-argument point
when both trailing values are scalar expressions.
@@ -263,21 +268,21 @@ def validate_text_command(command: str) -> tuple[str, list[str], list[str]]:
if len(args) == 3 and not errors:
second, third = args[1], args[2]
- if _is_scalar_argument(second) and _is_scalar_argument(third):
+ if _is_scalar_argument(second) and _is_explicit_scalar_expression(third):
args[1] = f"({second},{third})"
del args[2]
fixed = command[: match.start(1)] + ",".join(args) + command[match.end(1) :]
warnings.append("Combined scalar x and y arguments into a Text[] point argument")
- elif not _is_boolean_argument(third):
+ elif _is_point_argument(second) and _is_explicit_scalar_expression(third):
errors.append(
"Invalid 3-argument Text[] signature. Use "
"Text[<object>, <point>, <bool>] with a boolean third argument, "
"or combine scalar coordinates into Text[<object>, (<x>, <y>)]."
)
- if len(args) > 4:
+ if len(args) == 5 or len(args) > 6:
errors.append(
- "Text[] supports at most four arguments. Use Text[<object>, <point>, <bool>, <bool>]."
+ "Text[] supports one to four arguments, or six with horizontal and vertical alignment."
)
return fixed, warnings, errors
diff --git a/tests/visualizers/test_ggb_validator.py b/tests/visualizers/test_ggb_validator.py
index a981743c4..00ff45721 100644
--- a/tests/visualizers/test_ggb_validator.py
+++ b/tests/visualizers/test_ggb_validator.py
@@ -52,15 +52,27 @@ class TestTextArity:
def test_uppercase_point_arguments_are_not_combined(self):
command = 'T1=Text["label",P,Q]'
result = validate_command(command)
- assert not result.is_valid
+ # Q could be a named Boolean. Static validation cannot infer its type.
+ assert result.is_valid
assert result.fixed == command
- assert any("Invalid 3-argument Text[] signature" in error for error in result.errors)
- def test_text_with_more_than_four_arguments_is_rejected(self):
+ def test_text_with_five_arguments_is_rejected(self):
command = 'T1=Text["$a^2$",(1,2),true,true,5]'
result = validate_command(command)
assert not result.is_valid
- assert any("at most four arguments" in error for error in result.errors)
+ assert any("one to four arguments, or six" in error for error in result.errors)
+
+ def test_text_with_six_alignment_arguments_passes(self):
+ command = 'T1=Text["$a^2$",(1,2),true,true,-1,0]'
+ result = validate_command(command)
+ assert result.is_valid
+ assert result.errors == []
+
+ def test_named_boolean_third_argument_is_preserved(self):
+ command = 'T1=Text["label",P,showVariables]'
+ result = validate_command(command)
+ assert result.is_valid
+ assert result.fixed == command
class TestLaTeXBalance:
@@ -132,7 +144,7 @@ class TestScriptLevel:
[
"a=Slider(1,5,0.1)",
"b=Slider(1,5,0.1)",
- 'T9=Text["$(a+b)^2$",P,Q]',
+ 'T9=Text["$(a+b)^2$",(1,2),3]',
]
)
_, _, errors = validate_ggbscript(script)