formula

[9]:
import matplotlib.pyplot as plt
from EduNLP.Formula import Formula
from EduNLP.Formula import FormulaGroup
from EduNLP.Formula.viz import ForestPlotter

Formula("x^2 + 1 = y")
[9]:
[{'structure': {'bro': [None, 3],
                'child': [1, 2],
                'father': None,
                'forest': None},
  'val': {'id': 0, 'role': None, 'text': '^', 'type': 'supsub'}},
 {'structure': {'bro': [None, 2], 'child': None, 'father': 0, 'forest': None},
  'val': {'id': 1, 'role': 'base', 'text': 'x', 'type': 'mathord'}},
 {'structure': {'bro': [1, None], 'child': None, 'father': 0, 'forest': None},
  'val': {'id': 2, 'role': 'sup', 'text': '2', 'type': 'textord'}},
 {'structure': {'bro': [0, 4], 'child': None, 'father': None, 'forest': None},
  'val': {'id': 3, 'role': None, 'text': '+', 'type': 'bin'}},
 {'structure': {'bro': [3, 5], 'child': None, 'father': None, 'forest': None},
  'val': {'id': 4, 'role': None, 'text': '1', 'type': 'textord'}},
 {'structure': {'bro': [4, 6], 'child': None, 'father': None, 'forest': None},
  'val': {'id': 5, 'role': None, 'text': '=', 'type': 'rel'}},
 {'structure': {'bro': [5, None],
                'child': None,
                'father': None,
                'forest': None},
  'val': {'id': 6, 'role': None, 'text': 'y', 'type': 'mathord'}}]
[10]:
f = Formula("x^2 + 1 = y", variable_standardization=True)
f.ast.nodes
[10]:
NodeView((0, 1, 2, 3, 4, 5, 6))
[11]:
f.ast.edges
[11]:
OutEdgeView([(1, 0), (2, 0)])
[12]:
ForestPlotter().export(
    f.ast, root_list=[node["val"]["id"] for node in f.element if node["structure"]["father"] is None],
)
plt.show()
../../../_images/build_blitz_formula_formula_4_0.png
[13]:
f.ast.edges
[13]:
OutEdgeView([(1, 0), (2, 0)])
[14]:
f.variable_standardization()
[14]:
[{'structure': {'bro': [None, 3],
                'child': [1, 2],
                'father': None,
                'forest': None},
  'val': {'id': 0, 'role': None, 'text': '^', 'type': 'supsub'}},
 {'structure': {'bro': [None, 2], 'child': None, 'father': 0, 'forest': None},
  'val': {'id': 1, 'role': 'base', 'text': 'x', 'type': 'mathord', 'var': 0}},
 {'structure': {'bro': [1, None], 'child': None, 'father': 0, 'forest': None},
  'val': {'id': 2, 'role': 'sup', 'text': '2', 'type': 'textord'}},
 {'structure': {'bro': [0, 4], 'child': None, 'father': None, 'forest': None},
  'val': {'id': 3, 'role': None, 'text': '+', 'type': 'bin'}},
 {'structure': {'bro': [3, 5], 'child': None, 'father': None, 'forest': None},
  'val': {'id': 4, 'role': None, 'text': '1', 'type': 'textord'}},
 {'structure': {'bro': [4, 6], 'child': None, 'father': None, 'forest': None},
  'val': {'id': 5, 'role': None, 'text': '=', 'type': 'rel'}},
 {'structure': {'bro': [5, None],
                'child': None,
                'father': None,
                'forest': None},
  'val': {'id': 6, 'role': None, 'text': 'y', 'type': 'mathord', 'var': 1}}]
[15]:
fs = FormulaGroup([
    "x^2 = y",
    "x^3 = y^2",
    "x + y = \pi"
])
fs
[15]:
[[{'structure': {'bro': [None, 3],
                'child': [1, 2],
                'father': None,
                'forest': None},
  'val': {'id': 0, 'role': None, 'text': '^', 'type': 'supsub'}},
 {'structure': {'bro': [None, 2],
                'child': None,
                'father': 0,
                'forest': [6, 12]},
  'val': {'id': 1, 'role': 'base', 'text': 'x', 'type': 'mathord'}},
 {'structure': {'bro': [1, None], 'child': None, 'father': 0, 'forest': None},
  'val': {'id': 2, 'role': 'sup', 'text': '2', 'type': 'textord'}},
 {'structure': {'bro': [0, 4], 'child': None, 'father': None, 'forest': None},
  'val': {'id': 3, 'role': None, 'text': '=', 'type': 'rel'}},
 {'structure': {'bro': [3, None],
                'child': None,
                'father': None,
                'forest': [10, 14]},
  'val': {'id': 4, 'role': None, 'text': 'y', 'type': 'mathord'}}],
 [{'structure': {'bro': [None, 8],
                'child': [6, 7],
                'father': None,
                'forest': None},
  'val': {'id': 5, 'role': None, 'text': '^', 'type': 'supsub'}},
 {'structure': {'bro': [None, 7],
                'child': None,
                'father': 5,
                'forest': [1, 12]},
  'val': {'id': 6, 'role': 'base', 'text': 'x', 'type': 'mathord'}},
 {'structure': {'bro': [6, None], 'child': None, 'father': 5, 'forest': None},
  'val': {'id': 7, 'role': 'sup', 'text': '3', 'type': 'textord'}},
 {'structure': {'bro': [5, 9], 'child': None, 'father': None, 'forest': None},
  'val': {'id': 8, 'role': None, 'text': '=', 'type': 'rel'}},
 {'structure': {'bro': [8, None],
                'child': [10, 11],
                'father': None,
                'forest': None},
  'val': {'id': 9, 'role': None, 'text': '^', 'type': 'supsub'}},
 {'structure': {'bro': [None, 11],
                'child': None,
                'father': 9,
                'forest': [4, 14]},
  'val': {'id': 10, 'role': 'base', 'text': 'y', 'type': 'mathord'}},
 {'structure': {'bro': [10, None], 'child': None, 'father': 9, 'forest': None},
  'val': {'id': 11, 'role': 'sup', 'text': '2', 'type': 'textord'}}],
 [{'structure': {'bro': [None, 13],
                'child': None,
                'father': None,
                'forest': [1, 6]},
  'val': {'id': 12, 'role': None, 'text': 'x', 'type': 'mathord'}},
 {'structure': {'bro': [12, 14], 'child': None, 'father': None, 'forest': None},
  'val': {'id': 13, 'role': None, 'text': '+', 'type': 'bin'}},
 {'structure': {'bro': [13, 15],
                'child': None,
                'father': None,
                'forest': [4, 10]},
  'val': {'id': 14, 'role': None, 'text': 'y', 'type': 'mathord'}},
 {'structure': {'bro': [14, 16], 'child': None, 'father': None, 'forest': None},
  'val': {'id': 15, 'role': None, 'text': '=', 'type': 'rel'}},
 {'structure': {'bro': [15, None],
                'child': None,
                'father': None,
                'forest': None},
  'val': {'id': 16, 'role': None, 'text': '\\pi', 'type': 'mathord'}}]]
