Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
OpenFst Python extension | ||||||||
Line: 119 to 119 | ||||||||
>>> f.set_start(s) >>> n = f.add_state() >>> f.reserve_arcs(s, 1) # Optional. | ||||||||
Changed: | ||||||||
< < | >>> f.add_arc(s, 98, 98, fst.Weight.One(f.weight_type), n) | |||||||
> > | >>> f.add_arc(s, fst.Arc(98, 98, fst.Weight.One(f.weight_type), n)) | |||||||
>>> s = n >>> n = f.add_state() >>> f.reserve_arcs(s, 1) # Optional. | ||||||||
Changed: | ||||||||
< < | >>> f.add_arc(s, 97, 97, fst.Weight.One(f.weight_type), n) | |||||||
> > | >>> f.add_arc(s, fst.Arc(97, 97, fst.Weight.One(f.weight_type), n)) | |||||||
>>> s = n >>> n = f.add_state() >>> f.reserve_arcs(s, 1) # Optional. | ||||||||
Changed: | ||||||||
< < | >>> f.add_arc(s, 97, 97, fst.Weight.One(f.weight_type), n) | |||||||
> > | >>> f.add_arc(s, fst.Arc(97, 97, fst.Weight.One(f.weight_type), n)) | |||||||
>>> f.reserve_arcs(n, 1) # Optional. | ||||||||
Changed: | ||||||||
< < | >>> f.add_arc(n, 97, 97, fst.Weight.One(f.weight_type), n) | |||||||
> > | >>> f.add_arc(n, fst.Arc(97, 97, fst.Weight.One(f.weight_type), n)) | |||||||
>>> f.set_final(n, fst.Weight.One(f.weight_type)) >>> f.verify() # Checks FST's sanity. True |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
OpenFst Python extension | ||||||||
Line: 219 to 219 | ||||||||
Worked examplePutting 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). | ||||||||
Changed: | ||||||||
< < | >>> reader = fst.FarReader("hclg.far") | |||||||
> > | >>> reader = fst.FarReader.open("hclg.far") | |||||||
>>> LG = fst.determinize(fst.compose(reader["L"], reader["G"])) >>> CLG = fst.determinize(fst.compose(reader["C"], LG)) >>> HCLG = fst.determinize(fst.compose(reader["H"], CLG)) |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
Changed: | ||||||||
< < | OpenFst Python extension | |||||||
> > | OpenFst Python extension | |||||||
Changed: | ||||||||
< < | This describes the OpenFst 1.5.1 beta API.
This extension exposes nearly all of the OpenFst scripting interface in Python. Like the scripting interface, it supports arbitrary arcs and weights. The extension allows for rapid prototyping and interactive construction of FSTs using the Python REPL. | |||||||
> > | This extension exposes the OpenFst scripting API to Python. Like the scripting API, it supports arbitrary arcs and weights. The extension allows for rapid prototyping and interactive construction of FSTs using the Python REPL. | |||||||
Note that this extension is unrelated to, and incompatible with, any other third-party Python extensions for OpenFst (e.g., pyfst).
To install this package, either:
| ||||||||
Changed: | ||||||||
< < | ||||||||
> > | ||||||||
NB: >>> indicates the Python interactive prompt; all other typewriter-text lines are print to stdout or stderr.
Module import |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
OpenFst Python extension | ||||||||
Line: 83 to 83 | ||||||||
| ||||||||
Changed: | ||||||||
< < | Mutable FSTs also provide the num_states attribute, which indicates the number of states in the FST. | |||||||
> > | Mutable FSTs also provide the num_states attribute, which indicates the number of states in the FST. | |||||||
To access FST properties (i.e., cyclicity, weightedness), use the properties method.
>>> print "Is f cyclic?", f.properties(fst.CYCLIC, True) == fst.CYCLIC |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
OpenFst Python extension | ||||||||
Line: 26 to 26 | ||||||||
>>> print >> compiler, "0 1 98 121 1.5" >>> print >> compiler, "1 2 99 123 2.5" >>> print >> compiler, "2 3.5" | ||||||||
Changed: | ||||||||
< < | >>> f = compiler.compile() | |||||||
> > | >>> f = compiler.compile() # Creates the FST and flushes the compiler buffer. >>> f.num_states 3 | |||||||
>>> f.final(2)
FSTs can be read in from disk using | ||||||||
Changed: | ||||||||
< < | >>> f = fst.Fst.read("vector.fst") | |||||||
> > | >>> v = fst.Fst.read("vector.fst") | |||||||
This class method takes an optional second argument, a string indicating the desired FST type. The FST is converted to this type if it the on-disk FST is not already of the desired type.
>>> c = fst.Fst.read("const.fst") | ||||||||
Line: 85 to 87 | ||||||||
To access FST properties (i.e., cyclicity, weightedness), use the properties method.
>>> print "Is f cyclic?", f.properties(fst.CYCLIC, True) == fst.CYCLIC | ||||||||
Changed: | ||||||||
< < | True | |||||||
> > | Is f cyclic? True | |||||||
FST access and iteration
FST arcs and states can be accessed via the | ||||||||
Line: 105 to 107 | ||||||||
The following function can be used to count the number of arcs and states in an FST. | ||||||||
Changed: | ||||||||
< < | def num_arcs_and_states(f): return sum(1 + f.num_arcs(s) for s in f.states()) | |||||||
> > | >>> def num_arcs_and_states(f): ... return sum(1 + f.num_arcs(s) for s in f.states()) | |||||||
FST mutation |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
OpenFst Python extension | ||||||||
Line: 6 to 6 | ||||||||
This extension exposes nearly all of the OpenFst scripting interface in Python. Like the scripting interface, it supports arbitrary arcs and weights. The extension allows for rapid prototyping and interactive construction of FSTs using the Python REPL. | ||||||||
Changed: | ||||||||
< < | Note that this extension is unrelated to, and incompatible with, any other third-party Python extensions for OpenFst (e.g., pyfst). Install should not install this extension in the same Python environment as any third-party extensions. | |||||||
> > | Note that this extension is unrelated to, and incompatible with, any other third-party Python extensions for OpenFst (e.g., pyfst). | |||||||
To install this package, either:
| ||||||||
Changed: | ||||||||
< < | ||||||||
> > | ||||||||
NB: >>> indicates the Python interactive prompt; all other typewriter-text lines are print to stdout or stderr.
Module import | ||||||||
Line: 38 to 38 | ||||||||
>>> c.fst_type 'const' >>> v = fst.Fst.read("const.fst", fst_type="vector") | ||||||||
Added: | ||||||||
> > | >>> v.fst_type | |||||||
'vector'
This conversion can also be accomplished after instantiation using the | ||||||||
Line: 84 to 85 | ||||||||
To access FST properties (i.e., cyclicity, weightedness), use the properties method.
>>> print "Is f cyclic?", f.properties(fst.CYCLIC, True) == fst.CYCLIC | ||||||||
Changed: | ||||||||
< < | True
>>> print "Does f have weighted cycles?", ... printf.properties(fst.WEIGHTED_CYCLES, True) == fst.WEIGHTED_CYCLES False | |||||||
> > | True | |||||||
FST access and iteration
FST arcs and states can be accessed via the | ||||||||
Line: 171 to 169 | ||||||||
FST operations
All FSTs support constructive operations such as composition ( | ||||||||
Changed: | ||||||||
< < | >>> cv = fst.compose(c, v) >>> print cv <vector Fst at 0x7f1e45890190> | |||||||
> > | >>> cv = fst.compose(c, v) | |||||||
FSTs also support tests for equality (equal ), equivalence (equivalent ), stochastic equivalence (randequivalent ), and isomorphism (isomorphic ). | ||||||||
Changed: | ||||||||
< < | >>> print fst.isomorphic(c, v) | |||||||
> > | >>> fst.isomorphic(c, v) | |||||||
True
FSTs which are mutable (e.g., | ||||||||
Line: 213 to 209 | ||||||||
FSTs can be written to disk using the write instance method.
>>> f.write("f.fst") | ||||||||
Changed: | ||||||||
< < | They also can be written into FARs using the FarWriter object. This takes a filename argument, and optionally, specifications of the arc type and FAR type. Once created, an FST can be written to the FarWriter object using dictionary-style assignment. | |||||||
> > | 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. | |||||||
>>> writer = fst.FarWriter.create("lattice.far") >>> writer["1best"] = 1best >>> writer["2best] = 2best |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
OpenFst Python extension | ||||||||
Line: 175 to 175 | ||||||||
>>> print cv | ||||||||
Changed: | ||||||||
< < | FSTs also support tests for equality (equal ), equivalence (equivalent ), [[RandEquivalentDoc][stochastic equivalence] (randequivalent ), and isomorphism (isomorphic ). | |||||||
> > | FSTs also support tests for equality (equal ), equivalence (equivalent ), stochastic equivalence (randequivalent ), and isomorphism (isomorphic ). | |||||||
>>> print fst.isomorphic(c, v) True |
Line: 1 to 1 | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||
Changed: | |||||||||||||
< < | Python Extensions to OpenFst | ||||||||||||
> > | OpenFst Python extension | ||||||||||||
Changed: | |||||||||||||
< < | This extension exposes nearly all of the FST script-level interface in Python. Like the script-level interface, it supports arbitrary arcs and weights. | ||||||||||||
> > | This describes the OpenFst 1.5.1 beta API. | ||||||||||||
Changed: | |||||||||||||
< < | Note that this extension is unrelated to, and incompatible with, any other third-party Python extensions for OpenFst (e.g., pyfst ). Install should not install this extension in the same Python environment as any third-party extensions. | ||||||||||||
> > | This extension exposes nearly all of the OpenFst scripting interface in Python. Like the scripting interface, it supports arbitrary arcs and weights. The extension allows for rapid prototyping and interactive construction of FSTs using the Python REPL. | ||||||||||||
Changed: | |||||||||||||
< < | To install this package, issue --enable-python= during configuration. Or, you can install it from PyPi using tools like pip or easy_install . | ||||||||||||
> > | Note that this extension is unrelated to, and incompatible with, any other third-party Python extensions for OpenFst (e.g., pyfst). Install should not install this extension in the same Python environment as any third-party extensions. | ||||||||||||
Changed: | |||||||||||||
< < | Users may use this extension to rapidly prototype or construct FSTs interactively using the Python REPL. Note that >>> indicates the Python interactive prompt; all other typewriter-text lines are print to stdout or stderr . | ||||||||||||
> > | To install this package, either:
>>> indicates the Python interactive prompt; all other typewriter-text lines are print to stdout or stderr.
Module import
The Python module itself is called
>>> import pywrapfst as fst FST construction
FSTs can be compiled from arc-lists in the same format used by the >>> compiler = fst.Compiler() >>> print >> compiler, "0 1 97 120 .5" >>> print >> compiler, "0 1 98 121 1.5" >>> print >> compiler, "1 2 99 123 2.5" >>> print >> compiler, "2 3.5" >>> f = compiler.compile() >>> f.final(2) <tropical Weight 3.5 at 0x1215ed0>
FSTs can be read in from disk using >>> f = fst.Fst.read("vector.fst") This class method takes an optional second argument, a string indicating the desired FST type. The FST is converted to this type if it the on-disk FST is not already of the desired type. >>> c = fst.Fst.read("const.fst") >>> c.fst_type 'const' >>> v = fst.Fst.read("const.fst", fst_type="vector") 'vector'
This conversion can also be accomplished after instantiation using the >>> v = fst.convert(c, fst_type="vector") >>> v.fst_type 'vector'
Note that this conversion to the
FSTs can be read in from FST Archives (FARs) using the >>> reader = fst.FarReader.open("lattice.far") Each FST stored within a FAR has a unique string ID which can be used to extract it from the reader object. >>> f = reader["1best"] Or, all FSTs stored within a FAR may be accessed via iteration over the reader object. >>> for (name, f) in reader: ... print name, f.num_states ('1best', 23) ('2best', 27) ('3best', 27) ...
Finally, an empty mutable vector FST can be created using >>> f = fst.Fst()
By default, the resulting FST uses
>>> f.arc_type 'standard' >>> g = fst.Fst("log") >>> g.arc_type 'log' FST object attributes and properties
All FSTs have the following read-only attributes ("properties" in Python jargon):
Mutable FSTs also provide the num_states attribute, which indicates the number of states in the FST. | ||||||||||||
Changed: | |||||||||||||
< < | The Python module itself is called fst .
>>> import fst
FSTs are read in from disk using
>>> f = fst.Fst("vector1.fst") | ||||||||||||
> > | To access FST properties (i.e., cyclicity, weightedness), use the properties method.
>>> print "Is f cyclic?", f.properties(fst.CYCLIC, True) == fst.CYCLIC True >>> print "Does f have weighted cycles?", \ ... printf.properties(fst.WEIGHTED_CYCLES, True) == fst.WEIGHTED_CYCLES False FST access and iteration
FST arcs and states can be accessed via the >>> for state in f.states(): ... for arc in f.arcs(state): ... print state, arc.ilabel, arc.olabel, arc.weight, arc.nextstate 0 97 120 1.5 1 0 98 121 2.5 1 1 99 123 2.5 2
The final weight of a state can be accessed using the >>> for state in f.states(): ... print state, f.final(state) 0 Infinity 1 Infinity 2 3.5 The following function can be used to count the number of arcs and states in an FST.
def num_arcs_and_states(f): return sum(1 + f.num_arcs(s) for s in f.states())
FST mutation
Mutable FSTs can be modified by adding states (
>>> f = fst.Fst() >>> f.reserve_states(3) # Optional. >>> s = f.add_state() >>> f.set_start(s) >>> n = f.add_state() >>> f.reserve_arcs(s, 1) # Optional. >>> f.add_arc(s, 98, 98, fst.Weight.One(f.weight_type), n) >>> s = n >>> n = f.add_state() >>> f.reserve_arcs(s, 1) # Optional. >>> f.add_arc(s, 97, 97, fst.Weight.One(f.weight_type), n) >>> s = n >>> n = f.add_state() >>> f.reserve_arcs(s, 1) # Optional. >>> f.add_arc(s, 97, 97, fst.Weight.One(f.weight_type), n) >>> f.reserve_arcs(n, 1) # Optional. >>> f.add_arc(n, 97, 97, fst.Weight.One(f.weight_type), n) >>> f.set_final(n, fst.Weight.One(f.weight_type)) >>> f.verify() # Checks FST's sanity. True | ||||||||||||
>>> print f | |||||||||||||
Changed: | |||||||||||||
< < | |||||||||||||
> > | 0 1 98 98 1 2 97 97 2 3 97 97 3 3 97 97 3 | ||||||||||||
Changed: | |||||||||||||
< < | fst.Fst takes an optional second argument, a string indicating the desired FST type. The FST is converted to this type if it is not already of the appropriate type.
>>> c = fst.Fst("const1.fst") >>> print c <const Fst at 0x7f1e49aefbe8> >>> v = fst.Fst("const1.fst", fst_type="vector") >>> print v <vector Fst at 0x7f1e45890f10> | ||||||||||||
> > | 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 . | ||||||||||||
Changed: | |||||||||||||
< < | This conversion can also be accomplished after instantiation using fst.convert . | ||||||||||||
> > | 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. | ||||||||||||
Changed: | |||||||||||||
< < | >>> v = fst.convert(c, fst_type="vector") >>> print v <vector Fst at 0x7f1e45890290> | ||||||||||||
> > | >>> def clear(f): ... for state in f.states(): ... f.delete_arcs(state) ... f.delete_states() | ||||||||||||
Changed: | |||||||||||||
< < | All FSTs support constructive operations such as composition (fst.compose ), intersection (fst.intersect ), and reversal (fst.reverse ), and the result is stored in a vector FST. | ||||||||||||
> > | FST visualization | ||||||||||||
Changed: | |||||||||||||
< < | >>> cv = fst.compose(c, v) >>> print cv <vector Fst at 0x7f1e45890190>
All FSTs also support tests for equality (
>>> print fst.isomorphic(c, v) True
Finally, all FSTs can be written to disk using their | ||||||||||||
> > | The instance method text returns a string representing the FST as an arc-list using the same format and options as the fstprint binary. If f is an FST, then print f is an alias for print f.text() . | ||||||||||||
Changed: | |||||||||||||
< < | >>> c.write("const-copy.fst") | ||||||||||||
> > | >>> print f 0 1 98 98 1 2 97 97 2 3 97 97 3 3 97 97 3 | ||||||||||||
Changed: | |||||||||||||
< < | FSTs which are mutable (e.g., vector FSTs) also support destructive operations such as arc-sorting, inversion, and projection. These operations work in place, mutating the instance they are called on, and return nothing. These instance methods are not available for immutable FST types (e.g., const FSTs), and only become available once the FST has been converted to a mutable FST type. | ||||||||||||
> > | FSTs can also be written to a GraphViz file using the draw instance method. | ||||||||||||
Changed: | |||||||||||||
< < | >>> v.arcsort(sort_type="olabel") >>> v.invert() >>> v.project() | ||||||||||||
> > | >>> f.draw("f.gv") | ||||||||||||
Changed: | |||||||||||||
< < | A few operations (e.g., weight-pushing) are available in both constructive and destructive forms, albeit with slightly different options. | ||||||||||||
> > | FST operations | ||||||||||||
Changed: | |||||||||||||
< < | FST properties can be inspected using the properties instance method and module-level "CONSTANT_CASE" constants. | ||||||||||||
> > | All FSTs support constructive operations such as composition (compose ), intersection (intersect ), and reversal (reverse ), storing the result in a vector FST.
>>> cv = fst.compose(c, v) >>> print cv <vector Fst at 0x7f1e45890190> | ||||||||||||
Changed: | |||||||||||||
< < | >>> print bool(c.properties(fst.ACCEPTOR)) False >>> print bool(v.properties(fst.MUTABLE)) True | ||||||||||||
> > | FSTs also support tests for equality (equal ), equivalence (equivalent ), [[RandEquivalentDoc][stochastic equivalence] (randequivalent ), and isomorphism (isomorphic ).
>>> print fst.isomorphic(c, v) True | ||||||||||||
Changed: | |||||||||||||
< < | The FstError exception signals an error during FST operations. | ||||||||||||
> > | 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() | ||||||||||||
Changed: | |||||||||||||
< < | >>> fst.intersect(c, v) Error: IntersectFst: input FSTs are not acceptors Traceback (most recent call last): File "<stdin>", line 1, in <module> File "pywrapfst.pyx", line 1481, in pywrapfst.intersect (pywrapfst.cc:10151) File "pywrapfst.pyx", line 1128, in pywrapfst._init_MutableFst (pywrapfst.cc:7154) pywrapfst.FstError: Operation failed | ||||||||||||
> > | 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. | |||||||||||||
Changed: | |||||||||||||
< < |
>>> help(fst.equal) | ||||||||||||
> > | >>> help(fst.equal) | ||||||||||||
Help on built-in function equal in module pywrapfst:
equal(...) | |||||||||||||
Added: | |||||||||||||
> > | equal(ifst1, ifst2, delta=fst.kDelta) | ||||||||||||
Are two FSTs equal? | |||||||||||||
Changed: | |||||||||||||
< < | This function tests whether two FSTs have the same states with the same | ||||||||||||
> > | 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: | |||||||||||||
Changed: | |||||||||||||
< < |
| ||||||||||||
> > |
| ||||||||||||
Returns: | |||||||||||||
Changed: | |||||||||||||
< < | True if the two transducers satisfy the above condition, else False.
See also: `equivalent`, `isomorphic`, `randequivalent`. | ||||||||||||
> > | True if the two FSTs satisfy the above conditions, otherwise False. | ||||||||||||
Changed: | |||||||||||||
< < | >>> help(v.project) Help on built-in function project: | ||||||||||||
> > | See also: `equivalent`, `isomorphic`, `randequivalent`.
FST output | ||||||||||||
Changed: | |||||||||||||
< < | project(...) Converts the FST to an acceptor using input or output labels. | ||||||||||||
> > | FSTs can be written to disk using the write instance method.
>>> f.write("f.fst") | ||||||||||||
Changed: | |||||||||||||
< < | This operation destructively projects an FST onto its domain or range by
either copying each arc's input label to its output label (the default) or
vice versa.
Args:
See also: `decode`, `encode`, `relabel`.
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).
>>> L = fst.Fst("L.fst") >>> G = fst.Fst("G.fst") >>> C = fst.Fst("C.fst") >>> H = fst.Fst("H.fst") >>> LG = fst.determinize(fst.compose(L, G)) >>> CLG = fst.determinize(fst.compose(C, LG)) >>> HCLG = fst.determinize(fst.compose(H, CLG)) | ||||||||||||
> > | They also can be written into FARs using the FarWriter object. This takes a filename argument, and optionally, specifications of the arc type and FAR type. Once created, an FST can be written to the FarWriter object using dictionary-style assignment.
>>> writer = fst.FarWriter.create("lattice.far") >>> writer["1best"] = 1best >>> writer["2best] = 2best
Note that the FAR itself is not guaranteed to be flushed to disk until the >>> del writer Worked examplePutting 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). >>> reader = fst.FarReader("hclg.far") >>> LG = fst.determinize(fst.compose(reader["L"], reader["G"])) >>> CLG = fst.determinize(fst.compose(reader["C"], LG)) >>> HCLG = fst.determinize(fst.compose(reader["H"], CLG)) | ||||||||||||
>>> HCLG.minimize() | |||||||||||||
Changed: | |||||||||||||
< < | >>> HCLG.write("hclg.fst") | ||||||||||||
> > | >>> HCLG.write("hclg.fst") |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
Changed: | ||||||||
< < | Python Extensions to OpenFst | |||||||
> > | Python Extensions to OpenFst | |||||||
This extension exposes nearly all of the FST script-level interface in Python. Like the script-level interface, it supports arbitrary arcs and weights. | ||||||||
Added: | ||||||||
> > | Note that this extension is unrelated to, and incompatible with, any other third-party Python extensions for OpenFst (e.g., pyfst ). Install should not install this extension in the same Python environment as any third-party extensions. | |||||||
To install this package, issue --enable-python= during configuration. Or, you can install it from PyPi using tools like pip or easy_install .
Users may use this extension to rapidly prototype or construct FSTs interactively using the Python REPL. Note that | ||||||||
Line: 22 to 24 | ||||||||
Changed: | ||||||||
< < | fst.Fst takes an optional second argument, a string indicating the desired Fst type. The FST is converted to this type if it is not already of the appropriate type. | |||||||
> > | fst.Fst takes an optional second argument, a string indicating the desired FST type. The FST is converted to this type if it is not already of the appropriate type. | |||||||
>>> c = fst.Fst("const1.fst") |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python Extensions to OpenFstThis extension exposes nearly all of the FST script-level interface in Python. Like the script-level interface, it supports arbitrary arcs and weights. | ||||||||
Added: | ||||||||
> > | To install this package, issue --enable-python= during configuration. Or, you can install it from PyPi using tools like pip or easy_install . | |||||||
Users may use this extension to rapidly prototype or construct FSTs interactively using the Python REPL. Note that >>> indicates the Python interactive prompt; all other typewriter-text lines are print to stdout or stderr .
The Python module itself is called |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python Extensions to OpenFst | ||||||||
Line: 9 to 9 | ||||||||
The Python module itself is called fst . | ||||||||
Changed: | ||||||||
< < | import fst | |||||||
> > | >>> import fst | |||||||
FSTs are read in from disk using |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python Extensions to OpenFst | ||||||||
Line: 12 to 12 | ||||||||
import fst | ||||||||
Changed: | ||||||||
< < | FSTs are instantiated by using fst.Fst , which takes a filename string as its a required first argument. | |||||||
> > | FSTs are read in from disk using fst.Fst , which takes a filename string as its a required first argument. | |||||||
>>> f = fst.Fst("vector1.fst") |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python Extensions to OpenFst | ||||||||
Changed: | ||||||||
< < | This extension exposes nearly all of the FST script-level interface in Python. Like the script-level interface, it supports arbitrary arcs and weights. | |||||||
> > | This extension exposes nearly all of the FST script-level interface in Python. Like the script-level interface, it supports arbitrary arcs and weights. | |||||||
Users may use this extension to rapidly prototype or construct FSTs interactively using the Python REPL. Note that >>> indicates the Python interactive prompt; all other typewriter-text lines are print to stdout or stderr . | ||||||||
Line: 71 to 71 | ||||||||
A few operations (e.g., weight-pushing) are available in both constructive and destructive forms, albeit with slightly different options. | ||||||||
Changed: | ||||||||
< < | FST properties can be inspected using properties instance method and module-level "CONSTANT_CASE" constants. | |||||||
> > | FST properties can be inspected using the properties instance method and module-level "CONSTANT_CASE" constants. | |||||||
>>> print bool(c.properties(fst.ACCEPTOR)) | ||||||||
Line: 132 to 132 | ||||||||
Changed: | ||||||||
< < | Putting it all together, the following example, based on Mohri et al. 2002, 2008, shows the construction of an ASR lattice from a pronunciation lexicon L, grammar G, a transducer from context-dependent phones to context-independent phones C, and an HMM set H: | |||||||
> > | 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). | |||||||
>>> L = fst.Fst("L.fst") |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python Extensions to OpenFst | ||||||||
Line: 132 to 132 | ||||||||
Changed: | ||||||||
< < | Putting it all together, the following example, based on Mohri et al. 2002, 2008, shows the construction of an ASR system given a pronunciation lexicon L, grammar G, a transducer from context-dependent phones to context-independent phones C, and an HMM set H: | |||||||
> > | Putting it all together, the following example, based on Mohri et al. 2002, 2008, shows the construction of an ASR lattice from a pronunciation lexicon L, grammar G, a transducer from context-dependent phones to context-independent phones C, and an HMM set H: | |||||||
>>> L = fst.Fst("L.fst") |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python Extensions to OpenFst | ||||||||
Changed: | ||||||||
< < | This extension exposes nearly all of the FST script-level interface in Python. The current target is Python 2.7, though the module should be trivially portable to Python 3. | |||||||
> > | This extension exposes nearly all of the FST script-level interface in Python. Like the script-level interface, it supports arbitrary arcs and weights. | |||||||
Changed: | ||||||||
< < | Users may use this extension to rapidly prototype or construct FSTs interactively using the Python REPL. | |||||||
> > | Users may use this extension to rapidly prototype or construct FSTs interactively using the Python REPL. Note that >>> indicates the Python interactive prompt; all other typewriter-text lines are print to stdout or stderr . | |||||||
Changed: | ||||||||
< < | To enable it, provide the --enable-python flag to configure , then build and install as normal. This will install a Python module pywrapfst to the site-packages (or dist-packages , on Debian/Ubuntu and their derivatives) below the requested --prefix ; by default, this is /usr/local/lib/python2.7/{site,dist}-packages/ . Users who have installed Python in their home directory may install it by providing --prefix=$HOME flag to configure . In the worst case, users can manually build the shared object and then place it in Python's path.
The module is called | |||||||
> > | The Python module itself is called fst . | |||||||
Changed: | ||||||||
< < | import pywrapfst as fst | |||||||
> > | import fst | |||||||
Deleted: | ||||||||
< < | Note also that >>> indicates the Python interactive prompt; all other typewriter-text lines are print to stdout or stderr . | |||||||
FSTs are instantiated by using fst.Fst , which takes a filename string as its a required first argument. |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python Extensions to OpenFst | ||||||||
Line: 8 to 8 | ||||||||
To enable it, provide the --enable-python flag to configure , then build and install as normal. This will install a Python module pywrapfst to the site-packages (or dist-packages , on Debian/Ubuntu and their derivatives) below the requested --prefix ; by default, this is /usr/local/lib/python2.7/{site,dist}-packages/ . Users who have installed Python in their home directory may install it by providing --prefix=$HOME flag to configure . In the worst case, users can manually build the shared object and then place it in Python's path. | ||||||||
Changed: | ||||||||
< < | The module is called pywrapfst but users can alias it; we will assume the fst path is used in the following examples. | |||||||
> > | The module is called pywrapfst but users can alias it; we will assume the fst alias is used in the following examples. | |||||||
import pywrapfst as fst | ||||||||
Changed: | ||||||||
< < | Note also that >>> indicates the Python interactive prompt; all other typewriter-text lines are print to STDOUT or STDERR. | |||||||
> > | Note also that >>> indicates the Python interactive prompt; all other typewriter-text lines are print to stdout or stderr . | |||||||
FSTs are instantiated by using fst.Fst , which takes a filename string as its a required first argument. | ||||||||
Line: 136 to 136 | ||||||||
Changed: | ||||||||
< < | Putting it all together, the following example, based on Mohri et al. 2002, shows the construction of an ASR system given a pronunciation lexicon L, grammar G, a transducer from context-dependent phones to context-independent phones C, and an HMM set H: | |||||||
> > | Putting it all together, the following example, based on Mohri et al. 2002, 2008, shows the construction of an ASR system given a pronunciation lexicon L, grammar G, a transducer from context-dependent phones to context-independent phones C, and an HMM set H: | |||||||
>>> L = fst.Fst("L.fst") |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
Changed: | ||||||||
< < | Python extensions to OpenFst | |||||||
> > | Python Extensions to OpenFst | |||||||
Changed: | ||||||||
< < | This extension exposes nearly all of the "script" interface in Python. The current target is Python 2.7, though the module should be trivially portable to Python 3. | |||||||
> > | This extension exposes nearly all of the FST script-level interface in Python. The current target is Python 2.7, though the module should be trivially portable to Python 3. | |||||||
Changed: | ||||||||
< < | Users may use this script to rapidly prototype or construct FSTs interactively using the Python REPL. | |||||||
> > | Users may use this extension to rapidly prototype or construct FSTs interactively using the Python REPL. | |||||||
Changed: | ||||||||
< < | To enable this extension, provide the --enable-python flag to configure , then build and install as normal. This will install a Python module pywrapfst to the site-packages (or dist-packages , on Debian/Ubuntu and their derivatives) below the requested --prefix ; by default, this is /usr/local/lib/python2.7/{site,dist}-packages/ . Users who have installed Python in their home directory may install it by providing --prefix=$HOME flag to configure . In the worst case, users can manually build the shared object and then place it [[Python's path][http://stackoverflow.com/questions/10161568/import-python-module-not-on-path]. | |||||||
> > | To enable it, provide the --enable-python flag to configure , then build and install as normal. This will install a Python module pywrapfst to the site-packages (or dist-packages , on Debian/Ubuntu and their derivatives) below the requested --prefix ; by default, this is /usr/local/lib/python2.7/{site,dist}-packages/ . Users who have installed Python in their home directory may install it by providing --prefix=$HOME flag to configure . In the worst case, users can manually build the shared object and then place it in Python's path. | |||||||
The module is called pywrapfst but users can alias it; we will assume the fst path is used in the following examples. |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Added: | ||||||||
> > |
Python extensions to OpenFstThis extension exposes nearly all of the "script" interface in Python. The current target is Python 2.7, though the module should be trivially portable to Python 3. Users may use this script to rapidly prototype or construct FSTs interactively using the Python REPL.
To enable this extension, provide the
The module is called
import pywrapfst as fst
Note also that
FSTs are instantiated by using
>>> f = fst.Fst("vector1.fst") >>> print f <vector Fst at 0x7f1e45890e70>
>>> c = fst.Fst("const1.fst") >>> print c <const Fst at 0x7f1e49aefbe8> >>> v = fst.Fst("const1.fst", fst_type="vector") >>> print v <vector Fst at 0x7f1e45890f10>
This conversion can also be accomplished after instantiation using
>>> v = fst.convert(c, fst_type="vector") >>> print v <vector Fst at 0x7f1e45890290>
All FSTs support constructive operations such as composition (
>>> cv = fst.compose(c, v) >>> print cv <vector Fst at 0x7f1e45890190>
All FSTs also support tests for equality (
>>> print fst.isomorphic(c, v) True
Finally, all FSTs can be written to disk using their
>>> c.write("const-copy.fst") FSTs which are mutable (e.g., vector FSTs) also support destructive operations such as arc-sorting, inversion, and projection. These operations work in place, mutating the instance they are called on, and return nothing. These instance methods are not available for immutable FST types (e.g., const FSTs), and only become available once the FST has been converted to a mutable FST type.
>>> v.arcsort(sort_type="olabel") >>> v.invert() >>> v.project() A few operations (e.g., weight-pushing) are available in both constructive and destructive forms, albeit with slightly different options.
FST properties can be inspected using
>>> print bool(c.properties(fst.ACCEPTOR)) False >>> print bool(v.properties(fst.MUTABLE)) True
The
>>> fst.intersect(c, v) Error: IntersectFst: input FSTs are not acceptors Traceback (most recent call last): File "<stdin>", line 1, in <module> File "pywrapfst.pyx", line 1481, in pywrapfst.intersect (pywrapfst.cc:10151) File "pywrapfst.pyx", line 1128, in pywrapfst._init_MutableFst (pywrapfst.cc:7154) pywrapfst.FstError: Operation failed
To read documentation on individual FST operations, use Python's built-in
>>> help(fst.equal) Help on built-in function equal in module pywrapfst: equal(...) 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 transducers satisfy the above condition, else False. See also: `equivalent`, `isomorphic`, `randequivalent`. >>> help(v.project) Help on built-in function project: project(...) Converts the FST to an acceptor using input or output labels. This operation destructively projects an FST onto its domain or range by either copying each arc's input label to its output label (the default) or vice versa. Args: project_output: Should the output labels be projected? See also: `decode`, `encode`, `relabel`. Putting it all together, the following example, based on Mohri et al. 2002, shows the construction of an ASR system given a pronunciation lexicon L, grammar G, a transducer from context-dependent phones to context-independent phones C, and an HMM set H:
>>> L = fst.Fst("L.fst") >>> G = fst.Fst("G.fst") >>> C = fst.Fst("C.fst") >>> H = fst.Fst("H.fst") >>> LG = fst.determinize(fst.compose(L, G)) >>> CLG = fst.determinize(fst.compose(C, LG)) >>> HCLG = fst.determinize(fst.compose(H, CLG)) >>> HCLG.minimize() >>> HCLG.write("hclg.fst") |