EduNLP.Formula

class EduNLP.Formula.Formula.Formula(formula: (<class 'str'>, typing.List[typing.Dict]), variable_standardization=False, const_mathord=None, init=True, *args, **kwargs)[source]

The part transform a formula to the parsed abstracted syntax tree.

Parameters
  • formula (str or List[Dict]) – latex formula string or the parsed abstracted syntax tree

  • variable_standardization

  • const_mathord

  • init

  • args

  • kwargs

Examples

>>> f = Formula("x")
>>> f
<Formula: x>
>>> f.ast
[{'val': {'id': 0, 'type': 'mathord', 'text': 'x', 'role': None}, 'structure': {'bro': [None, None], 'child': None, 'father': None, 'forest': None}}]
>>> f.elements
[{'id': 0, 'type': 'mathord', 'text': 'x', 'role': None}]
>>> f.variable_standardization(inplace=True)
<Formula: x>
>>> f.elements
[{'id': 0, 'type': 'mathord', 'text': 'x', 'role': None, 'var': 0}]
variable_standardization(inplace=False, const_mathord=None, variable_connect_dict=None)[source]

It makes same parmeters have the same number.

Parameters
  • inplace

  • const_mathord

  • variable_connect_dict

property ast
property elements
property ast_graph: (<class 'networkx.classes.graph.Graph'>, <class 'networkx.classes.digraph.DiGraph'>)
to_str()[source]
reset_ast(formula_ensure_str: bool = True, variable_standardization=False, const_mathord=None, *args, **kwargs)[source]
property resetable
class EduNLP.Formula.Formula.FormulaGroup(formula_list: (<class 'list'>, typing.List[str], typing.List[EduNLP.Formula.Formula.Formula]), variable_standardization=False, const_mathord=None, detach=True)[source]

The part transform a group of formula to the parsed abstracted syntax forest.

to_str[source]
ast

show all ast details

elements

just show elements’ id, type, text and role

ast_graph

draw a ast graph

Parameters
  • formula (str or List[Dict] or List[Formula]) – latex formula string or the parsed abstracted syntax tree or a group of parsed abstracted syntax tree

  • variable_standardization

  • const_mathord

  • detach

Examples

>>> fg = FormulaGroup(["x + y", "y + x", "z + x"])
>>> fg
<FormulaGroup: <Formula: x + y>;<Formula: y + x>;<Formula: z + x>>
>>> fg = FormulaGroup(["x + y", Formula("y + x"), "z + x"])
>>> fg
<FormulaGroup: <Formula: x + y>;<Formula: y + x>;<Formula: z + x>>
>>> fg = FormulaGroup(["x", Formula("y"), "x"])
>>> fg.elements
[{'id': 0, 'type': 'mathord', 'text': 'x', 'role': None}, {'id': 1, 'type': 'mathord', 'text': 'y', 'role': None}, {'id': 2, 'type': 'mathord', 'text': 'x', 'role': None}]
>>> fg = FormulaGroup(["x", Formula("y"), "x"], variable_standardization=True)
>>> fg.elements
[{'id': 0, 'type': 'mathord', 'text': 'x', 'role': None, 'var': 0}, {'id': 1, 'type': 'mathord', 'text': 'y', 'role': None, 'var': 1}, {'id': 2, 'type': 'mathord', 'text': 'x', 'role': None, 'var': 0}]
variable_standardization(inplace=False, const_mathord=None, variable_connect_dict=None)[source]

It makes same parmeters have the same number.

Parameters
  • inplace

  • const_mathord

  • variable_connect_dict

to_str()[source]
property ast
property elements
property ast_graph: (<class 'networkx.classes.graph.Graph'>, <class 'networkx.classes.digraph.DiGraph'>)
Parameters
  • formula – the parsed abstracted syntax tree

  • link_vars

  • kwargs

EduNLP.Formula.ast.str2ast(formula: str, *args, **kwargs)[source]

给字符串的接口

EduNLP.Formula.ast.get_edges(forest)[source]

构造边集合

Parameters

forest (List[Dict]) – 森林

Returns

edges – 边集合

Return type

list of tuple(src,dst,type)

EduNLP.Formula.ast.ast(formula: (<class 'str'>, typing.List[typing.Dict]), index=0, forest_begin=0, father_tree=None, is_str=False)[source]

The origin code author is https://github.com/hxwujinze

Parameters
  • formula (str or List[Dict]) – 公式字符串或通过katex解析得到的结构体

  • index (int) – 本子树在树上的位置

  • forest_begin (int) – 本树在森林中的起始位置

  • father_tree (List[Dict]) – 父亲树

  • is_str (bool) –

Returns

  • tree (List[Dict]) – 重新解析形成的特征树

  • todo (finish all types)

Notes

Some functions are not supportd in katex e.g.,

  1. tag
    • \begin{equation} \tag{tagName} F=ma \end{equation}

    • \begin{align} \tag{1} y=x+z \end{align}

    • \tag*{hi} x+y^{2x}

  2. dddot
    • \frac{ \dddot y }{ x }

For more information, refer to katex support table

建森林

Parameters

forest (List[Dict]) –

Returns

trees

Return type

List[Dict]

EduNLP.Formula.ast.katex_parse(formula)[source]

将公式传入katex进行语法解析