如何使用C#将mathml转换为单词流? [英] How can transform mathml into stream of words using C#?

查看:87
本文介绍了如何使用C#将mathml转换为单词流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将MathMl标签转换为单词串(字符串)

例如:



how i can transform the MathMl tags into stream of words (string)
for example:

<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"><mml:msqrt><mml:msup><mml:mrow><mml:mi>a</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:msup><mml:mrow><mml:mi>b</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup></mml:msqrt></mml:math>



如何将最后一个公式转换为字符串,如sqrt(a ^ 2 + b ^ 2)



i使用MMLtoCSharp,但它将公式转换为如下代码:

double a = 0.0;

double b = 0.0;

Math.Sqrt(* a * a + b * b);


how i can transform the last formula to string like this "sqrt(a^2+b^2)"

i used the MMLtoCSharp but it transform formula to code like this:
double a = 0.0;
double b = 0.0;
Math.Sqrt(*a*a+b*b);

推荐答案

如果你想在一定程度上做到这一点(请看下一段),这是不是很难做,但将是一项重要的工作。



更重要的是,大多数先进的符号数学实体都无法翻译成任何算法语言的表达式。一个基本的例子是积分。当然,如果这是sin(x)dx的积分,它可以完成,但一般情况下这在理论上是不可能的。



所以,如果要将此类转换限制为大致匹配.NET BCL System.Math 操作集的有限数学运算集,并拒绝所有其他情况,则可以执行此操作。毕竟,MathML以XML表示,它表示表达式树二进制表达式树) 。您可以使用任何XML解析库来解析某种DOM中的MathML代码。通过此DOM,您可以生成C#字符串。节点之间的父子关系将定义表达式的结构,order和()括号以及文本节点将表示终端表达式节点(例如示例中的2)或变量名称。



请注意,某些MathML实体与数学表达式无关。一个例子是行的概念,它仅与图形表示有关。通过将此节点视为常规子表达式,您可以有效地忽略此问题。你只需要输入括号,不需要任何操作(你可以考虑一个没有操作的子表达式作为普通的子表达式,一个什么都不做的一元操作)。当然,你可能会得到很多冗余的圆括号;您可以决定是否对其进行优化。



MathML语法和语义在此完整描述: W3数学



这就是你所需要的。



-SA
If you want to do it to some reasonable extent (please see next paragraph), this is not extremely hard to do, but will be a big piece of work.

More importantly, most of advanced symbolic mathematical entities cannot be translated into an expression in any algorithmic language. One basic example is the integral. Of course, if this is, say, the integral of sin(x) dx, it can be done, but in general case this is theoretically impossible.

So, if you want to limit such transformation to a limited set of mathematical operations roughly matching the set of .NET BCL System.Math operations and reject all other cases, you can do it. After all, MathML is expressed in XML, which represents an expression tree (binary expression tree ). You can use any of the XML parsing libraries to parse MathML code in a kind of DOM. From this DOM, you can generate a C# strings. The parent-child relationships between nodes will define the structure of the expression, the order and () brackets, and text nodes will represent the terminal expression nodes (such as "2" in your example), or the variable names.

Note that some MathML entities have nothing to do with mathematical expressions. One example is the concept of "row", which is related just to the graphical representation. You can effectively ignore this problem, by considering such node as a regular sub-expression. You just take in in brackets on output, without any operation (you can consider a sub-expression without operation as a "trivial" sub-expression, a unary operation which does nothing). Certainly, you may get a lot of redundant round brackets; you may decide to optimize it or not.

MathML syntax and semantics is fully described here: W3 Math.

This is all you need.

—SA


这篇关于如何使用C#将mathml转换为单词流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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