Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Virtual Environment
.venv/
venv/
ENV/
env/

# Python cache
__pycache__/
*.pyc
*.pyo
*.pyd

# Distribution / Installation
dist/
build/
*.egg-info/
*.egg
15 changes: 6 additions & 9 deletions EED/EED.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from sympy.core.numbers import Pi, Exp1,I,Infinity,NegativeInfinity
import numpy as np
import timeout_decorator
from extended_zss import ext_distance
from latex_pre_process import *
from .extended_zss import ext_distance
from .latex_pre_process import *
from sympy.simplify import *
"""
Guide:
Expand Down Expand Up @@ -197,7 +197,6 @@ def __str__(self):




def print_tree(node, indent=0):
"""Print a tree structure"""
print(' ' * indent + f'└─ {node.label}')
Expand Down Expand Up @@ -264,9 +263,9 @@ def EED(answer_latex,test_latex,debug_mode=False):

if not test_latex:
return 0,-1,-1,-1
if '\\int' in test_latex or '\\int' in answer_latex:
if '\int' in test_latex or '\int' in answer_latex:
return 0,-1,-1,-1
if '\\sum' in test_latex or '\\sum' in answer_latex:
if '\sum' in test_latex or '\sum' in answer_latex:
return 0,-1,-1,1
if answer_latex==test_latex:
return 100,0.0,-1,0
Expand Down Expand Up @@ -322,8 +321,7 @@ def EED(answer_latex,test_latex,debug_mode=False):
print("Failed to build expression tree,returning zero")
if debug_mode:
raise SymPyError(f"Failed to build the sympy expression tree.\n GT:{answer_exp}\n GEN:{test_exp}")
return 0,-1,-1,-1

return 0,-1,calc_tree_size(tree_answer),-1
distance=ext_distance(
tree_test,
tree_answer,
Expand All @@ -332,9 +330,8 @@ def EED(answer_latex,test_latex,debug_mode=False):
insert_cost=insert_tree_func,
single_remove_cost=remove_func,
remove_cost=remove_tree_func,
update_cost=update_func)
update_cost=update_func)
try:


distance=ext_distance(
tree_test,
Expand Down
3 changes: 3 additions & 0 deletions EED/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .latex_pre_process import *
from .EED import *
from .extended_zss import *
1 change: 1 addition & 0 deletions EED/extended_zss.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ def treedist(x, y):

return treedists[-1][-1]


11 changes: 5 additions & 6 deletions EED/latex_pre_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from sympy import simplify



def brackets_balanced(s: str) -> bool:
"""
Check if the brackets in a LaTeX string are balanced
Expand All @@ -26,7 +25,6 @@ def brackets_balanced(s: str) -> bool:
return len(stack) == 0



def remove_non_ascii(text):
return text.encode("ascii", errors="ignore").decode()

Expand Down Expand Up @@ -172,7 +170,7 @@ def replacer(match):
numerator, denominator = match.group(1), match.group(2)
wrap_num = f'{{{numerator}}}' if not (numerator.startswith('{') and numerator.endswith('}')) else numerator
wrap_den = f'{{{denominator}}}' if not (denominator.startswith('{') and denominator.endswith('}')) else denominator
return fr'\frac{wrap_num}{wrap_den}'
return fr'\frac{{{wrap_num}}}{{{wrap_den}}}'

return re.sub(pattern, replacer, latex_str)

Expand Down Expand Up @@ -280,7 +278,7 @@ def vec_lower_idx(input_str):
Return:
str(str): Converted
"""
pattern = r'\\vec\{([^{}]+)_{([^{}]+)}\}'
pattern = r'\\vec{([^{}]+)_{([^{}]+)}\}'
replacement = r'\\vec{\1}_{\2}'
return re.sub(pattern, replacement, input_str)
def convert_vec_syntax(text):
Expand Down Expand Up @@ -321,7 +319,7 @@ def extract_last_equal_content(s: str, strip_whitespace: bool = True) -> str:
"""
Extract the content after the last occurrence of specific mathematical comparison or assignment operators.

:param strip_whitespace: If True, removes leading and trailing whitespace from the extracted content. Defaults to True.
:param strip_whitespace: If True, removes leading and trailing whitespace from the extracted content.
(e.g., '=', '\\approx', '\\ge', '\\le', etc.) within the input string `s`. It then extracts
and returns the content that follows the operator. If no operator is found, the entire string
is returned. Optionally, leading and trailing whitespace can be stripped from the extracted content.
Expand Down Expand Up @@ -477,7 +475,7 @@ class MyConfig:
Args:
interpret_as_mixed_fractions (bool): Whether to interpert 2 \frac{1}{2} as 2/2 or 2 + 1/2
interpret_simple_eq_as_assignment (bool): Whether to interpret simple equations as assignments k=1 -> 1
interpret_contains_as_eq (bool): Whether to interpret contains as equality x \\in {1,2,3} -> x = {1,2,3}
interpret_contains_as_eq (bool): Whether to interpret contains as equality x \in {1,2,3} -> x = {1,2,3}
lowercase_symbols (bool): Whether to lowercase all symbols
"""
class MyNormalization:
Expand Down Expand Up @@ -521,3 +519,4 @@ def master_convert(s):

Sym=latex2sympy(preprocessed_stage2,normalization_config=MyNormalization(),conversion_config=MyConfig())
return Sym

77 changes: 0 additions & 77 deletions EED/readme.md

This file was deleted.

11 changes: 1 addition & 10 deletions EED/test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
from latex_pre_process import master_convert
# This is a test for the master_convert function

test_latex = r"\\boxed{t=x^2+y^2}"
converted_latex = master_convert(test_latex)
print(f"Converted LaTeX: {converted_latex}")



from EED import EED
from EED import master_convert, EED
# This is a test for the EED function
answer_latex='2 m g + 4\\frac{mv_0^2}{l}'
gen_latex_1 ="2 m g+4\\frac{mv_0^2}{l}"
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sympy
numpy
latex2sympy2-extended
timeout-decorator
30 changes: 30 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from setuptools import setup, find_packages
import os

with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

setup(
name='EED',
version='0.1.0',
packages=find_packages(),
description='A Python package for calculating the Expression Edit Distance (EED) for LaTeX expressions.',
long_description=long_description,
long_description_content_type="text/markdown",
author='PhyBench', # Should be updated in README.md
author_email='phybench@example.com', # Should be updated in README.md
url='https://github.com/phybench/phybench-eed', # Add a URL to your project
license='MIT',
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
install_requires=[
'sympy',
'numpy',
'latex2sympy2-extended',
'timeout-decorator',
],
)