Yes, definitely. Eventually, I would like to have a nice framework to make parsing things like this easier and have a nicer, more extensible API, but for now, I think even simple parsers like this are better than nothing.
Aaron Meurer On Wed, Jul 3, 2013 at 10:48 AM, Ondřej Čertík <ondrej.cer...@gmail.com> wrote: > Pablo, > > On Wed, Jul 3, 2013 at 5:38 AM, peibol <pabe...@gmail.com> wrote: >> 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))' > > > I think that's a great start. I assume you pass the expression to > sympify() and obtain a SymPy expression as a result. > > Aaron, I think we should have this in sympy, what do you think? We can > install lxml in .travis.yml and have this tested. If so, I can help > you Pablo with sending a PR and get it setup. > > Ondrej > > -- > 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 sympy+unsubscr...@googlegroups.com. > To post to this group, send email to sympy@googlegroups.com. > 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 sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at http://groups.google.com/group/sympy. For more options, visit https://groups.google.com/groups/opt_out.