15

MathML to/from Plain Text Converter

 2 years ago
source link: https://www.codeproject.com/Tips/5331794/MathML-to-from-Plain-Text-Converter
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

MathML to/from Plain Text Converter

%7Baae426b1-c99b-43a4-90b7-e81e5be5c897%7D.PNG

Xavier Junqué i de Fortuny

Rate me:
star-fill-lg.png
star-fill-lg.png
star-fill-lg.png
star-fill-lg.png
star-fill-lg.png
5.00/5 (10 votes)
21 May 2022MIT1 min read
Converts MathML coded string to/from plain text string
The code consists of two classes in order to parse the MathML string. The first class, MathToString prepares the string. The second class, ParseML, does the parsing. Similarly, to get MathML code from text one class prepares and the other does the conversion.
Sample Image

Introduction

There seems to be few resources converting MathML code into plain text. A good reason is there is no consensus in how to format some math expressions. But for many expressions involving (+,-,/,*,^,=) operators, here is one possible converter.

The Classes Engaged

  • Class MathToString prepares the string with the MathML code so that class ParseML can perform the detailed parsing.

Preparation

First, the spaces are replaced by empty strings and some special characters are replaced too. Also, some tags not involved in the math expression, like style tags, are removed. Then any <mfrac> tag, that stands for a fraction, this is parsed first and replaced in the input string in between "|" characters so that later it can be recovered by ParseML class.

Using the Code

To convert, just call the shared method MathToString.convertToString():

VB.NET
Copy Code
Dim converted as String = MathMLToString.convertToString(MathMLcodeToConvert)

Basic Principles

The parsing method is a recursive-descent parsing: Parsing Expressions by Recursive Descent.

Evaluation method E calls T for any addition or substraction, but T calls first F for any multiplication or substraction, and F calls first P for any power possible power operation. P calls first v to get next token. If there is a "(" token, v calls recursively to T.

Copy Code
E --> T {( "+" | "-" ) T}
T --> F {( "*" | "/" ) F}
F --> P ["^" F]
P --> v | "(" E ")" | "-" T

History

  • 12th May, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

%7Baae426b1-c99b-43a4-90b7-e81e5be5c897%7D.PNG

No Biography provided


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK