[Python] Latex formulas common parts detector

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I have a task to make an anti-plagiarism of formulas. It is necessary for it to highlight similar places in two formulas (example in the photo). But there is a problem, after I found these similar places I cannot highlight them in the original formulas, since their entry in Latex is slightly different. Help please!

    [​IMG]

    Code:

    import sympy
    from sympy.parsing.latex import parse_latex



    def flatten(nested_list) -> list:
    result = []
    for item in nested_list:
    if isinstance(item, list):
    result.extend(flatten(item))
    else:
    result.append(item)
    return result


    def expression_tree(expression, depth=0, max_depth=1):
    if max_depth is not None and depth > max_depth or expression.is_Atom:
    return str(expression)

    tree_list = []
    for arg in expression.args:
    tree_list.append(expression_tree(arg, depth + 1, max_depth))

    return tree_list

    def rec_check_expressions(expr, expressions):
    for i in expressions:
    if sympy.parse_expr(i).equals(expr):
    return [sympy.sympify(i), sympy.sympify(expr)]
    if type(expr) is str:
    return []
    return [rec_check_expressions(a, expressions) for a in flatten(expression_tree(expr))]


    def get_same_parts(expr1, expr2, min_len):
    depth = 1
    all_subtrees1 = []
    _ = []
    while 1:
    subtrees1 = flatten(expression_tree(expr1, max_depth=depth))
    if subtrees1 == _:
    break

    all_subtrees1.append(subtrees1)
    all_subtrees1 = flatten(all_subtrees1)

    _ = subtrees1
    depth += 1

    all_subtrees1 = [a for a in set(all_subtrees1) if len(a) > min_len]

    return rec_check_expressions(expr2, all_subtrees1)


    exp1 = parse_latex(r"\sqrt{(x^2-12x-325)}-\cos(12\pi-x^2)")
    exp2 = parse_latex(r"|(x-25)(x+13)|+\sin(-x*x+6\pi+5\pi+\pi)")
    parts = get_same_parts(exp1, exp2, 4)

    exp1 = sympy.latex(sympy.sympify(exp1)).replace(" ", "")
    exp2 = sympy.latex(sympy.sympify(exp2)).replace(" ", "")


    for i, j in parts:
    a = sympy.latex(i).replace(" ", "")
    b = sympy.latex(j).replace(" ", "")
    exp1 = exp1.replace(a, "\colorbox{#88E788}{$" + a + "}$")
    exp2 = exp2.replace(b, "\colorbox{#88E788}{$" + b + "}$")


    print(exp1)
    print(exp2)




    Help please!!!!!!!!!!

    Continue reading...

Compartilhe esta Página