Finite State Transducer

pyformlang.fst

This module deals with finite state transducers.

Available Classes

FST

A Finite State Transducer

class pyformlang.fst.FST[source]

Representation of a Finite State Transducer

add_final_state(final_state: Any)[source]

Add a final state

Parameters:
final_stateany

The final state to add

add_start_state(start_state: Any)[source]

Add a start state

Parameters:
start_stateany

The start state

add_transition(s_from: Any, input_symbol: Any, s_to: Any, output_symbols: Iterable[Any])[source]

Add a transition to the FST

Parameters:
s_fromany

The source state

input_symbolany

The symbol to read

s_toany

The destination state

output_symbolsiterable of Any

The symbols to output

add_transitions(transitions_list)[source]

Adds several transitions to the FST

Parameters:
transitions_listlist of tuples

The tuples have the form (s_from, in_symbol, s_to, out_symbols)

concatenate(other_fst)[source]

Makes the concatenation of two fst :param other_fst: The other FST :type other_fst: FST

Returns:
fst_concatenateFST

A new FST which is the concatenation of the two given FST

property final_states

Get the final states of the FST

Returns:
final_statesset of any

The final states of the FST

classmethod from_networkx(graph)[source]

Import a networkx graph into an finite state transducer. The imported graph requires to have the good format, i.e. to come from the function to_networkx

Parameters:
graph

The graph representation of the FST

Returns:
enfa

A FST read from the graph

get_number_transitions() int[source]

Get the number of transitions in the FST

Returns:
n_transitionsint

The number of transitions

property input_symbols

Get the input symbols of the FST

Returns:
input_symbolsset of any

The input symbols of the FST

intersection(indexed_grammar)[source]

Compute the intersection with an other object

Equivalent to:

>> fst and indexed_grammar

kleene_star()[source]

Computes the kleene star of the FST

Returns:
fst_starFST

A FST representing the kleene star of the FST

property output_symbols

Get the output symbols of the FST

Returns:
output_symbolsset of any

The output symbols of the FST

property start_states

Get the start states of the FST

Returns:
start_statesset of any

The start states of the FST

property states

Get the states of the FST

Returns:
statesset of any

The states

to_networkx() MultiDiGraph[source]

Transform the current fst into a networkx graph

Returns:
graphnetworkx.MultiDiGraph

A networkx MultiDiGraph representing the fst

property transitions

Gives the transitions as a dictionary

translate(input_word: Iterable[Any], max_length: int = -1) Iterable[Any][source]

Translate a string into another using the FST

Parameters:
input_worditerable of any

The word to translate

max_lengthint, optional

The maximum size of the output word, to prevent infinite generation due to epsilon transitions

Returns:
output_worditerable of any

The translation of the input word

union(other_fst)[source]

Makes the union of two fst :param other_fst: The other FST :type other_fst: FST

Returns:
union_fstFST

A new FST which is the union of the two given FST

write_as_dot(filename)[source]

Write the FST in dot format into a file

Parameters:
filenamestr

The filename where to write the dot file