解析MATHML纯数学表达式 [英] Parsing MathML to plain math expression

查看:143
本文介绍了解析MATHML纯数学表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MathDox 公式编辑器产生MathML了。现在我想通过MathDox生产以表达我以后可以用它来评估来找到答案MATHML转换。

I am using MathDox formula editor to produce MathML. Now I want to convert the MathML produced by MathDox to expression which I can later use to evaluate to find the answer.

For eg:

MathML:
<math xmlns='http://www.w3.org/1998/Math/MathML'>
 <mrow>
  <mn>3</mn>
  <mo>+</mo>
  <mn>5</mn>
 </mrow>
</math>

Want to convert to expression as:
3+5

现在我可以使用3 + 5得到的答案8。

Now I can use 3+5 to get answer 8.

我在搜索的JavaScript <中/ strong>或 C#此转换的解决方案。试图谷歌,但并没有获得太大的帮助。有点接近的解决方案,我发现 rel=\"nofollow\">,但它是一个桌面应用程序和商业了。不过,我想我的问题开源Web应用程序的解决方案。 。任何帮助将不胜感激。

I am in a search of javascript or c# solution for this conversion. Tried to google it, but didn't get much help. Somewhat closer solution I found here, but it is a desktop application and commercial too. However, I want open source web app solution for my problem. Any help will be appreciated.

注意:为了简单起见,我只提了简单的加法上面的例子中,但MATHML也可以包含复杂的epression 。像推导和日志

Note: For simplicity I've mentioned only simple addition in example above but the mathml can also contain complex epression like derivations and log.

推荐答案

这可以使用JavaScript中的下列步骤来实现:

This can be achieved using the following steps in JavaScript:


  1. 从MATHML转换为XML DOM

  2. 从XML DOM转换为纯文本

  3. 使用EVAL函数来获得表达的十进制值

下面的代码确实恰恰是:

The following code does precisely that:

function getDOM(xmlstring) {
    parser=new DOMParser();
    return parser.parseFromString(xmlstring, "text/xml");
}

function remove_tags(node) {
    var result = "";
    var nodes = node.childNodes;
    var tagName = node.tagName;
    if (!nodes.length) {
        if (node.nodeValue == "π") result = "pi";
        else if (node.nodeValue == " ") result = "";
        else result = node.nodeValue;
    } else if (tagName == "mfrac") {
        result = "("+remove_tags(nodes[0])+")/("+remove_tags(nodes[1])+")";
    } else if (tagName == "msup") {
        result = "Math.pow(("+remove_tags(nodes[0])+"),("+remove_tags(nodes[1])+"))";
    } else for (var i = 0; i < nodes.length; ++i) {
        result += remove_tags(nodes[i]);
    }

    if (tagName == "mfenced") result = "("+result+")";
    if (tagName == "msqrt") result = "Math.sqrt("+result+")";

    return result;
}

function stringifyMathML(mml) {
   xmlDoc = getDOM(mml);
   return remove_tags(xmlDoc.documentElement);
}

// Some testing

s = stringifyMathML("<math><mn>3</mn><mo>+</mo><mn>5</mn></math>");
alert(s);
alert(eval(s));

s = stringifyMathML("<math><mfrac><mn>1</mn><mn>2</mn></mfrac><mo>+</mo><mn>1</mn></math>");
alert(s);
alert(eval(s));

s = stringifyMathML("<math><msup><mn>2</mn><mn>4</mn></msup></math>");
alert(s);
alert(eval(s));

s = stringifyMathML("<math><msqrt><mn>4</mn></msqrt></math>");
alert(s);
alert(eval(s));



按照前面的代码,就可以延长接受MathML了。例如,它很容易增加三角或任何其他自定义功能

Following the previous code, it is possible to extend the accepted MathML. For example, it would be easy to add trigonometry or any other custom function.

有关这篇文章的目的,我使用的 MATHML编辑打造MATHML(在代码的测试部分使用)。

For the purpose of this post, I used the tool from mathml editor to build the MathML (used in the test part of the code).

这篇关于解析MATHML纯数学表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