示例#1
0
def handle_term(term):
    print("ORIGINAL:")
    print(term)
    print()

    doc = None
    for term_type in term_parsers.known_types():
        print("Trying to parse '%s' ..." % term_type, end=' ')
        try:
            doc = MathDOM.fromString(term, term_type)
            print("done.")
            break
        except ParseException as e:
            print("Parsing as %s failed: %s" % (term_type, str(e).encode('UTF-8')))

    if doc is None:
        print("The term is not parsable.")
        sys.exit(0)


    print("MathML parsing done.")
    print()
    write_infix(doc)

    print()
    print("Exchanging '+' and '-' ...")
    print()
    for apply_tag in doc.xpath('//math:apply[math:plus or math:minus and count(math:*) > 2]'):
        operator = apply_tag.operatorname()
        if operator == 'plus':
            apply_tag.set_operator('minus')
        elif operator == 'minus' and apply_tag.operand_count() > 1:
            apply_tag.set_operator('plus')
    write_infix(doc)

    print()
    print("Searching for negative numbers using XPath expression '//math:cn[number() < 0]' ...")
    print()
    for cn_tag in doc.xpath('//math:cn[number() < 0]'):
        value = cn_tag.value()
        print("%s [%s]" % (value, type(value)))

    print()
    print("Serializing all sub-terms using XPath '//math:apply' ...")
    print()

    for apply_tag in doc.xpath('//math:apply'):
        print(apply_tag.serialize(converter=infix_converter))

    print()
    print('Done.')
示例#2
0
def handle_term(term):
    print "ORIGINAL:"
    print term
    print

    doc = None
    for term_type in term_parsers.known_types():
        print "Trying to parse '%s' ..." % term_type,
        try:
            doc = MathDOM.fromString(term, term_type)
            print "done."
            break
        except ParseException, e:
            print "Parsing as %s failed: %s" % (term_type, unicode(e).encode('UTF-8'))
示例#3
0
           <mo>]</mo>
          </mrow>
          <mo>-</mo>
          <mrow>
           <mi>&#952;2</mi>
           <mo>[</mo>
           <mi>t</mi>
           <mo>]</mo>
          </mrow>
         </mrow>
         <mo>]</mo>
        </mrow>
        <mn>2</mn>
       </msup>
       <mo>&#8290;</mo>
       <msub>
        <mi>m</mi>
        <mn>3</mn>
       </msub>
      </mrow>
     </mrow>
     <mo>)</mo>
    </mrow>
   </mrow>
   <mo>)</mo>
  </mrow>
 </mrow>
</math>"""

doc = MathDOM.fromString(MathMLeqn)