首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript的MathML生成算法。任何推荐的参考资料

Javascript的MathML生成算法。任何推荐的参考资料
EN

Stack Overflow用户
提问于 2014-02-08 22:56:44
回答 1查看 568关注 0票数 3

我正在尝试构建能够为传统数学输入生成MathML的代码。我正在使用JavaScript进行编码。是否有任何参考或推荐阅读,我可以通过了解所需的算法?我正在阅读W3C标准的MathML,这是一个参考标准,但不是算法。

例如,对于示例输入

代码语言:javascript
运行
复制
sqrt 9 * 5 + 20

我想生成如下所示的MathML表达式

代码语言:javascript
运行
复制
<math xmlns='w3.org/1998/Math/MathML'>; <mrow> <mrow> <mn>5</mn> <mo>&#8290;</mo> <mn>9</mn> <mo>&#8290;</mo> <mi>SQRT</mi> </mrow> <mo>+</mo> <mn>20</mn> </mrow> </math>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-09 14:44:48

我在这里找到了一个关于MathML的很好的教程:http://rypress.com/tutorials/mathml/basic-algebra.html,并开始编写一个非常基本的代数解析器(例如,4*sqrt(x+6)=(5-z)*y/7),其中包含一个处理括号的粗略堆栈和一个示例sqrt函数。这就是你要找的方向吗?

这里的摇摆不定:http://jsfiddle.net/alhambra1/bSJyE/

JavaScript代码:

代码语言:javascript
运行
复制
<script>
document.write('<p><input id="input" size=50>')
document.write('<button onclick="convertToMathML()">Convert</button></p>')
document.write('<div id="output"></div>')

function lex(str,ptr){
    var ascii = str.charCodeAt(ptr),
        lexeme = {string: "", type: ""},
        operators = {"+": "+"
                    , "-": "-"
                    , "*": "&times;"
                    , "/": "&divide;"
                    , "=": "="},
        functions = {sqrt: "msqrt"}

    //identify type
    if (ascii == 41)
        lexeme.type = "closeBracket"
    else if (ascii == 40){
        lexeme.type = "func"
        lexeme.func = "mfenced"
    }
    else if (ascii > 45 && ascii < 58 && ascii != 47)
        lexeme.type = "mn"
    else if ((ascii > 64 && ascii < 91) || (ascii > 96 && ascii < 123)){
        for (i in functions){
            if (str.substr(ptr,i.length).toLowerCase() == i){
                lexeme.type = "func"
                lexeme.func = functions[i]
                ptr += i.length - 1
            } else
                lexeme.type = "mi"
        }
    } else if (!operators[str.charAt(ptr)])
        return {string: str.charAt(ptr), type: "error", pointer: ptr}
    else
        lexeme.type = "mo"

    switch (lexeme.type){
        case "mo":
            lexeme.string = operators[str.charAt(ptr++)]
            break
        default:
            lexeme.string = str.charAt(ptr++)
            break
    }

    ascii = str.charCodeAt(ptr)

    //identify numbers and functions 
    if (lexeme.type == "mn"){
        while (ptr < str.length && ascii > 45 && ascii < 58 && ascii != 47){
            lexeme.string += str.charAt(ptr)
            ascii = str.charCodeAt(++ptr)
        }
    } else if (lexeme.type == "func" && lexeme.func != "mfenced"){
        while (ptr < str.length && str.substr(ptr).match(/^\s/)){
            ascii = str.charCodeAt(++ptr)
        }
        if (str.charAt(ptr) != "(")
            return {string: str.charAt(ptr), type: "error", pointer: ptr}
        else
            ptr++
    }

    lexeme["pointer"] = ptr

    return lexeme
}

function markup(lexeme){
    return "<" + lexeme.type + ">\n"
           + lexeme.string + "\n"
           + "</" + lexeme.type + ">\n"
}

function convertToMathML(){
    var str = document.getElementById('input').value,
        expression = "",
        ptr = 0,
        stack = []

    while (ptr < str.length){
        var currLexeme = lex(str,ptr)

        if (currLexeme.type == "closeBracket"){
            if (stack.length == 0)
                expression = "Extra bracket at: " + (currLexeme.pointer - 1)
            else
                expression += "</" + stack.pop().func + ">\n"
                            + "</mrow>"          
            ptr = currLexeme.pointer
        } else if (currLexeme.type == "error"){
            expression = "Cannot parse \"" + currLexeme.string
                       + "\" at " + currLexeme.pointer
            break
        } else if (currLexeme.type == "func"){
            expression += "<" + currLexeme.func + ">\n"
                        + "<mrow>\n"
            stack.push(currLexeme)
            ptr = currLexeme.pointer
        } else {
            expression += markup (currLexeme)
            ptr = currLexeme.pointer
        }
    }

    if (ptr >= str.length && stack.length > 0)
        expression = "Missing " +  stack.length + " closing bracket/s."

    expression = "<math xmlns='http://www.w3.org/1998/Math/MathML'>"
               + expression + "</math>"

    document.getElementById('output').innerHTML = expression
}
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21652835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档