saytex.saytexsyntax package

Submodules

saytex.saytexsyntax.compiler module

Defines the SaytexSyntax class, containing methods for converting between SayTeX and LaTeX.

exception saytex.saytexsyntax.compiler.LatexParsingError

Bases: Exception

Raised in from_latex, if error.

exception saytex.saytexsyntax.compiler.MultipleSaytexInterpretations

Bases: Exception

Raised in to_latex, if error.

class saytex.saytexsyntax.compiler.SaytexSyntax

Bases: object

Contains methods to_latex and from_latex for converting between LaTeX and SayTeX syntax. Also supports reloading of the syntax dictionary.

compute_latex(word_list, word_index, dp_memo, next_params)

Converts the string formed by the words word_list[word_index:] into LaTeX, and returns a score of how good the conversion is. The score is proportional to the number of non-keywords used in the conversion, and a lower score is better. Assumes that the string formed by word_list is valid SayTeX Syntax.

Parameters:
  • word_list – A list of tokenized words derived from saytex_string.
  • word_index – The current index in the word_list that we are at.
  • dp_memo – A dictionary mapping indices in the word_list to generated LaTeX strings.
  • next_params – A dictionary of parameters to be passed to the next get_latex call.
Returns:

A tuple (string, value) where value is a measure of how good the string is as a LaTeX translation of saytex_string.

is_valid_saytex_syntax(potential_saytex_string)

Determines if a string is valid SayTeX Syntax or not.

Parameters:potential_saytex_string – str, potentially conforming to SayTeX syntax
Returns:bool, indicating whether the str actually conforms to SayTeX syntax or not
load_syntax_dictionary(syntax_directory)

Load the syntax dictionary as self.syntax_dictionary.

to_latex(saytex_string)

Converts SayTeX Syntax into LaTeX code.

Parameters:saytex_string – A string containing valid SayTeX Syntax code.
Returns:A string containing a valid translation of the input string into LaTeX (if no exception).
exception saytex.saytexsyntax.compiler.SaytexSyntaxError

Bases: Exception

Raised in to_latex, if error.

saytex.saytexsyntax.config module

saytex.saytexsyntax.config.ALLOWED_CHARACTERS_IN_SAYTEX_WORD = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}

This constant should correspond to the most recent definition of the SayTeX Syntax. Note that the specification allows for both words and numbers, the latter of which are not restricted to the characters below.

saytex.saytexsyntax.config.MAX_WORDS_PER_SAYTEX_COMMAND = 7

This constant should correspond to the most recent definition of the SayTeX Syntax. Note that the total time of the SayTeX Syntax Compiler will be O(n*m) if m is the variable below, so be careful. (Also, it will be O(n^2).)

saytex.saytexsyntax.config.SYNTAX_DIRECTORY = 'saytex_dictionary'

The path to the directory containing all syntax files. The specifications for the files can be found in saytex-syntax-v01.md.

saytex.saytexsyntax.syntax_dictionary module

Defines a SyntaxDictionary class which handles reading from a SayTeX Syntax file.

exception saytex.saytexsyntax.syntax_dictionary.InvalidSyntaxFile

Bases: Exception

Raised if the syntax json file is not of valid format.

class saytex.saytexsyntax.syntax_dictionary.SyntaxDictionary(syntax_file=None, syntax_directory=None)

Bases: object

Represents a Syntax Dictionary.

get_latex(saytex, params={})

Returns the corresponding latex code for saytex, and None if saytex is an invalid command. The latex code will be space padded according to the value in the dictionary.

Parameters:
  • saytex – str, containing a potential saytex command
  • params – dict, containing params to be passed to post_process_latex
Returns:

a post processed str, or None if saytex is not a valid command

get_next_params(saytex)

Returns a (possibly empty) dictionary containing the params that should be passed to the next call of post_process_latex

Parameters:saytex – str, containing a potential saytex command
get_syntax_entry(saytex)

Returns a syntax entry corresponding to saytex, if exists. Otherwise, raises KeyError.

Parameters:saytex – str, containing a potential saytex command
Returns:a dictionary in self.syntax_list
load_syntax()

Loads the syntax defined in self.syntax_file or self.syntax_directory into the array self.syntax_list and the dictionary self.syntax_dictionary.

make_syntax_entry_default(syntax_item)

Modifies the supplied syntax entry d, to add default values for parameters.

Parameters:syntax_item – A dictionary representing a syntax entry to be modified in place.
post_process_latex(syntax_entry, insert_curly_brackets=False)

Returns the post processed version of the syntax_entry, by adding spacing or curly brackets.

Parameters:
  • syntax_entry – a dictionary that could be a member of syntax_list
  • insert_curly_brackets – bool, indicating whether the latex command should be curly bracket padded instead of space padded
Returns:

str, the post processed version

exception saytex.saytexsyntax.syntax_dictionary.UnrecognizedSaytexCommand

Bases: Exception

Module contents

The saytexsyntax package provides the SaytexSyntax class, which can be used for converting between SayTeX Syntax and LaTeX.