k2-fsa / k2

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

empty Fsas and RaggedTensors are truthy #1195

Open svandiekendialpad opened 1 year ago

svandiekendialpad commented 1 year ago

I encountered a case where best_paths = k2.shortest_path(fsa, use_double_scores=True) can return an emtpy Fsa and was surprised that bool(best_paths) returns True. Is this by design or would you consider adding something like:

class Fsa(object):
    [...]
    def __bool__(self) -> bool:
        return self.num_arcs > 0
    [...]

I noticed the same behaviour for RaggedTensor where bool(k2.RaggedTensor([], dtype=torch.int32)) returns True.