[16]:
fs.variable_standardization()
[16]:
[[{'structure': {'bro': [None, 3],
                 'child': [1, 2],
                 'father': None,
                 'forest': None},
   'val': {'id': 0, 'role': None, 'text': '^', 'type': 'supsub'}},
  {'structure': {'bro': [None, 2],
                 'child': None,
                 'father': 0,
                 'forest': [6, 12]},
   'val': {'id': 1, 'role': 'base', 'text': 'x', 'type': 'mathord', 'var': 0}},
  {'structure': {'bro': [1, None], 'child': None, 'father': 0, 'forest': None},
   'val': {'id': 2, 'role': 'sup', 'text': '2', 'type': 'textord'}},
  {'structure': {'bro': [0, 4], 'child': None, 'father': None, 'forest': None},
   'val': {'id': 3, 'role': None, 'text': '=', 'type': 'rel'}},
  {'structure': {'bro': [3, None],
                 'child': None,
                 'father': None,
                 'forest': [10, 14]},
   'val': {'id': 4, 'role': None, 'text': 'y', 'type': 'mathord', 'var': 1}}],
 [{'structure': {'bro': [None, 8],
                 'child': [6, 7],
                 'father': None,
                 'forest': None},
   'val': {'id': 5, 'role': None, 'text': '^', 'type': 'supsub'}},
  {'structure': {'bro': [None, 7],
                 'child': None,
                 'father': 5,
                 'forest': [1, 12]},
   'val': {'id': 6, 'role': 'base', 'text': 'x', 'type': 'mathord', 'var': 0}},
  {'structure': {'bro': [6, None], 'child': None, 'father': 5, 'forest': None},
   'val': {'id': 7, 'role': 'sup', 'text': '3', 'type': 'textord'}},
  {'structure': {'bro': [5, 9], 'child': None, 'father': None, 'forest': None},
   'val': {'id': 8, 'role': None, 'text': '=', 'type': 'rel'}},
  {'structure': {'bro': [8, None],
                 'child': [10, 11],
                 'father': None,
                 'forest': None},
   'val': {'id': 9, 'role': None, 'text': '^', 'type': 'supsub'}},
  {'structure': {'bro': [None, 11],
                 'child': None,
                 'father': 9,
                 'forest': [4, 14]},
   'val': {'id': 10, 'role': 'base', 'text': 'y', 'type': 'mathord', 'var': 1}},
  {'structure': {'bro': [10, None], 'child': None, 'father': 9, 'forest': None},
   'val': {'id': 11, 'role': 'sup', 'text': '2', 'type': 'textord'}}],
 [{'structure': {'bro': [None, 13],
                 'child': None,
                 'father': None,
                 'forest': [1, 6]},
   'val': {'id': 12, 'role': None, 'text': 'x', 'type': 'mathord', 'var': 0}},
  {'structure': {'bro': [12, 14], 'child': None, 'father': None, 'forest': None},
   'val': {'id': 13, 'role': None, 'text': '+', 'type': 'bin'}},
  {'structure': {'bro': [13, 15],
                 'child': None,
                 'father': None,
                 'forest': [4, 10]},
   'val': {'id': 14, 'role': None, 'text': 'y', 'type': 'mathord', 'var': 1}},
  {'structure': {'bro': [14, 16], 'child': None, 'father': None, 'forest': None},
   'val': {'id': 15, 'role': None, 'text': '=', 'type': 'rel'}},
  {'structure': {'bro': [15, None],
                 'child': None,
                 'father': None,
                 'forest': None},
   'val': {'id': 16, 'role': None, 'text': '\\pi', 'type': 'mathord'}}]]