Skip to content

Commit 8863706

Browse files
authored
Feature/handwritting aliases (#243)
* Added alias passing * Enforcing E to be treated as a symbol when parsing aliases * Fixed issue with parsing E
1 parent 4825aeb commit 8863706

3 files changed

Lines changed: 53 additions & 7 deletions

File tree

app/evaluation_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_multi_character_implicit_multi_variable(self):
173173
("e**ea", False, True),
174174
("e**Ea", False, True),
175175
("e^{ea}", True, True),
176-
# ("e^{Ea}", True, True), # TODO: Support aliases for latex input
176+
("e^{Ea}", True, True),
177177
]
178178
)
179179
def test_e_latex(self, response, is_latex, is_correct):

app/preview_test.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ def test_eulers_number_notation(self, response, is_latex, elementary_functions,
121121
("e**ea", False, "e^{ea}", "E**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
122122
("e**Ea", False, "e^{ea}", "E**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
123123
("e^{ea}", True, "e^{ea}", "exp(ea)", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
124-
# ("e^{Ea}", True, "e^{Ea}", "e**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}), # TODO: Clarify if we want to be able to use aliases for LaTeX?
124+
("e^{Ea}", True, "e^{Ea}", "e**ea", {"ea": {"aliases": ["ea", "Ea"], "latex": "ea"}}),
125125
("e**aea", False, "e^{aea}", "E**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
126126
("e**aEa", False, "e^{aea}", "E**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
127127
("e^{aea}", True, "e^{aea}", "exp(aea)", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
128-
# ("e^{aEa}", True, "e^{aEa}", "e**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}), # TODO: Clarify if we want to be able to use aliases for LaTeX?
128+
("e^{aEa}", True, "e^{aEa}", "e**aea", {"aea": {"aliases": ["aea", "aEa"], "latex": "aea"}}),
129129
]
130130
)
131131
def test_e_latex(self, response, is_latex, response_latex, response_sympy, symbols):
@@ -140,9 +140,35 @@ def test_e_latex(self, response, is_latex, response_latex, response_sympy, symbo
140140
assert "preview" in result.keys()
141141
preview = result["preview"]
142142

143-
assert preview["latex"] == response_latex
144-
assert preview["sympy"] == response_sympy
143+
assert preview["latex"] == response_latex, "latex_error"
144+
assert preview["sympy"] == response_sympy, "sympy_error"
145+
146+
@pytest.mark.parametrize(
147+
"response, is_latex, response_latex, response_sympy, symbols", [
148+
("ab", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
149+
("Ab", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
150+
("aB", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
151+
("AB", False, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
152+
("ab", True, "ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
153+
("Ab", True, "Ab", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
154+
("aB", True, "aB", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
155+
("AB", True, "AB", "ab", {"ab": {"aliases": ["Ab", "AB", "aB"], "latex": "ab"}}),
156+
]
157+
)
158+
def test_alias(self, response, is_latex, response_latex, response_sympy, symbols):
159+
params = {
160+
"is_latex": is_latex,
161+
"strict_syntax": False,
162+
"elementary_functions": True,
163+
"symbols": symbols,
164+
}
165+
166+
result = preview_function(response, params)
167+
assert "preview" in result.keys()
168+
preview = result["preview"]
145169

170+
assert preview["latex"] == response_latex, "latex error"
171+
assert preview["sympy"] == response_sympy, "sympy error"
146172

147173
@pytest.mark.parametrize(
148174
"response, is_latex, response_latex, response_sympy",

app/utility/preview_utilities.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import re
22
from typing import TypedDict
3+
4+
from sympy.parsing.sympy_parser import standard_transformations, implicit_multiplication_application
35
from typing_extensions import NotRequired
46

5-
from sympy import Symbol
7+
from sympy import Symbol, parse_expr, srepr
68
from latex2sympy2 import latex2sympy
79

810
from copy import deepcopy
@@ -177,6 +179,24 @@ def parse_latex(response: str, symbols: SymbolDict, simplify: bool, parameters=N
177179
substitutions[latex_symbol_str_postprocess] = Symbol(sympy_symbol_str)
178180

179181

182+
aliases = symbols[sympy_symbol_str]['aliases']
183+
transformations = (standard_transformations + (implicit_multiplication_application,))
184+
for alias in aliases:
185+
if not alias.strip():
186+
continue
187+
try:
188+
parsed_alias = parse_expr(
189+
alias,
190+
transformations=transformations,
191+
global_dict={},
192+
local_dict={'Symbol': Symbol,'E': Symbol("E")}
193+
)
194+
substitutions[parsed_alias] = Symbol(sympy_symbol_str)
195+
except Exception as e:
196+
print(e)
197+
substitutions[Symbol(alias)] = Symbol(sympy_symbol_str)
198+
199+
180200
parsed_responses = set()
181201
for expression in response_set:
182202
try:
@@ -195,7 +215,7 @@ def parse_latex(response: str, symbols: SymbolDict, simplify: bool, parameters=N
195215
if simplify is True:
196216
expression_postprocess = expression_postprocess.simplify()
197217

198-
parsed_responses.add(str(expression_postprocess.xreplace(substitutions)))
218+
parsed_responses.add(str(expression_postprocess.subs(substitutions)))
199219

200220
if len(parsed_responses) < 2:
201221
return parsed_responses.pop()

0 commit comments

Comments
 (0)