Download tests/test_language_model.py from tailot/lingologos-geometry: direct link, hf CLI and curl.
- Browser
- Download file 10.5 kB
-
https://huggingface.co/datasets/tailot/lingologos-geometry/resolve/main/tests/test_language_model.py
- Command line
-
hf download hf://datasets/tailot/lingologos-geometry/tests/test_language_model.py
-
curl -L -o test_language_model.py https://huggingface.co/datasets/tailot/lingologos-geometry/resolve/main/tests/test_language_model.py
10.5 kB
| """Controlled language, AST round trip, algebraic model and the Rete solver.""" | |
| import json | |
| import random | |
| from fractions import Fraction as F | |
| import pytest | |
| from geometry.kernels import Point | |
| from geometry.language import ParseError, classify, describe, parse_problem, parse_sexpr | |
| from geometry.model import ModelError, build_model, parameters_of | |
| from geometry.solver import NoResult, SymbolicProblemError, encode_result, solve_numeric | |
| SENTENCES = { | |
| "Point A = (0, 0).": "(Point A \"0\" \"0\")", | |
| "point b2 = (1/2, -3).": None, # lower case point names are not allowed | |
| "Point B = (u1, 0).": "(Point B u1 \"0\")", | |
| "Line l through A and B.": "(LineThrough l A B)", | |
| "Line m: 2x + 3y = 6.": "(LineEq m \"2x+3y\" \"6\")", | |
| "Circle c with center O and radius squared 9.": "(CircleCenterR2 c O \"9\")", | |
| "Circle c with center O and radius 3.": "(CircleCenterR2 c O \"(3)^2\")", | |
| "Circle c through A, B and C.": "(CircleThrough c A B C)", | |
| "Rotation r by (3/5, 4/5) about O.": "(Rotation r \"3/5\" \"4/5\" O)", | |
| "Rotation r by (0, 1).": "(Rotation r \"0\" \"1\" origin)", | |
| "Translation t by (2, -1).": "(Translation t \"2\" \"-1\")", | |
| "Scaling s by (2, 3).": "(Scaling s \"2\" \"3\")", | |
| "Reflection f across line A-B.": "(Reflection f (LineVia A B))", | |
| "Transformation u is r then t.": "(Compose u r t)", | |
| "Let M be the midpoint of A and B.": "(Let M (Midpoint A B))", | |
| "Let G be the centroid of triangle A, B, C.": "(Let G (Centroid A B C))", | |
| "Let H be the orthocenter of triangle A, B, C.": "(Let H (Orthocenter A B C))", | |
| "Let O be the circumcenter of triangle A, B, C.": "(Let O (Circumcenter A B C))", | |
| "Let X be the intersection of line l and line A-B.": "(Let X (Intersection (LineRef l) (LineVia A B)))", | |
| "Assume A, B and C are not collinear.": "(Assume (NotCollinear A B C))", | |
| "Assume A and B are distinct.": "(Assume (Distinct A B))", | |
| "Assume u2*(u2 - u1) + u3^2 = 0.": "(Assume (Zero \"u2*(u2-u1)+u3^2\"))", | |
| "Find the intersection of line l and line m.": "(Find (Intersection (LineRef l) (LineRef m)))", | |
| "Find the circumcircle of A, B and C.": "(Find (Circumcircle A B C))", | |
| "Find the tangents from P to circle c.": "(Find (Tangents P c))", | |
| "Find the orthocenter and centroid of triangle A, B, C.": "(Find (TriangleCenters A B C))", | |
| "Find the power of P with respect to circle c.": "(Find (Power P c))", | |
| "Find the secant products from P to circle c along (1, 0) and (1, 1).": "(Find (Secants P c \"1\" \"0\" \"1\" \"1\"))", | |
| "Find the image of P under t.": "(Find (Image P t))", | |
| "Prove that A, B and C are collinear.": "(Prove (Collinear A B C))", | |
| "Prove that line A-B is perpendicular to line C-D.": "(Prove (Perpendicular A B C D))", | |
| "Prove that line A-B is parallel to line C-D.": "(Prove (Parallel A B C D))", | |
| "Prove that segment A-B is congruent to segment C-D.": "(Prove (Congruent A B C D))", | |
| "Prove that P lies on the circle with center O through A.": "(Prove (OnCircle P O A))", | |
| "Prove that M is the midpoint of A and B.": "(Prove (Midpoint M A B))", | |
| "Prove that B lies between A and C.": "(Prove (Between A B C))", | |
| "Prove that angle A-B-C is a right angle.": "(Prove (RightAngle A B C))", | |
| } | |
| def test_every_sentence_form_parses_to_the_expected_tree(sentence, expected): | |
| prob = parse_problem(sentence) | |
| assert prob.args[0].sexpr() == expected | |
| assert parse_sexpr(prob.sexpr()) == prob | |
| def test_keywords_are_case_insensitive_and_whitespace_tolerant(): | |
| a = parse_problem("point A = (1,2).\nLET m BE THE MIDPOINT OF A and B".replace("LET m", "LET M")) | |
| b = parse_problem("Point A = (1, 2). Let M be the midpoint of A and B.") | |
| assert a == b | |
| def test_assume_nonzero_list_and_aliases(): | |
| prob = parse_problem("Assume u1 != 0 and u3 != 0.") | |
| assert [s.sexpr() for s in prob.args] == ["(Assume (NonZero u1))", "(Assume (NonZero u3))"] | |
| assert parse_problem("Prove that B lies between A and C.").args[0].args[0].args == ("A", "B", "C") | |
| def test_ungrammatical_input_raises_parse_error(bad): | |
| with pytest.raises(ParseError): | |
| parse_problem(bad) | |
| def test_parse_error_reports_the_sentence_number(): | |
| with pytest.raises(ParseError, match="sentence 2"): | |
| parse_problem("Point A = (0, 0). Dance with A.") | |
| def test_ast_roundtrip_for_generated_problems(): | |
| from generate_dataset import GENERATORS, THEOREMS | |
| rng = random.Random(5) | |
| for name, gen in GENERATORS.items(): | |
| for _ in range(25): | |
| text = gen(rng)[0] | |
| prob = parse_problem(text) | |
| assert parse_sexpr(prob.sexpr()) == prob | |
| for _, _, text, _, _ in THEOREMS: | |
| prob = parse_problem(text) | |
| assert parse_sexpr(prob.sexpr()) == prob | |
| def test_classify_and_describe(): | |
| info = classify(parse_problem("Point A = (0, 0). Let M be the midpoint of A and B. Assume A and B are distinct. " | |
| "Find the power of P with respect to circle c. Prove that M lies between A and B.")) | |
| assert (len(info.declarations), len(info.lets), len(info.assumptions), len(info.queries), len(info.goals)) == (1, 1, 1, 1, 1) | |
| assert describe(info.goals[0].args[0]) == "M lies between A and B" | |
| assert describe(info.queries[0].args[0]) == "the power of P with respect to circle c" | |
| # -- algebraic model ------------------------------------------------------------------------------ | |
| TRI = "Point A = (0, 0). Point B = (u1, 0). Point C = (u2, u3). Assume u1 != 0 and u3 != 0. " | |
| def test_model_of_the_euler_line_problem(): | |
| m = build_model(parse_problem(TRI + "Let G be the centroid of triangle A, B, C. Let H be the orthocenter of triangle A, B, C. " | |
| "Let O be the circumcenter of triangle A, B, C. Prove that G, H and O are collinear.")) | |
| assert m.params == ["u1", "u2", "u3"] | |
| assert m.ring.names == ("u1", "u2", "u3", "G_x", "G_y", "H_x", "H_y", "O_x", "O_y") | |
| assert len(m.hypotheses) == 6 and len(m.goals) == 1 | |
| assert len(m.explicit_nondeg) == 2 and len(m.implicit_nondeg) >= 1 | |
| assert m.goals[0].tarski.count("B(") == 3 # collinearity is B(a,b,c) | B(b,c,a) | B(c,a,b) | |
| # nondegeneracy list has no duplicates | |
| assert len(m.nondegeneracy) == len(set(m.nondegeneracy)) | |
| def test_model_records_line_and_circle_equations(): | |
| m = build_model(parse_problem("Point A = (0, 0). Point B = (2, 1). Line l through A and B. Line m: 2x + 3y = 6. " | |
| "Circle c with center A and radius squared 5.")) | |
| kinds = {(e["kind"], e["name"]) for e in m.entities} | |
| assert kinds == {("line", "l"), ("line", "m"), ("circle", "c")} | |
| line_m = next(e for e in m.entities if e["name"] == "m") | |
| assert line_m["implicit"] == "(2)*x + (3)*y + (-6) = 0" | |
| def test_model_errors(text, msg): | |
| with pytest.raises(ModelError, match=msg): | |
| build_model(parse_problem(text)) | |
| def test_numeric_route_refuses_a_non_linear_line_equation(): | |
| """The numeric route must not build a line fact out of a quadratic equation. | |
| The algebraic model has always refused such a statement; the Rete solver used | |
| to go on and report an intersection between a parabola and a straight line. | |
| """ | |
| sol = solve_numeric(parse_problem("Point A = (0, 0). Point B = (3, 0). Line m: x^2 + y = 1. " | |
| "Line n: 2x + 3y = 7. Find the intersection of line m and line n.")) | |
| assert len(sol.errors) == 1 and "linear" in sol.errors[0][1] and "degree 2" in sol.errors[0][1] | |
| assert "m" not in sol.lines and "n" in sol.lines | |
| assert sol.results == {} | |
| # -- Rete solver --------------------------------------------------------------------------------------- | |
| MIXED = """Point A = (0, 0). Point B = (4, 0). Point C = (1, 3). | |
| Let M be the midpoint of A and B. Let H be the orthocenter of triangle A, B, C. | |
| Line l through A and C. Line m: 3x + y = 12. Circle c with center A and radius squared 9. Circle d through A, B and C. | |
| Rotation r by (3/5, 4/5) about A. Translation t by (2, -1). Transformation u is r then t. | |
| Find the intersection of line l and line m. Find the circumcircle of A, B and C. Find the tangents from B to circle c. | |
| Find the orthocenter and centroid of triangle A, B, C. Find the power of B with respect to circle c. | |
| Find the secant products from B to circle c along (-1, 0) and (-2, 1). Find the image of C under u.""" | |
| def test_rete_solver_computes_every_query_and_construction(): | |
| sol = solve_numeric(parse_problem(MIXED)) | |
| assert sol.errors == [] and sol.firings > 10 | |
| assert sol.points["M"] == Point(2, 0) and sol.points["H"] == Point(1, 1) | |
| assert sol.circles["d"].center == Point(2, 1) | |
| kinds = [k for k, _ in (sol.results[q] for q in sorted(sol.results, key=lambda s: int(s[1:])))] | |
| assert kinds == ["intersection", "circumcircle", "tangents", "centers", "power", "secants", "image"] | |
| img = sol.results["q6"][1] | |
| assert img.image == Point(F(1, 5), F(8, 5)) and img.rigid | |
| json.dumps({q: encode_result(k, p) for q, (k, p) in sol.results.items()}) # JSON serializable | |
| def test_solver_reports_degenerate_constructions_and_results(): | |
| sol = solve_numeric(parse_problem("Point A = (0, 0). Point B = (1, 1). Point C = (2, 2). " | |
| "Let H be the orthocenter of triangle A, B, C. Find the circumcircle of A, B and C.")) | |
| assert "H" not in sol.points and any(k == "H" for k, _ in sol.errors) | |
| assert isinstance(sol.results["q0"][1], NoResult) | |
| def test_symbolic_problem_is_rejected_by_the_numeric_solver(): | |
| with pytest.raises(SymbolicProblemError): | |
| solve_numeric(parse_problem("Point A = (u, 0). Point B = (1, 1). Find the circumcircle of A, B and B.")) | |
| assert parameters_of(classify(parse_problem("Point A = (u, v+1)."))) == ["u", "v"] | |