k2-fsa / k2

FSA/FST algorithms, differentiable, with PyTorch compatibility.
https://k2-fsa.github.io/k2
Apache License 2.0
1.08k stars 211 forks source link

Question about k2.union() function. #1262

Closed lawlict closed 7 months ago

lawlict commented 7 months ago

Hi, I hope to take k2.union() function to merge the nbest paths into one fsa. The result is correct, but some how not as expected. Here is a simple example:

import k2
fsa_a = k2.linear_fsa([1,2,3])
fsa_b = fsa_a
fsas = k2.create_fsa_vec([fsa_a, fsa_b])
fsa_union = k2.union(fsas)
fsa_a.draw('fsa_a.png', title='fsa_a')
fsa_b.draw('fsa_b.png', title='fsa_b')
fsa_union.draw('fsa_union.png', title='fsa_union')

Since fsa_a and fsa_b are the same, I expect that fsa_union would be equal to fsa_a. But fsa_union actually looks like: image Is there any way to simplify fsa_union?

Best wishes.

csukuangfj commented 7 months ago

I think fsa union follows the following semantics from openfst. https://www.openfst.org/twiki/bin/view/FST/UnionDoc

lawlict commented 7 months ago

@csukuangfj Yes, the result of union() function is correct, and I wonder is there anyway to simplify it?

danpovey commented 7 months ago

You could determinize it with epsilon removal, if supported, or remove epsilons then determinize. I don't recall how good the support in k2 for determinization is, it is only supported on CPU I think.

lawlict commented 7 months ago

@danpovey I see. Thank you for your response.