|
| 1 | +# MathCAT add-on: generates speech, braille, and allows exploration of expressions written in MathML |
| 2 | +# The goal of this add-on is to replicate/improve on the functionality of MathPlayer which has been discontinued. |
| 3 | +# Author: Neil Soiffer |
| 4 | +# Copyright: this file uses whatever GNU copyright that is required for NVDA addons |
| 5 | +# The code additionally makes use of the MathCAT library (written in Rust) which is covered by the MIT license |
| 6 | +# and also (obviously) requires external speech engines and braille drivers. |
| 7 | + |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +# import shutil |
| 12 | +# if os.path.exists("libmathcat_py.pyd"): |
| 13 | +# os.remove("libmathcat_py.pyd") |
| 14 | +# shutil.copy("..\\target\\i686-pc-windows-msvc\\release\\libmathcat_py.dll", "libmathcat.pyd") |
| 15 | + |
| 16 | +import libmathcat |
| 17 | + |
| 18 | +def SetMathCATPreferences(): |
| 19 | + try: |
| 20 | + libmathcat.SetRulesDir( |
| 21 | + # this assumes the Rules dir is in the same dir a the library. Modify as needed |
| 22 | + os.path.join( os.path.dirname(os.path.abspath(__file__)), "Rules") |
| 23 | + ) |
| 24 | + except Exception as e: |
| 25 | + print("problem with finding the MathCAT rules") |
| 26 | + |
| 27 | + try: |
| 28 | + libmathcat.SetPreference("TTS", "none") |
| 29 | + libmathcat.SetPreference("Language", "en") # Also "id" and "vi" |
| 30 | + libmathcat.SetPreference("SpeechStyle", "SimpleSpeak") # Also "ClearSpeak" |
| 31 | + libmathcat.SetPreference("Verbosity", "verbose") # also terse "terse"/"medium" |
| 32 | + libmathcat.SetPreference("CapitalLetters_UseWord", "false") # if "true", X => "cap x" |
| 33 | + except Exception as e: |
| 34 | + print("problem with setting a preference") |
| 35 | + |
| 36 | +def SetMathMLForMathCAT(mathml: str): |
| 37 | + try: |
| 38 | + libmathcat.SetMathML(mathml) |
| 39 | + except Exception as e: |
| 40 | + print("problem with SetMathML") |
| 41 | + |
| 42 | +def GetSpeech(): |
| 43 | + try: |
| 44 | + return libmathcat.GetSpokenText() |
| 45 | + except Exception as e: |
| 46 | + return "problem with getting speech for MathML" |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +SetMathCATPreferences() # you only need to this once |
| 51 | + |
| 52 | +mathml = "<math><mfrac> <mn>1</mn> <mi>X</mi> </mfrac> </math>" |
| 53 | +SetMathMLForMathCAT(mathml) |
| 54 | +print("MathML: {}\nSpeech: '{}'".format(mathml, GetSpeech())) |
| 55 | + |
| 56 | +mathml = "<math><msup> <mi>x</mi> <mn>3</mn> </msup> </math>" |
| 57 | +SetMathMLForMathCAT(mathml) |
| 58 | +print("MathML: {}\nSpeech: '{}'".format(mathml, GetSpeech())) |
0 commit comments