def test_detect_function_declaration():
    result = parse_element(load_fragment_tag("function.xml"))
    symbol = result.symbols[0]

    assert symbol.element.text == "p(x;θ,y)"
    assert symbol.type_ == NodeType.FUNCTION
    assert symbol.start == 0
    assert symbol.end == 16
    assert (
        Token("(", "atom", 1, 2) in symbol.tokens
    ), "function tokens should include parentheses"
    assert (
        Token(")", "atom", 15, 16) in symbol.tokens
    ), "function tokens should include parentheses"
    assert not any(
        [t.text == "," for t in symbol.tokens]
    ), "function tokens should not include commas"
    assert not any(
        [t.text == ";" for t in symbol.tokens]
    ), "function tokens should not include semicolons"

    child_symbols = symbol.child_symbols
    assert len(child_symbols) == 4
    assert str(child_symbols[0].element) == "<mi>p</mi>"
    assert str(child_symbols[1].element) == "<mi>x</mi>"
    assert str(child_symbols[2].element) == "<mi>θ</mi>"
    assert str(child_symbols[3].element) == "<mi>y</mi>"
Esempio n. 2
0
def test_merge_contigous_symbols_delimit_at_operator():
    result = parse_element(load_fragment_tag("var1_plus_var2.xml"))
    assert str(
        result.element) == "<mrow><mi>var1</mi><mo>+</mo><mi>var2</mi></mrow>"
    assert len(result.symbols) == 2
    assert str(result.symbols[0].element) == "<mi>var1</mi>"
    assert str(result.symbols[1].element) == "<mi>var2</mi>"
def test_parse_accent():
    result = parse_element(load_fragment_tag("bar_x.xml"))
    assert len(result.symbols) == 1
    assert str(result.element) == '<mover accent="true"><mi>x</mi><mo>ˉ</mo></mover>'
    symbol = result.symbols[0]
    assert symbol.contains_affix_token
    assert symbol.tokens == [Token("x", "atom", 5, 6), Token("ˉ", "affix", 0, 5)]
Esempio n. 4
0
def test_parent_symbol_inherits_children_from_row():
    result = parse_element(load_fragment_tag("x_sub_a_plus_b.xml"))
    symbol = result.symbols[0]
    assert len(symbol.children) == 3
    assert str(symbol.children[0].element) == "<mi>x</mi>"
    assert str(symbol.children[1].element) == "<mi>a</mi>"
    assert str(symbol.children[2].element) == "<mi>b</mi>"
Esempio n. 5
0
def test_parse_prime():
    result = parse_element(load_fragment_tag("x_prime.xml"))
    assert len(result.symbols) == 1
    symbol = result.symbols[0]
    assert len(symbol.children) == 1
    assert str(symbol.children[0].element) == "<mi>x</mi>"
    assert symbol.tokens == [Token(0, 1, "x", 0), Token(1, 2, "′", 2)]
Esempio n. 6
0
def test_parse_single_symbol():
    result = parse_element(load_fragment_tag("x.xml"))
    assert len(result.symbols) == 1
    symbol = result.symbols[0]
    assert str(symbol.element) == "<mi>x</mi>"
    assert symbol.children == []
    assert symbol.tokens == [Token(0, 1, "x", 0)]
    assert result.tokens == [Token(0, 1, "x", 0)]
def test_ignore_derivative_tokens():
    result = parse_element(load_fragment_tag("delta_a_d_b.xml"))
    assert len(result.symbols) == 2
    assert str(result.symbols[0].element) == "<mi>a</mi>"
    assert str(result.symbols[1].element) == "<mi>b</mi>"

    # Make sure that derivatives tokens aren't removed from the MathML.
    assert any([e.name == "mo" and e.text == "d" for e in result.element])
    assert any([e.name == "mo" and e.text == "∂" for e in result.element])
Esempio n. 8
0
def test_merge_contiguous_symbols():
    result = parse_element(load_fragment_tag("relu.xml"))
    assert str(result.element) == "<mi>ReLU</mi>"
    symbol = result.symbols[0]
    assert str(symbol.element) == "<mi>ReLU</mi>"
    assert symbol.children == []
    assert symbol.tokens == [
        Token(0, 4, "ReLU", 0),
    ]
Esempio n. 9
0
def test_parse_node_with_child_nodes():
    result = parse_element(load_fragment_tag("x_sub_i.xml"))
    symbol = result.symbols[0]
    assert str(symbol.element) == "<msub><mi>x</mi><mi>i</mi></msub>"
    assert len(symbol.children) == 2
    assert str(symbol.children[0].element) == "<mi>x</mi>"
    assert str(symbol.children[1].element) == "<mi>i</mi>"
    assert symbol.tokens == [
        Token(0, 1, "x", 0),
        Token(2, 3, "i", 1),
    ]
Esempio n. 10
0
def test_parse_single_symbol():
    result = parse_element(load_fragment_tag("x.xml"))
    assert len(result.symbols) == 1
    symbol = result.symbols[0]
    assert str(symbol.element) == "<mi>x</mi>"
    assert symbol.type_ == NodeType.IDENTIFIER
    assert symbol.children == []
    assert symbol.tokens == [Token("x", "atom", 0, 1)]
    assert symbol.start == 0
    assert symbol.end == 1
    assert not symbol.defined
    assert not symbol.contains_affix_token
    assert result.tokens == [Token("x", "atom", 0, 1)]
Esempio n. 11
0
def test_detect_definition_of_function():
    result = parse_element(load_fragment_tag("function_definition.xml"))
    symbol = result.symbols[0]
    assert symbol.type_ == NodeType.FUNCTION
    assert symbol.defined
