Computes the similarity score and distance metrics between two LaTeX expressions. This function evaluates the equivalence of two mathematical expressions represented in LaTeX format. It uses symbolic computation and tree-based distance metrics to calculate a similarity score and other
(answer_latex, test_latex, debug_mode=False)
| 251 | |
| 252 | |
| 253 | def EED(answer_latex, test_latex, debug_mode=False): |
| 254 | """Computes the similarity score and distance metrics between two LaTeX |
| 255 | expressions. This function evaluates the equivalence of two mathematical |
| 256 | expressions represented in LaTeX format. It uses symbolic computation and |
| 257 | tree-based distance metrics to calculate a similarity score and other |
| 258 | related metrics. |
| 259 | |
| 260 | tuple: A tuple containing the following elements: |
| 261 | - score (float): The similarity score between the two expressions (0 to 100). |
| 262 | - relative_distance (float): The normalized distance between the two expressions. |
| 263 | - answer_tree_size (int): The size of the expression tree for the answer. |
| 264 | - distance (float): The raw distance between the two expression trees. |
| 265 | Notes: |
| 266 | - If either input contains unsupported LaTeX constructs (e.g., integrals or sums), |
| 267 | the function returns default values indicating failure. |
| 268 | - If the test expression is significantly longer than the answer expression, |
| 269 | the function assumes they are not equivalent. |
| 270 | - The function uses symbolic simplification and tree-based distance metrics to |
| 271 | evaluate equivalence. |
| 272 | - In case of errors during processing, the function returns default values unless |
| 273 | `debug_mode` is enabled, in which case it raises specific exceptions. |
| 274 | Exceptions: |
| 275 | - LaTeXError: Raised when LaTeX conversion to symbolic expressions fails (if `debug_mode` is True). |
| 276 | - SymPyError: Raised when symbolic simplification or tree construction fails (if `debug_mode` is True). |
| 277 | - DistError: Raised when distance calculation fails (if `debug_mode` is True). |
| 278 | Args: |
| 279 | answer_latex: the latex expression of answer expression |
| 280 | test_latex: the latex expression of test expression |
| 281 | debug_mode: whether it raise errors or just skip it |
| 282 | Returns: |
| 283 | tuple: A tuple containing the following elements: |
| 284 | - score (float): The similarity score between the two expressions (0 to 100). |
| 285 | - relative_distance (float): The normalized distance between the two expressions. |
| 286 | - answer_tree_size (int): The size of the expression tree for the answer. |
| 287 | - distance (float): The raw distance between the two expression trees. |
| 288 | """ |
| 289 | |
| 290 | if not test_latex: |
| 291 | return 0, -1, -1, -1 |
| 292 | if '\\int' in test_latex or '\\int' in answer_latex: |
| 293 | return 0, -1, -1, -1 |
| 294 | if '\\sum' in test_latex or '\\sum' in answer_latex: |
| 295 | return 0, -1, -1, 1 |
| 296 | if answer_latex == test_latex: |
| 297 | return 100, 0.0, -1, 0 |
| 298 | if len(test_latex) > 3 * len(answer_latex): |
| 299 | return 0, -1, -1, -1 |
| 300 | |
| 301 | try: |
| 302 | |
| 303 | answer_exp = master_convert(answer_latex) |
| 304 | test_exp = master_convert(test_latex) |
| 305 | except: |
| 306 | print( |
| 307 | f'Failed to convert input latex to sympy expression,please check it' |
| 308 | ) |
| 309 | if debug_mode: |
| 310 | raise LaTeXError( |
no test coverage detected