Skip to content

Commit 19a8d85

Browse files
Merge pull request #210 from lambda-feedback/tr182-override-elementary-function-alternatives
Updated expression_utilities to ensure that input symbol alternatives…
2 parents a59692c + f449b7a commit 19a8d85

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

app/expression_utilities.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,26 @@ def substitute_input_symbols(exprs, params):
233233

234234
substitutions = [(expr, expr) for expr in params.get("reserved_keywords",[])]
235235

236+
input_symbols = params.get("symbols",dict())
237+
238+
input_symbols_alternatives = []
239+
for (code, definition) in input_symbols.items():
240+
input_symbols_alternatives += definition["aliases"]
241+
236242
if params.get("elementary_functions", False) is True:
237243
alias_substitutions = []
238244
for expr in exprs:
239245
for (name, alias_list) in elementary_functions_names+special_symbols_names:
240-
if name in expr:
241-
alias_substitutions += [(name, " "+name)]
242-
for alias in alias_list:
243-
if alias in expr:
244-
alias_substitutions += [(alias, " "+name)]
246+
if name in input_symbols_alternatives:
247+
continue
248+
else:
249+
if (name in expr) and not (name in input_symbols_alternatives):
250+
alias_substitutions += [(name, " "+name)]
251+
for alias in alias_list:
252+
if (alias in expr) and not (alias in input_symbols_alternatives):
253+
alias_substitutions += [(alias, " "+name)]
245254
substitutions += alias_substitutions
246255

247-
input_symbols = params.get("symbols",dict())
248-
249256
if "symbols" in params.keys():
250257
# Removing invalid input symbols
251258
input_symbols_to_remove = []

app/symbolic_comparison_evaluation_tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,21 @@ def test_as_substring_in_expression(self):
15581558
result = evaluation_function(response, answer, params)
15591559
assert result["is_correct"] is True
15601560

1561+
def test_alternatives_to_input_symbols_takes_priority_over_elementary_function_alternatives(self):
1562+
answer = "Ef*a/b"
1563+
params = {
1564+
"strict_syntax": False,
1565+
"elementary_functions": True,
1566+
"symbols": {
1567+
"Ef": {"aliases": ["E"], "latex": r"$E$"},
1568+
},
1569+
}
1570+
response = "E*a/b"
1571+
result = evaluation_function(response, answer, params)
1572+
assert result["is_correct"] is True
1573+
response = "e*a/b"
1574+
result = evaluation_function(response, answer, params)
1575+
assert result["is_correct"] is False
15611576

15621577
if __name__ == "__main__":
15631578
pytest.main(['-xk not slow', "--tb=line", '--durations=10', os.path.abspath(__file__)])

0 commit comments

Comments
 (0)