Skip to main content

Python Finite-State Toolkit

Project description

PyFoma

Python Finite-State Toolkit

PyFoma is 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.

PyFoma supports:

  • Compiling both weighted and unweighted automata and 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 the tropical semiring for 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 lexicons as weighted or unweighted right-linear grammars, similarly to the lexc-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.


History

As a tool, PyFoma is unrelated to the foma compiler, 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 by The Kleene Programming Language.


Quickstart

  • Build weighted and unweighted automata and transducers from regular expressions:
from pyfoma import FST
myfst = FST.re("(cl|n|s)?e(ve|a)r")
myfst.view()
  • ... or through low-level specification
from pyfoma import FST, State

myfst = FST()            # Init object
s0 = myfst.initialstate  # FST() always has one state, make that s0
s1 = State()             # Add a state
s0.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 weight
myfst.states = {s0,s1}                 # Set of states
myfst.finalstates = {s1}               # Set of final states
myfst.alphabet = {"a","x","y"}         # Optional alphabet
myfst.view()
list(myfst.generate("aaa", weights = True))
  • Access basic algorithms such as determinization, minimization, weight pushing, shortest path, etc. Every algorithm has a mutating form and a non-mutating form.
from pyfoma.algorithms import pushed_weights # Only needed for non-mutating alg

fst = pushed_weights(fst) # Non-mutating
# ===== OR =====
fst.push_weights() # Mutating

fst.view()
print(fst)    # Also print in AT&T string format
  • Construct models of phonology and morphophonology with replacement rule transducers:
nasal = FST.re("[mnŋ]")  # Define nasals for reuse in rule
nasalization = FST.re("$^rewrite(a:'ã'|e:'ẽ'|i:'ĩ'|o:'õ'|u:'ũ' / _ $nasal)", {'nasal': nasal})
nasalization.view()
list(nasalization.generate("foma"))  # Pass word through transducer, returns generator

Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfoma-1.0.6.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

pyfoma-1.0.6-py3-none-any.whl (37.8 kB view details)

Uploaded Python 3

File details

Details for the file pyfoma-1.0.6.tar.gz.

File metadata

  • Download URL: pyfoma-1.0.6.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.13

File hashes

Hashes for pyfoma-1.0.6.tar.gz
Algorithm Hash digest
SHA256 f4eb58e92e909b89b5e09bffdb9de63af763e6a87b1ae3e08c568c042fee708f
MD5 3e99dea9dfae754d85d8aacf219d8107
BLAKE2b-256 b9cbdb872741f507e6a8547d7ba99340584b351e21d59e567c7aceaa6ce50cad

See more details on using hashes here.

File details

Details for the file pyfoma-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: pyfoma-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 37.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.13

File hashes

Hashes for pyfoma-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 caa8d06796628d23687dec5e2cf11706682b5d70a1e5484f660c21e0c1b84f70
MD5 7a99cf2e8a6eab05c9a257102eb77276
BLAKE2b-256 1d09f261857673fe7a8d3ef7afbd2ec1b17c345a77e1e5602a798c0132d43989

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page