Generate output based on first character of a word

120 views Asked by At

I am trying to set up a Finite State Transducer with Helsinki Finite State Technology (HFST) for Python.

I want if the first character of a word is an 'o' the output is "Positive" and if there are characters following in the same word, output empty for every character by using regex.
But I don't accept only "o".

e.g. "oa" = "positive" , empty
     "aa" = 0
     "o"  = 0

What I got so far from the HFST tutorials:

t = hfst.HfstBasicTransducer()
t.add_state(1)
t.add_state(2)
tr = hfst.HfstBasicTransition(1,"o","positive",0.0)
tr2 = hfst.regex("?:0")
t.add_transition(0,1,tr)
t.add_transition(1,2, tr2)
1

There are 1 answers

0
popcorndude On

The missing step is that lookup() will only return paths that end in a final state, which you can specify with transducer.set_final_weight(state, weight).

>>> import hfst
>>> t = hfst.HfstBasicTransducer()
>>> t.add_state(1)
1
>>> tr = hfst.HfstBasicTransition(1,'o', 'positive',0.0)
>>> t.add_transition(0,tr)
>>> t.set_final_weight(1,0.0)
>>> t.lookup('o')
{'o': [('positive', 0.0)]}