saytex package

Submodules

saytex.compiler module

Defines the Saytex class, containing methods for converting between natural language and LaTeX. The conversion is done in a two-step process by first translating the input into SayTeX Syntax, after which the saytexsyntax module is used for the final LaTeX conversion.

class saytex.compiler.Saytex

Bases: object

Contains the method to_latex to convert from natural language to LaTeX. It will do this by invoking the layers defined in config.py in the specified order, followed by a final call to SaytexSyntax. The method to_saytex will do the same thing, without the call to SaytexSyntax.

add_layer(layer_class, layer_priority)

Adds a layer to the natural language -> Saytex Syntax conversion process.

Parameters:
  • layer_class – The class of the layer, which must be a subclass of saytex.layers.layer.SaytexLayer. Note that this parameter is not a string, but rather the class itself.
  • layer_priority – The priority of the layer, represented as a number. The priority affects in which order the layers are executed, and a lower number means it is executed sooner. The priorities only make sense in relation to other priorities, and the default priorities can be found in saytex.config.
Returns:

None

get_layer_priorities()

Returns a dictionary mapping the currently used layers to their priorities.

Returns:Dictionary of layers to numbers.
get_layers()

Returns the set of currently used layers.

Returns:Set of layers.
remove_layer(layer_class)

Removes a layer from the conversion process. Can be used to remove default layers.

Parameters:layer_class – The class of the layer, which must be a subclass of saytex.layers.layer.SaytexLayer. Note that this parameter is not a string, but rather the class itself.
Returns:None
to_latex(math_string)

Converts natural language into LaTeX code.

Parameters:math_string – A string containing a spoken math expression. The string must be recognizable to SayTeX, of which the specifics depend on the particular layers that are used. The set of all recognizable SayTeX strings is a superset of SayTeX Syntax.
Returns:A string containing a valid translation of the input string to LaTeX code. If the string is not recognizable (that is, it cannot be converted into SayTeX), the UnrecognizableSaytexInput exception is thrown.
to_saytex(math_string)

Converts natural language into SayTeX Syntax.

Parameters:math_string – A string containing a spoken math expression. The string should use the SayTeX+ format, which is a superset of SayTeX Syntax.
Returns:A string containing a valid translation of the input string to SayTeX Syntax. If the string is not recognizable (that is, it cannot be converted into SayTeX), the UnrecognizableSaytexInput exception is thrown.
exception saytex.compiler.UnrecognizableSaytexInput

Bases: Exception

Raised in to_latex and to_saytex, if the input cannot be transformed into valid SayTeX Syntax.

saytex.config module

This configuration file contains the default layers as well as the default layer priorities. Changing it will change the default for all Saytex instances. To change the layers for a specific Saytex instance, look into the documentation on add_layer and remove_layer of Saytex.

saytex.config.default_layer_priorities = {<class 'saytex.layers.speech_recognition_error_correction.SpeechRecognitionErrorCorrectionLayer'>: 0, <class 'saytex.layers.case_insensitivity.CaseInsensitivityLayer'>: 0, <class 'saytex.layers.math_symbols_transform.MathSymbolsTransformLayer'>: 0, <class 'saytex.layers.capitalization.CapitalizationLayer'>: 1, <class 'saytex.layers.spoken_number_recognition.SpokenNumberRecognitionLayer'>: 1, <class 'saytex.layers.synonym_standardization.SynonymStandardizationLayer'>: 1, <class 'saytex.layers.handle_of.HandleOfLayer'>: 2, <class 'saytex.layers.from_to_recognition.FromToRecognitionLayer'>: 3, <class 'saytex.layers.divided_by_recognition.DividedByRecognitionLayer'>: 3, <class 'saytex.layers.prettification.PrettificationLayer'>: 4}

The layer_priorities is a dictionary mapping layer classes to a number, reflecting the in which order the layers should be executed. Layers are executed from low to high priority. Layers with the same priority can be executed in any order; the idea is that such layers are independent of each other. In designing new layers, one should strive for not adding a new priority level.

saytex.config.default_layers = {<class 'saytex.layers.speech_recognition_error_correction.SpeechRecognitionErrorCorrectionLayer'>, <class 'saytex.layers.spoken_number_recognition.SpokenNumberRecognitionLayer'>, <class 'saytex.layers.case_insensitivity.CaseInsensitivityLayer'>, <class 'saytex.layers.handle_of.HandleOfLayer'>, <class 'saytex.layers.prettification.PrettificationLayer'>, <class 'saytex.layers.from_to_recognition.FromToRecognitionLayer'>, <class 'saytex.layers.divided_by_recognition.DividedByRecognitionLayer'>, <class 'saytex.layers.math_symbols_transform.MathSymbolsTransformLayer'>, <class 'saytex.layers.synonym_standardization.SynonymStandardizationLayer'>, <class 'saytex.layers.capitalization.CapitalizationLayer'>}

The used_layers is a set containing the default layers to be used by Saytex when converting natural language into SayTeX Syntax. Changing the layers that are in use will affect what spoken math expressions that SayTeX can recognize. Each layer must be a subclass of saytex.layers.layer.SaytexLayer. Creating new layers is as simple as creating a new subclass of saytex.layers.layer.SaytexLayer, and then adding it to the default_layers and default_layer_priorities, or just adding it on a case-by-case situation to a Saytex instance.

Module contents

The saytex package mainly consists of the Saytex class, used for converting from natural language to LaTeX, as well as the SaytexSyntax class, used for converting from the intermediary SayTeX Syntax into LaTeX.