m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0 KiB

6 months ago
  1. """Module with some functions for MathML, like transforming MathML
  2. content in MathML presentation.
  3. To use this module, you will need lxml.
  4. """
  5. from sympy.utilities.pkgdata import get_resource
  6. from sympy.utilities.decorator import doctest_depends_on
  7. __doctest_requires__ = {('apply_xsl', 'c2p'): ['lxml']}
  8. def add_mathml_headers(s):
  9. return """<math xmlns:mml="http://www.w3.org/1998/Math/MathML"
  10. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  11. xsi:schemaLocation="http://www.w3.org/1998/Math/MathML
  12. http://www.w3.org/Math/XMLSchema/mathml2/mathml2.xsd">""" + s + "</math>"
  13. @doctest_depends_on(modules=('lxml',))
  14. def apply_xsl(mml, xsl):
  15. """Apply a xsl to a MathML string
  16. @param mml: a string with MathML code
  17. @param xsl: a string representing a path to a xsl (xml stylesheet)
  18. file. This file name is relative to the PYTHONPATH
  19. >>> from sympy.utilities.mathml import apply_xsl
  20. >>> xsl = 'mathml/data/simple_mmlctop.xsl'
  21. >>> mml = '<apply> <plus/> <ci>a</ci> <ci>b</ci> </apply>'
  22. >>> res = apply_xsl(mml,xsl)
  23. >>> ''.join(res.splitlines())
  24. '<?xml version="1.0"?><mrow xmlns="http://www.w3.org/1998/Math/MathML"> <mi>a</mi> <mo> + </mo> <mi>b</mi></mrow>'
  25. """
  26. from lxml import etree
  27. s = etree.XML(get_resource(xsl).read())
  28. transform = etree.XSLT(s)
  29. doc = etree.XML(mml)
  30. result = transform(doc)
  31. s = str(result)
  32. return s
  33. @doctest_depends_on(modules=('lxml',))
  34. def c2p(mml, simple=False):
  35. """Transforms a document in MathML content (like the one that sympy produces)
  36. in one document in MathML presentation, more suitable for printing, and more
  37. widely accepted
  38. >>> from sympy.utilities.mathml import c2p
  39. >>> mml = '<apply> <exp/> <cn>2</cn> </apply>'
  40. >>> c2p(mml,simple=True) != c2p(mml,simple=False)
  41. True
  42. """
  43. if not mml.startswith('<math'):
  44. mml = add_mathml_headers(mml)
  45. if simple:
  46. return apply_xsl(mml, 'mathml/data/simple_mmlctop.xsl')
  47. return apply_xsl(mml, 'mathml/data/mmlctop.xsl')