Package: pyfoma
PyFomaPython Finite-State ToolkitPyFomais a an open source (Apache) package for finite-state automaton and transducer modeling and learning. It is implemented entirely in Python with no external dependencies.PyFomasupports:Compiling both weighted and unweighted automataand transducers (FSMs) from Perl/Python-like regular expressions.All the standard weighted and unweighted automata algorithms: epsilon-removal, determinization, minimization, composition, shortest-path algorithms, extraction of strongly connected components, (co)accessibility, etc.Weights in thetropical semiringfor automata and transducer construction both for low-level construction methods and regular expressions.Integration with Jupyter-style notebooks for automata visualization and debugging.Custom extensions to the regular expression parser and compiler using Python.Compilation of morphological lexiconsas weighted or unweighted right-linear grammars, similarly to thelexc-formalism.A comprehensive replacement-rule formalism to construct string-rewriting transducers.The PyFoma implementation aims at a level of abstraction where most major finite-state algorithms are implemented clearly and asymptotically optimally in what resembles canonical pseudocode, so the code itself could be used for instructional purposes. Additionally, many algorithms can illustrate how they work. The regular expression and right-linear grammar formalisms are intended to be accessible to linguists and computer scientists alike.HistoryAs a tool, PyFoma is unrelated to thefomacompiler, which is implemented in C and uses the Xerox formalism for regular expressions and which has its own Python extensions, but it inherits many of its FSM construction algorithms. The regular expression formalism is influenced byThe Kleene Programming Language.QuickstartBuild weighted and unweighted automata and transducers from regular expressions:frompyfomaimportFSTmyfst=FST.re("(cl|n|s)?e(ve|a)r")myfst.view()... or through low-level specificationfrompyfomaimportFST,Statemyfst=FST()# Init objects0=myfst.initialstate# FST() always has one state, make that s0s1=State()# Add a states0.add_transition(s1,("a","x"),1.0)# Add transitions...s0.add_transition(s1,("a","y"),2.0)s1.add_transition(s0,("a","a"),0.0)s1.finalweight=2.0# Set the final weightmyfst.states={s0,s1}# Set of statesmyfst.finalstates={s1}# Set of final statesmyfst.alphabet={"a","x","y"}# Optional alphabetmyfst.view()list(myfst.generate("aaa",weights=True))... or using avisual toolAccess basic algorithms such as determinization, minimization, weight pushing, shortest path, etc.
Every algorithm has amutatingform and anon-mutatingform.frompyfoma.algorithmsimportpushed_weights# Only needed for non-mutating algfst=pushed_weights(fst)# Non-mutating# ===== OR =====fst.push_weights()# Mutatingfst.view()print(fst)# Also print in AT&T string formatConstruct models of phonology and morphophonology with replacement rule transducers:nasal=FST.re("[mnŋ]")# Define nasals for reuse in rulenasalization=FST.re("$^rewrite(a:'ã'|e:'ẽ'|i:'ĩ'|o:'õ'|u:'ũ' / _ $nasal)",{'nasal':nasal})nasalization.view()list(nasalization.generate("foma"))# Pass word through transducer, returns generatorDocumentationRegular expression compilerMorphological Analyzer TutorialAlgorithms overviewExample code:The Soundex Algorithm