It will be something like that. For the moment, I'm just implemented the
basics of presentation mathml. I will add features as I need them.
After running it, sympify should be called. What do you think about it?
def parseMML(mmlinput):
from lxml import etree
from StringIO import *
from lxml import objectify
mmlinput= mmlinput.replace(' xmlns="', ' xmlnamespace="')
parser = etree.XMLParser(ns_clean=True,remove_pis=True,remove_comments=True)
tree = etree.parse(StringIO(mmlinput), parser)
objectify.deannotate(tree,cleanup_namespaces=True,xsi=True,xsi_nil=True)
mmlinput=etree.tostring(tree.getroot())
exppy="" #this is the python expression
symvars=[] #these are symbolic variables which can eventually take part in
the expression
events = ("start", "end")
level = 0
context = etree.iterparse(StringIO(mmlinput),events=events)
for action, elem in context:
if (action=='start') and (elem.tag=='mfrac'):
level += 1
mmlaux=etree.tostring(elem[0])
(a,b)=parseMML(mmlaux)
symvars.append(b)
exppy+=a
exppy+='/'
mmlaux=etree.tostring(elem[1])
(a,b)=parseMML(mmlaux)
symvars.append(b)
exppy+=a
if (action=='end') and (elem.tag=='mfrac'):
level -= 1
if level:
continue
if (action=='start') and (elem.tag=='mrow'):
exppy+='('
if (action=='end') and (elem.tag=='mrow'):
exppy+=')'
if action=='start' and elem.tag=='mn': #this is a number
exppy+=elem.text
if action=='start' and elem.tag=='mi': #this is a variable
exppy+=elem.text
symvars.append(elem.text) #we'll return the variable, so sympy can sympify
it afterwards
if action=='start' and elem.tag=='mo': #this is a operation
exppy+=elem.text
return (exppy, symvars)
With the example: mmlinput='''<?xml version="1.0"?> <math
xmlns="http://www.w3.org/1998/Math/MathML"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/1998/Math/MathML
http://www.w3.org/Math/XMLSchema/mathml2/mathml2.xsd"> <mrow> <mfrac>
<mrow> <mn>3</mn> </mrow> <mrow> <mn>57</mn> </mrow> </mfrac> </mrow>
</math>'''
We get:
exppy='((3)/(57))'
El martes, 2 de julio de 2013 21:32:22 UTC+2, peibol escribió:
>
> Thanks, I'll try to make my function the more generic I can.
>
>
> El martes, 2 de julio de 2013 21:01:00 UTC+2, Ondřej Čertík escribió:
>>
>> On Tue, Jul 2, 2013 at 12:32 PM, peibol <[email protected]> wrote:
>> > I think xml2dict cannot accomplish it, since I get the same key for
>> multiple
>> > values...
>> >
>> > For example: mathml_prueba='''<mrow> <mfrac> <mrow> <mn>3</mn> </mrow>
>> > <mrow> <mn>5</mn> </mrow> </mfrac> </mrow> '''
>> >
>> > I get the following dict:
>> > {'mrow': {'mfrac': {'mrow': [{'mn': '3'}, {'mn': '5'}]}}}
>> > Notice the merged 'mrow' key...
>> >
>> > Any suggestion for a xml lib to parse the expression?
>>
>> lxml seems to be able to parse the expression:
>>
>> http://nbviewer.ipython.org/5912034
>>
>> Ondrej
>>
>> >
>> >
>> > El martes, 2 de julio de 2013 19:53:28 UTC+2, peibol escribió:
>> >>
>> >> The mathdox box produces Presentation Mathml.
>> >>
>> >> Sympy produces Content Mathml in the printing module, but there is the
>> c2p
>> >> function in utils module that converts Content Mathml into
>> Presentation
>> >> Mathml.
>> >>
>> >> So I'm trying the xml2dict parsing with presentation mathml... I think
>> >> that once I have the dict, at least for easy expressions like
>> fractions,
>> >> with addings, algebraic... it's nothing more than a -sophisticated-
>> >> replacement with some parenthesis abuse.
>> >>
>> >> El martes, 2 de julio de 2013 19:38:08 UTC+2, Aaron Meurer escribió:
>> >>>
>> >>> Just to be clear, is this presentation mathml or content mathml? I
>> >>> believe the latter would be much easier to parse.
>> >>>
>> >>> Aaron Meurer
>> >>>
>> >>> On Tue, Jul 2, 2013 at 12:03 PM, Ondřej Čertík <[email protected]>
>> >>> wrote:
>> >>> > On Tue, Jul 2, 2013 at 10:35 AM, Stefan Krastanov
>> >>> > <[email protected]> wrote:
>> >>> >> I am sorry, I do not think there is an out-of-the-box way to do
>> this
>> >>> >> currently.
>> >>> >>
>> >>> >> What I can suggest is to use the xml2dict python library to parse
>> the
>> >>> >> openmath xml and then write your own small routine for traversing
>> the
>> >>> >> dictionary and transforming it into a sympy expression.
>> >>> >>
>> >>> >> If you do this I am sure that the sympy team would be very
>> interested
>> >>> >> to have your work merged.
>> >>> >
>> >>> > Absolutely. Pablo, if you would send us a PR that could do that,
>> >>> > that would be awesome.
>> >>> >
>> >>> > Ondrej
>> >>> >
>> >>> >>
>> >>> >>
>> >>> >> On 2 July 2013 18:22, peibol <[email protected]> wrote:
>> >>> >>> Hehe, they don't write the expression in MathML, I'm not that
>> kind of
>> >>> >>> demon.
>> >>> >>>
>> >>> >>> They enter expressions in a textarea by means of
>> >>> >>> http://mathdox.org/formulaeditor/, from which I can get the
>> openmath
>> >>> >>> or the
>> >>> >>> (presentation) mathml .The web app is for K12 students, well
>> 12-16
>> >>> >>> years
>> >>> >>> old, so I think this way to enter mathematical expressions is one
>> of
>> >>> >>> the
>> >>> >>> easiest...
>> >>> >>>
>> >>> >>> El martes, 2 de julio de 2013 18:17:19 UTC+2, Stefan Krastanov
>> >>> >>> escribió:
>> >>> >>>>
>> >>> >>>> This is probably not very helpful, but why require the students
>> to
>> >>> >>>> write their results in MathML? It is not a very natural way to
>> write
>> >>> >>>> expression. On the other hand, if you use valid python
>> expression it
>> >>> >>>> would probably be much easier.
>> >>> >>>
>> >>> >>> --
>> >>> >>> You received this message because you are subscribed to the
>> Google
>> >>> >>> Groups
>> >>> >>> "sympy" group.
>> >>> >>> To unsubscribe from this group and stop receiving emails from it,
>> >>> >>> send an
>> >>> >>> email to [email protected].
>> >>> >>> To post to this group, send email to [email protected].
>> >>> >>> Visit this group at http://groups.google.com/group/sympy.
>> >>> >>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>> >>> >>>
>> >>> >>>
>> >>> >>
>> >>> >> --
>> >>> >> You received this message because you are subscribed to the Google
>> >>> >> Groups "sympy" group.
>> >>> >> To unsubscribe from this group and stop receiving emails from it,
>> send
>> >>> >> an email to [email protected].
>> >>> >> To post to this group, send email to [email protected].
>> >>> >> Visit this group at http://groups.google.com/group/sympy.
>> >>> >> For more options, visit https://groups.google.com/groups/opt_out.
>> >>> >>
>> >>> >>
>> >>> >
>> >>> > --
>> >>> > You received this message because you are subscribed to the Google
>> >>> > Groups "sympy" group.
>> >>> > To unsubscribe from this group and stop receiving emails from it,
>> send
>> >>> > an email to [email protected].
>> >>> > To post to this group, send email to [email protected].
>> >>> > Visit this group at http://groups.google.com/group/sympy.
>> >>> > For more options, visit https://groups.google.com/groups/opt_out.
>> >>> >
>> >>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> Groups
>> > "sympy" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> an
>> > email to [email protected].
>> > To post to this group, send email to [email protected].
>> > Visit this group at http://groups.google.com/group/sympy.
>> > For more options, visit https://groups.google.com/groups/opt_out.
>> >
>> >
>>
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.