FST mutation

Mutable FSTs can be modified by adding states (add_state), adding arcs leaving existing states (add_arc), marking a existing state as the start state (set_start), or giving a non-infinite final weight to an existing state (set_final). Optionally, the user can reserve states before adding them using the reserve_states instance method, and reserve arcs leaving an existing state using the reserve_arcs method. The following snippet creates an acceptor which, when its arc labels are interpreted as bytes, accepts the well-known "sheep language" /baa+/.

While it is possible to add arcs whose destination state has not yet been added, any other references to states not yet created (by add_state) is forbidden and will raise an FstIndexError.

Existing arcs and states can also be deleted using delete_states, and arcs leaving an existing state can be deleted using delete_arcs. For example, the following function can be used to remove all arcs and states from an FST.

FSTs which are mutable (e.g., vector FSTs) also support destructive operations such as arc-sorting (arcsort), inversion (invert), projection (project), and union (union). These operations work in place, mutating the instance they are called on and returning nothing. These instance methods are not available for immutable FST types (e.g., const FSTs).

>>> v.arcsort(sort_type="olabel")
>>> v.invert()
>>> v.project()

A few operations (e.g., weight-pushing, epsilon-removal) are available in both constructive and destructive forms, albeit with slightly different options.

To read documentation on individual FST operations, use Python's built-in help function.

>>> help(fst.equal)
Help on built-in function equal in module pywrapfst:
equal(...)
equal(ifst1, ifst2, delta=fst.kDelta)
Are two FSTs equal?
This function tests whether two FSTS have the same states with the same
numbering and the same transitions with the same labels and weights in the
same order.
Args:
ifst1: The first input FST.
ifst2: The second input FST.
delta: Comparison/quantization delta.
Returns:
True if the two FSTs satisfy the above conditions, otherwise False.
See also: `equivalent`, `isomorphic`, `randequivalent`.

FST output

FSTs can be written to disk using the write instance method.

>>> f.write("f.fst")

They also can be written into FARs using the FarWriter object. Once created, an FST can be written to the FarWriter object using dictionary-style assignment.

Note that the FAR itself is not guaranteed to be flushed to disk until the FarWriter is garbage-collected. Under normal circumstances, calling del on the FarWriter variable will decrement the reference count to zero and trigger garbage collection on the next cycle.

>>> del writer

Worked example

Putting it all together, the following example, based on Mohri et al. 2002, 2008, shows the construction of an ASR recognition transducer from a pronunciation lexicon L, grammar G, a transducer from context-dependent phones to context-independent phones C, and an HMM set H (where we assume that the components are all determinizable and, preferably, in the log semiring).