开网站做备案需要什么资料,网站做下载功能,网站设计模板简约,硬件开发是做什么工作1、问题背景
我们在使用 LXML 库解析 MathML 表达式时#xff0c;可能会遇到这样一个问题#xff1a;在递归解析过程中#xff0c;我们可能会重复进入同一个节点#xff0c;导致解析结果不正确。例如#xff0c;我们希望将以下 MathML 表达式解析为 Python 表达式#x…
1、问题背景
我们在使用 LXML 库解析 MathML 表达式时可能会遇到这样一个问题在递归解析过程中我们可能会重复进入同一个节点导致解析结果不正确。例如我们希望将以下 MathML 表达式解析为 Python 表达式
?xml version1.0?
math xmlnshttp://www.w3.org/1998/Math/MathML xmlns:mmlhttp://www.w3.org/1998/Math/MathML xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://www.w3.org/1998/Math/MathML http://www.w3.org/Math/XMLSchema/mathml2/mathml2.xsdmrowmfracmn3/mn/mnmn5/mn/mn/mfrac/mrow
/math如果我们使用以下代码来解析该表达式
def parseMML(mmlinput):from lxml import etreefrom StringIO import *from lxml import objectifyexppy[]events (start, end)context etree.iterparse(StringIO(mmlinput),eventsevents)for action, elem in context:if (actionstart) and (elem.tagmrow):exppy(if (actionend) and (elem.tagmrow):exppy)if (actionstart) and (elem.tagmfrac):mmlauxetree.tostring(elem[0])exppyparseMML(mmlaux)exppy/mmlauxetree.tostring(elem[1])exppyparseMML(mmlaux)if actionstart and elem.tagmn: #this is a numberexppyelem.textreturn (exppy)那么我们得到的解析结果将是
[(, (, 3, ), /, (, 5, ), (, 3, ), (, 5, ), )]而不是我们期望的
[(, (, 3, ), /, (, 5, ), )]这是因为在解析 mfrac 节点时我们递归调用了 parseMML 函数两次分别解析了分子和分母。而在解析分子时我们又递归调用了 parseMML 函数导致重复进入了 mrow 节点。
2、解决方案
为了解决这个问题我们可以使用一个栈来保存已经解析过的节点。当我们开始解析一个新的节点时我们可以将该节点压入栈中。当我们完成解析该节点时我们可以将该节点从栈中弹出。这样我们就能够避免重复进入同一个节点。
以下代码演示了如何使用栈来避免重复进入同一个节点
def parseMML(mmlinput):from lxml import etreefrom StringIO import *from lxml import objectifyexppy[]events (start, end)context etree.iterparse(StringIO(mmlinput),eventsevents)nodestack[]for action, elem in context:if actionstart and elem.tag in nodestack:continueif (actionstart) and (elem.tagmrow):nodestack.append(elem.tag)exppy(if (actionend) and (elem.tagmrow):nodestack.pop()exppy)if (actionstart) and (elem.tagmfrac):nodestack.append(elem.tag)mmlauxetree.tostring(elem[0])exppyparseMML(mmlaux)exppy/mmlauxetree.tostring(elem[1])exppyparseMML(mmlaux)if actionstart and elem.tagmn: #this is a numberexppyelem.textreturn (exppy)使用该代码我们可以得到正确的解析结果
[(, (, 3, ), /, (, 5, ), )]