saytex.layers package¶
Submodules¶
saytex.layers.capitalization module¶
Recognizes the keyword “capital” to capitalize the following word.
-
class
saytex.layers.capitalization.
CapitalizationLayer
¶ Bases:
saytex.layers.layer.SaytexLayer
-
execute_layer
(input_string)¶ Transforms “capital a” into “A”, for example.
-
saytex.layers.case_insensitivity module¶
Makes the input treated as case insensitive, which is what it should be if coming from spoken language.
-
class
saytex.layers.case_insensitivity.
CaseInsensitivityLayer
¶ Bases:
saytex.layers.layer.SaytexLayer
-
execute_layer
(input_string)¶ Transforms all capital letters into lowercase letters.
-
saytex.layers.divided_by_recognition module¶
Recognizes the word ‘over’ and transforms it into a properly formatted fraction (fraction begin … end begin … end)
-
class
saytex.layers.divided_by_recognition.
DividedByRecognitionLayer
¶ Bases:
saytex.layers.layer.SaytexLayer
-
execute_layer
(input_string)¶ Transforms “a over b” into “fraction begin a end begin b end”.
-
find_associativity_left
(words, over_index)¶ Finds the index of the start word of the numerator, using some heuristic.
Parameters: - words – list of words
- over_index – index where the word ‘over’ is in the words list
Returns: index i such that words[i:over_index] are the numerator
-
find_associativity_right
(words, over_index)¶ Finds the index of the end word of the denominator, using some heuristic.
Parameters: - words – list of words
- over_index – index where the word ‘over’ is in the words list
Returns: index i such that words[over_index+1:i+1] are the denominator
-
-
saytex.layers.divided_by_recognition.
findmatching
(words, pos, w1='left parenthesis', w2='right parenthesis', forward=True)¶ Find matching parantheses.
Parameters: - words – list, to be searched in
- pos – int, position of ( we want to find a match for
Returns: int, position of matching ) bracket; if there is no match, return len(s)
saytex.layers.from_to_recognition module¶
Recognizes from-to sequences such as “integral from … to …” and “sum from … to …”.
-
class
saytex.layers.from_to_recognition.
FromToRecognitionLayer
¶ Bases:
saytex.layers.layer.SaytexLayer
-
execute_layer
(input_string)¶ Replaces “from … to …” with “subscript begin … end superscript begin .. end”
-
symbolify_fromto
(s)¶ Finds a from-to-sequence and its corresponding sum or integral. We assume that everything between the words “from” and “to” is the from part. We assume that the first simple expression after “to” is the to part. Simple expression is defined as at most two variables and one operation. If parantheses, it uses that. In the case where there is no matching “to” word for a “from” word, we assume that the entirety of the rest of the string is the from part. The return string is formatted as sum subscript begin expression1 end superscript begin expression2 end. (‘sum’ can be substituted for ‘integral’.)
-
-
exception
saytex.layers.from_to_recognition.
NoFromTo
¶ Bases:
Exception
Raised if there is no from-to sequence in a string.
-
saytex.layers.from_to_recognition.
findmatching
(s, pos, w1='left parenthesis', w2='right parenthesis')¶ Find matching parantheses.
Parameters: - s – string, to be searched in
- pos – int, position of ( we want to find a match for
Returns: int, position of matching ) bracket; if there is no match, return len(s)
-
saytex.layers.from_to_recognition.
makeword
(s)¶
saytex.layers.handle_of module¶
Handles the word “of”. It should either be interpreted as a function, say f(x) as “f of x”, or it should be ignored (e.g., “integral of x”)
-
class
saytex.layers.handle_of.
HandleOfLayer
¶ Bases:
saytex.layers.layer.SaytexLayer
-
execute_layer
(input_string)¶ Handles the word “of” appropriately.
-
saytex.layers.layer module¶
Defines the base layer interface to be used by all layers.
-
exception
saytex.layers.layer.
InvalidLayerAccess
¶ Bases:
Exception
Raised in SaytexLayer.get_layer if someone tries to access an invalid layer.
-
class
saytex.layers.layer.
SaytexLayer
¶ Bases:
object
-
execute_layer
(input_string)¶ To be overridden in a layer subclass.
Parameters: input_string – str, the input Returns: str, the output of the layer
-
replace_words
(word_tuples, input_string)¶ Replaces words in input_string using the word_tuples.
Parameters: - word_tuples – a list of tuples of two strings, where the first one is the word to be replaced and the second one is the word to replace it with
- input_string – str
Returns: str, with words replaced
-
saytex_syntax_operators
()¶ Returns a set of all recognized operators in SayTeX Syntax.
-
saytex.layers.math_symbols_transform module¶
Transforms symbols such as ‘+’ and ‘*’ into the words ‘plus’ and ‘times’
-
class
saytex.layers.math_symbols_transform.
MathSymbolsTransformLayer
¶ Bases:
saytex.layers.layer.SaytexLayer
-
SYMBOLS
= {'*': 'times', '+': 'plus', '-': 'minus', 'π': 'pi'}¶
-
execute_layer
(input_string)¶ Transforms all symbols into words for the symbols.
-
saytex.layers.prettification module¶
Does various things to produce prettier LaTeX strings, such as inserting spaces into integrals.
-
class
saytex.layers.prettification.
PrettificationLayer
¶ Bases:
saytex.layers.layer.SaytexLayer
-
execute_layer
(input_string)¶ Adds spaces to integrals to produce prettier equations.
-
saytex.layers.speech_recognition_error_correction module¶
Corrects errors in the speech recognition software used. Currently specific to the Microsoft speech engine, and might not produce good results if used with other speech recognition software.
-
class
saytex.layers.speech_recognition_error_correction.
SpeechRecognitionErrorCorrectionLayer
¶ Bases:
saytex.layers.layer.SaytexLayer
-
aggressive
= True¶
-
common_mischaracterizations
= [('see', 'c'), ('hey', 'a'), ('day', 'a'), ('overbyte', 'over b'), ('richer', 'greater'), ('clothes', 'close'), ('whole', 'close'), ('closed', 'close'), ('cosign', 'cosine'), ('quotes', 'close'), ('oakland', 'open'), ('eggs', 'x'), ('clubs', 'close'), ('eclipse', 'a plus'), ('aid', 'a'), ('beat', 'b'), ('nfinity', 'infinity'), ('menace', 'minus'), ('age', 'h'), ('why', 'y'), ('acts', 'x'), ('and', 'end')]¶
-
common_mischaracterizations_aggressive
= [('some', 'sum'), ('be', 'b'), ("I'm", 'n'), ('sign', 'sine')]¶
-
execute_layer
(input_string)¶ Replaces the mischaracterizations with guesses for what the user actually said. Not doing anything smart really, only using word replacements. Could easily be modified to be smarter.
-
saytex.layers.spoken_number_recognition module¶
Recognizes spoken numbers and transforms them into number literals.
-
class
saytex.layers.spoken_number_recognition.
SpokenNumberRecognitionLayer
¶ Bases:
saytex.layers.layer.SaytexLayer
-
execute_layer
(input_string)¶ Transforms spoken numbers into number literals. For example, “five hundred and five” is transformed into “505”.
-
saytex.layers.synonym_standardization module¶
Maps common ways of expressing math formulas into the specific SayTeX Syntax way of saying things.
-
exception
saytex.layers.synonym_standardization.
InvalidSynonymStandardizationDictionary
¶ Bases:
Exception
-
class
saytex.layers.synonym_standardization.
SynonymStandardizationDictionary
(syntax_file=None, syntax_directory=None)¶ Bases:
object
Works as an interface for the synonym standardization dictionary.
-
get_standard_synonym
(word, 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_syntax_entry
(word)¶ 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.
-
-
class
saytex.layers.synonym_standardization.
SynonymStandardizationLayer
¶ Bases:
saytex.layers.layer.SaytexLayer
-
convert_synonyms
(saytex_string, word_list=None, word_index=0, dp_memo=None)¶ Converts SayTeX Syntax into LaTeX code.
Parameters: - saytex_string – A string containing valid SayTeX Syntax code.
- (optional) (word_list) – A list of tokenized words derived from saytex_string. If word_list is not None, then the saytex_string will be ignored. Is normally only used for recursive calls.
- word_index – The current index in the word_list that we are at. Used in recursive calls. If it is not valid SayTeX Syntax, a SaytexSyntaxError will be raised.
- 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 string containing a valid translation of the input string into LaTeX (if no exception). If word_index > 0, the return value is a tuple (string, value) where value is a measure of how good the string is as a LaTeX translation of saytex_string.
-
execute_layer
(input_string)¶ Converts common synonyms into SayTeX Syntax equivalents.
-
-
exception
saytex.layers.synonym_standardization.
UnrecognizedSynonym
¶ Bases:
Exception
Module contents¶
Defines all layers, used for converting natural language into SayTeX Syntax. All layers should subclass the layer interface defined in layers.py.