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_state (any) – The final state to add

add_start_state(start_state: Any)[source]

Add a start state

Parameters

start_state (any) – 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_from (any) – The source state

  • input_symbol (any) – The symbol to read

  • s_to (any) – The destination state

  • output_symbols (iterable of Any) – The symbols to output

add_transitions(transitions_list)[source]

Adds several transitions to the FST

Parameters

transitions_list (list 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_concatenate – A new FST which is the concatenation of the two given FST

Return type

FST

property final_states

Get the final states of the FST

Returns

final_states – The final states of the FST

Return type

set of any

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

A FST read from the graph

Return type

enfa

get_number_transitions() int[source]

Get the number of transitions in the FST

Returns

n_transitions – The number of transitions

Return type

int

property input_symbols

Get the input symbols of the FST

Returns

input_symbols – The input symbols of the FST

Return type

set of any

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_star – A FST representing the kleene star of the FST

Return type

FST

property output_symbols

Get the output symbols of the FST

Returns

output_symbols – The output symbols of the FST

Return type

set of any

property start_states

Get the start states of the FST

Returns

start_states – The start states of the FST

Return type

set of any

property states

Get the states of the FST

Returns

states – The states

Return type

set of any

to_networkx() networkx.classes.multidigraph.MultiDiGraph[source]

Transform the current fst into a networkx graph

Returns

graph – A networkx MultiDiGraph representing the fst

Return type

networkx.MultiDiGraph

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_word (iterable of any) – The word to translate

  • max_length (int, optional) – The maximum size of the output word, to prevent infinite generation due to epsilon transitions

Returns

output_word – The translation of the input word

Return type

iterable of any

union(other_fst)[source]

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

Returns

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

Return type

FST

write_as_dot(filename)[source]

Write the FST in dot format into a file

Parameters

filename (str) – The filename where to write the dot file