How to properly set fst rules

482 views Asked by At

I got in touch with tranducers and python, so i use default FST library. For example, I have a list ['a','b','c']. I need to replace 'b' if it is followed by 'c'. I make following rules, but it works only if 'b' is between 'a' and 'c' and only with this length of array.

from fst import fst
list = ['a','b','c']
t = fst.FST('example')
for i in range(0,len(list)):
    t.add_state(str(i))

t.initial_state = '0'
t.add_arc('0','0',('a'),('a'))
t.add_arc('0','1',('b'),('d'))
t.add_arc('1','1',('c'),('c'))
t.set_final('1')

print t.transduce(list)

I got ['a','d','c'] I need to be able replace 'b' with 'd' wherever it is. e.g. replace 'b' when followed by 'l'

['m','r','b','l'] => ['m','r','o','l'] 
['m','b','l'] => ['m','o','l'] 
['b','l','o'] => ['o','l','o'] 

Please help me, thanks!

1

There are 1 answers

0
chapelo On BEST ANSWER

Consider this function...

lists = [['m','r','b','l'], 
         ['m','b','l'], 
         ['b','l','o'], 
         ['b','m','o']]

def change(list_, find_this, followed_by, replace_to):
    return_list = list_.copy()
    idx = list_.index(find_this)
    if list_[idx+1] == followed_by:
        return_list = list_.copy()
        return_list[idx] = replace_to
    return return_list

for lst in lists:
    print(change(lst, 'b', 'l', 'o'))

''' output:
['m', 'r', 'o', 'l']
['m', 'o', 'l']
['o', 'l', 'o']
['b', 'm', 'o']
'''

You should add other pertinent validations, though.