def connect(fsa: Fsa) -> Fsa: '''Connect an FSA. Removes states that are neither accessible nor co-accessible. It works only on CPU. Note: A state is not accessible if it is not reachable from the start state. A state is not co-accessible if it cannot reach the final state. Caution: If the input FSA is already connected, it is returned directly. Otherwise, a new connected FSA is returned. Args: fsa: The input FSA to be connected. Returns: An FSA that is connected. ''' if fsa.properties & fsa_properties.ACCESSIBLE != 0 and \ fsa.properties & fsa_properties.COACCESSIBLE != 0: return fsa assert fsa.is_cpu() need_arc_map = True ragged_arc, arc_map = _k2.connect(fsa.arcs, need_arc_map=need_arc_map) out_fsa = k2.utils.fsa_from_unary_function_tensor(fsa, ragged_arc, arc_map) return out_fsa
def connect(fsa: Fsa) -> Fsa: '''Connect an FSA. Removes states that are neither accessible nor co-accessible. Note: A state is not accessible if it is not reachable from the start state. A state is not co-accessible if it cannot reach the final state. Caution: If the input FSA is already connected, it is returned directly. Otherwise, a new connected FSA is returned. Args: fsa: The input FSA to be connected. Returns: An FSA that is connected. ''' if fsa.properties & fsa_properties.ACCESSIBLE != 0 and \ fsa.properties & fsa_properties.COACCESSIBLE != 0: return fsa need_arc_map = True ragged_arc, arc_map = _k2.connect(fsa.arcs, need_arc_map=need_arc_map) out_fsa = Fsa(ragged_arc) for name, value in fsa.named_tensor_attr(): setattr(out_fsa, name, index_attr(value, arc_map)) for name, value in fsa.named_non_tensor_attr(): setattr(out_fsa, name, value) return out_fsa
def connect(fsa: Fsa) -> Fsa: '''Connect an FSA. Removes states that are neither accessible nor co-accessible. Note: A state is not accessible if it is not reachable from the start state. A state is not co-accessible if it cannot reach the final state. Caution: If the input FSA is already connected, it is returned directly. Otherwise, a new connected FSA is returned. Args: fsa: The input FSA to be connected. Returns: An FSA that is connected. ''' properties = getattr(fsa, 'properties', None) if properties is not None \ and is_accessible(properties) \ and is_coaccessible(properties): return fsa need_arc_map = True ragged_arc, arc_map = _k2.connect(fsa.arcs, need_arc_map=need_arc_map) out_fsa = Fsa.from_ragged_arc(ragged_arc) for name, value in fsa.named_tensor_attr(): setattr(out_fsa, name, index_select(value, arc_map)) for name, value in fsa.named_non_tensor_attr(): setattr(out_fsa, name, value) return out_fsa