예제 #1
0
def remove_epsilon(fsa: Fsa) -> Fsa:
    '''Remove epsilons (symbol zero) in the input Fsa.

    Caution:
      It only works on for CPU and doesn't support autograd.

    Args:
      fsa:
        The input FSA. It can be either a single FSA or an FsaVec.
        Must be top-sorted.
    Returns:
        The result Fsa, it's equivalent to the input ``fsa`` under
        tropical semiring but will be epsilon-free.
        It will be the same as the input ``fsa`` if the input
        ``fsa`` is epsilon-free. Otherwise, a new epsilon-free fsa
        is returned and the input ``fsa`` is NOT modified.
    '''
    properties = getattr(fsa, 'properties', None)
    if properties is not None and properties & fsa_properties.EPSILON_FREE != 0:
        return fsa

    ragged_arc, arc_derivs = _k2.remove_epsilon(fsa.arcs)
    aux_labels = None
    if hasattr(fsa, 'aux_labels'):
        aux_labels = _k2.simple_ragged_index_select(fsa.aux_labels, arc_derivs)
    out_fsa = Fsa(ragged_arc, aux_labels)

    for name, value in fsa.named_non_tensor_attr():
        setattr(out_fsa, name, value)

    return out_fsa
예제 #2
0
파일: fsa_algo.py 프로젝트: qijiaxing/k2
def determinize(fsa: Fsa) -> Fsa:
    '''Determinize the input Fsa.

    Caution:
      It only works on for CPU and doesn't support autograd.

    Args:
      fsa:
        The input FSA. It can be either a single FSA or an FsaVec.
        Must be connected. It's also expected to be epsilon-free,
        but this is not checked; in any case,
        epsilon will be treated as a normal symbol.
    Returns:
        The result Fsa, it's equivalent to the input ``fsa`` under
        tropical semiring but will be deterministic.
        It will be the same as the input ``fsa`` if the input
        ``fsa`` has property kFsaPropertiesArcSortedAndDeterministic.
        Otherwise, a new deterministic fsa is returned and the
        input ``fsa`` is NOT modified.
    '''
    properties = getattr(fsa, 'properties', None)
    if properties is not None \
            and properties & fsa_properties.ARC_SORTED_AND_DETERMINISTIC != 0: # noqa
        return fsa

    ragged_arc, arc_derivs = _k2.determinize(fsa.arcs)
    aux_labels = None
    if hasattr(fsa, 'aux_labels'):
        aux_labels = _k2.simple_ragged_index_select(fsa.aux_labels, arc_derivs)
    out_fsa = Fsa(ragged_arc, aux_labels)

    for name, value in fsa.named_non_tensor_attr():
        setattr(out_fsa, name, value)

    return out_fsa