Esempio n. 12
0
def test_ignore_definition_in_sum_argument():
    result = parse_element(load_fragment_tag("sum_i_equals_0.xml"))
    assert str(result.symbols[0].element) == "<mi>i</mi>"
    assert not result.symbols[0].defined
Esempio n. 13
0
def test_detect_definition_with_annotated_operator():
    result = parse_element(load_fragment_tag("iid.xml"))
    assert str(result.symbols[0].element) == "<mi>x</mi>"
    assert result.symbols[0].defined
Esempio n. 14
0
def test_detect_multiple_definitions():
    result = parse_element(load_fragment_tag("multiple_definitions.xml"))
    assert str(result.symbols[0].element) == "<mi>x</mi>"
    assert result.symbols[0].defined
    assert str(result.symbols[1].element) == "<mi>y</mi>"
    assert result.symbols[1].defined
Esempio n. 15
0
def test_merge_contiguous_operators():
    result = parse_element(load_fragment_tag("double_bar.xml"))
    assert len(result.symbols) == 1
    assert str(result.element) == "<mo>∣∣</mo>"
Esempio n. 16
0
def test_dot_is_an_operator():
    result = parse_element(load_fragment_tag("dot.xml"))
    assert len(result.symbols) == 1
    assert str(result.symbols[0].element) == "<mo>.</mo>"
    assert result.symbols[0].type_ == "operator"
Esempio n. 17
0
def test_do_not_parse_error_node():
    result = parse_element(load_fragment_tag("error.xml"))
    assert result.symbols == []
    assert result.element is None
    assert result.tokens == []
Esempio n. 18
0
def test_e_is_not_a_symbol_as_exponent_base():
    result = parse_element(load_fragment_tag("e_sup_x.xml"))
    assert len(result.symbols) == 1
    assert str(result.symbols[0].element) == "<mi>x</mi>"
Esempio n. 19
0
def test_ignore_quantifiers():
    result = parse_element(load_fragment_tag("forall.xml"))
    assert len(result.symbols) == 1
    assert str(result.symbols[0].element) == "<mi>x</mi>"
Esempio n. 20
0
def test_ignore_derivative_tokens():
    result = parse_element(load_fragment_tag("delta_a_d_b.xml"))
    assert len(result.symbols) == 2
    assert str(result.symbols[0].element) == "<mi>a</mi>"
    assert str(result.symbols[1].element) == "<mi>b</mi>"
Esempio n. 21
0
def test_merge_contiguous_styled_identifiers():
    result = parse_element(load_fragment_tag("bold_relu.xml"))
    assert str(result.element) == '<mi mathvariant="bold">ReLU</mi>'
    symbol = result.symbols[0]
    assert symbol.start == 0
    assert symbol.end == 13
Esempio n. 22
0
def test_keep_identifiers_with_different_styles_separate():
    result = parse_element(load_fragment_tag("script_x_regular_y.xml"))
    assert len(result.symbols) == 2
    assert str(result.symbols[0].element) == '<mi mathvariant="script">X</mi>'
    assert str(result.symbols[1].element) == "<mi>Y</mi>"
Esempio n. 23
0
def test_detect_multiple_definitions():
    result = parse_element(load_fragment_tag("multiple_definitions.xml"))
    defined_symbols = list(filter(lambda s: s.defined, result.symbols))
    assert len(defined_symbols) == 2
    assert str(defined_symbols[0].element) == "<mi>x</mi>"
    assert str(defined_symbols[1].element) == "<mi>y</mi>"
Esempio n. 24
0
def test_quantifiers_are_operators():
    result = parse_element(load_fragment_tag("forall.xml"))
    assert len(result.symbols) == 2
    assert str(result.symbols[0].element) == "<mo>∀</mo>"
    assert str(result.symbols[1].element) == "<mi>x</mi>"
Esempio n. 25
0
def test_summation_is_not_symbol():
    result = parse_element(load_fragment_tag("sum.xml"))
    assert len(result.symbols) == 2
    assert str(result.symbols[0].element) == "<mi>i</mi>"
    assert str(result.symbols[1].element) == "<mi>N</mi>"
Esempio n. 26
0
def test_detect_definition():
    result = parse_element(load_fragment_tag("x_equals_1.xml"))
    assert str(result.symbols[0].element) == "<mi>x</mi>"
    assert result.symbols[0].defined
Esempio n. 27
0
def test_number_is_not_a_symbol():
    result = parse_element(load_fragment_tag("1.xml"))
    assert str(result.element) == "<mn>1</mn>"
    assert result.symbols == []
    assert result.tokens == [Token(0, 1, "1", 0)]
Esempio n. 28
0
def test_merge_contiguous_identifiers_into_one_with_script():
    result = parse_element(load_fragment_tag("word_sub_i.xml"))
    symbol = result.symbols[0]
    assert str(symbol.element) == "<msub><mi>word</mi><mi>i</mi></msub>"
Esempio n. 29
0
def test_d_is_symbol_on_its_own():
    result = parse_element(load_fragment_tag("d.xml"))
    assert len(result.symbols) == 1
    assert str(result.symbols[0].element) == "<mi>d</mi>"
    assert result.symbols[0].tokens == [Token(0, 1, "d", 0)]
Esempio n. 30
0
def test_parse_text_as_symbol():
    result = parse_element(load_fragment_tag("text.xml"))
    assert len(result.symbols) == 1
    assert str(result.element) == "<mtext>text</mtext>"