模块将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 web应用程序组合到一个web应用程序(war)中而不相互影响?   java一次屏蔽两位   java如何在多个类上初始化元素?   java在后台服务中处理通知或使用GCM(或其他推送通知服务)   java从const方法调用JNI函数   javascript如何使用函数/方法返回?   Java优化:声明类变量与使用临时变量   java字符算术基数8 vs基数10   Java流收集要存储的对象列表   swing我正在用Java中的keyListener制作一个精灵移动器   在Gradle构建脚本中使用Scala(或java)方法   java Android Mediaplayer下一步按钮不起作用   Java Sound API在播放音频文件后将其锁定   java将变量从外部类传递到内部类的最佳方法   使用play framework的博客web应用程序出现java逻辑错误   java我们可以在Spring批处理中处理大型zip文件吗?   java如何检查JTable的选定行的特定列中的值是否已经在JList中?