模块将ascii数学转换为mathml

asciitomathml的Python项目详细描述


概述

ascii to mathml将ascii数学转换为mathml。见http://mathcs.chapman.edu/~jipsen/mathml/asciimath.html 更多细节。例如,asciitomathml将字符串x^2转换为:

<math xmlns="http://www.w3.org/1998/Math/MathML">
 <mstyle>
  <msup>
   <mi>x</mi>
   <mn>2</mn>
  </msup>
 </mstyle>
</math>

安装

以正常方式安装AsciitoMathML:

python setup.py install

或者,对于pip:

pip install asciitomathml

python 2的安装

如果需要Asciimathml for Python 2.7::
pip安装asciitomathml=.88

使用

下面将从字符串创建etree:

import asciitomathml.asciitomathml
the_string = 'x^2'
math_obj =  asciitomathml.asciitomathml.AsciiMathML()
math_obj.parse_string(the_string)

要获取树,请使用数学树方法:

math_tree = math_obj.get_tree() # math_tree is an etree object

相反,如果您想要一个xml字符串,请使用to_xml_string方法:

xml_string = math_obj.to_xml_string() # xml_string a binary string

XML字符串将具有“bytes”类型。如果你想要一种“str” 传递“unicode”类型的编码:

xml_string = math_obj.to_xml_string(encoding='unicode')

字符串将被编码为utf-8。

如果向此方法传递utf8以外的编码,则字符串将以 标准XML编码,符合XML标准:

<?xml version='1.0' encoding='utf8'?>

如果要将字符串合并到XML文档中,并且不希望 编码字符串时,您可能应该使用get_tree方法并合并 在etree文档中生成的对象。同样,不通过任何 编码到此方法时,返回的字符串将被编码为ascii和 不应包含字符串的编码部分。不管怎样 由于您需要一个不带编码的树,请传递no-encoding字符串 to-xml字符串方法的选项:

xml_string = math_obj.to_xml_string(encoding="utf8", no_encoding_string = True)

数学风格

您可以将允许的任何属性传递给<;msstyle>;。使用mstyle 创建方法时传递dictionary的选项:

math_obj =  asciitomathml.asciitomathml.AsciiMathML(mstyle={'displaystyle':'true'})

最有用的属性可能是displaystyle。通常,将此属性设置为 真的如果你将方程单独放在块中。否则,根本不要设置此值, 或者将其设置为false。mathml联盟这样解释:

For an instance of MathML embedded in a textual data format (such as HTML) in “display” mode, i.e. in place of a paragraph, displaystyle = “true” and scriptlevel = “0” for the outermost expression of the embedded MathML; if the MathML is embedded in “inline” mode, i.e. in place of a character, displaystyle = “false” and scriptlevel = “0” for the outermost expression. See Chapter 7 The MathML Interface for further discussion of the distinction between “display” and “inline” embedding of MathML and how this can be specified in particular instances. In general, a MathML renderer may determine these initial values in whatever manner is appropriate for the location and context of the specific instance of MathML it is rendering, or if it has no way to determine this, based on the way it is most likely to be used; as a last resort it is suggested that it use the most generic values displaystyle = “”true”” and scriptlevel = “”0”“.

http://www.w3.org/TR/MathML2/chapter3.html#presm.mstyle

脚本

我已经包括了两个脚本作为例子。这些脚本显示了 利比亚人。因为他们必须从文件中读取文本,形成段落,并区分 在数学标记和非数学标记之间,它们并不是用来广泛转换 文本到HTM或FO。有关此类转换,请参见:

http://docutils.sourceforge.net/

具体来说,请参见sandbox/docbook目录,其中包含 将文本转换为docbook,然后再转换为html的样式表和说明 或fo.

要使用脚本,请键入:

python scripts/asciimath2fo.py <file.txt>

或:

python scripts/asciimath2html.py <file.txt>

脚本将“`”和“`”之间的任何内容转换为mathml;否则,脚本只复制 逐字记录。请参阅示例目录中的示例。要快速入门,请尝试:

python scripts/asciimath2html.py examples/linear_regression.txt > linear.xhtml

然后在一个可以处理mathml的浏览器(比如firefox)中打开linear.xhtml

测试

要测试库,请键入:

python test/test_all.py

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java为什么这段代码要打印空字符串?   java未知错误:由于标签页崩溃,会话被删除   布尔型上的java函数if-else   java最佳蚂蚁教程/快速入门?   JAVAutil。java中的扫描程序跳过do while循环中的扫描程序输入   java我们可以在selenium中使用ExpectedConditions和FluentWait来实现通用等待方法吗?   java如何使用gson库解析JSONObject   java GWT模拟Android LinearLayout的布局重量属性?   Java正则表达式重写日期表达式   java MediaPlayer播放我的MP3文件,但非常安静   java背景不显示javaFX   用于CLI的java quarkus开发模式,如何重新启动应用程序   websphere WSJdbcDataSource的java Jar文件   java Spring 4对象不会自动连接变量   java从Dbpedia定制本体/RDF图