コード例 #1
0
ファイル: fsa.py プロジェクト: zhu-han/k2
    def __str__(self) -> str:
        '''Return a string representation of this object

        For visualization and debug only.
        '''
        if hasattr(self, 'aux_labels') and isinstance(self.aux_labels,
                                                      torch.Tensor):
            aux_labels = self.aux_labels.to(torch.int32)
        else:
            aux_labels = None
        if self.arcs.num_axes() == 2:
            ans = 'k2.Fsa: ' + _k2.fsa_to_str(self.arcs, False, aux_labels)
        else:
            ans = 'k2.FsaVec: \n'
            for i in range(self.shape[0]):
                # get the i-th Fsa
                ragged_arc, start = self.arcs.index(0, i)
                end = start + ragged_arc.values().shape[0]
                ans += 'FsaVec[' + str(i) + ']: ' + _k2.fsa_to_str(
                    ragged_arc, False,
                    None if aux_labels is None else aux_labels[start:end])
        ans += 'properties_str = ' + _k2.fsa_properties_as_str(
            self._properties) + '.'
        for name, value in self.named_tensor_attr(include_scores=False):
            sep = '\n'
            ans += f'{sep}{name}: {value}'
        for name, value in self.named_non_tensor_attr():
            if name == 'symbols':
                continue
            sep = '\n'
            ans += f'{sep}{name}: {value}'

        return ans
コード例 #2
0
ファイル: utils.py プロジェクト: aarora8/k2
def to_str(fsa: Fsa, openfst: bool = False) -> str:
    '''Convert an Fsa to a string.  This version prints out all integer
    labels and integer ragged labels on the same line as each arc, the
    same format accepted by Fsa.from_str().

    Note:
      The returned string can be used to construct an Fsa with Fsa.from_str(),
      but you would need to know the names of the auxiliary labels and ragged
      labels.

    Args:
      openfst:
        Optional. If true, we negate the scores during the conversion.

    Returns:
      A string representation of the Fsa.
    '''
    assert fsa.arcs.num_axes() == 2
    extra_labels = []
    ragged_labels = []
    for name, value in sorted(fsa.named_tensor_attr(include_scores=False)):
        if isinstance(value, torch.Tensor) and value.dtype == torch.int32:
            extra_labels.append(value)
        elif isinstance(value, k2.RaggedTensor):
            ragged_labels.append(value)

    return _k2.fsa_to_str(fsa.arcs,
                          openfst=openfst,
                          extra_labels=extra_labels,
                          ragged_labels=ragged_labels)
コード例 #3
0
def to_str(fsa: Fsa, openfst: bool = False) -> str:
    '''Convert an Fsa to a string.

    Note:
      The returned string can be used to construct an Fsa.

    Args:
      openfst:
        Optional. If true, we negate the scores during the conversion,

    Returns:
      A string representation of the Fsa.
    '''
    if hasattr(fsa, 'aux_labels'):
        aux_labels = fsa.aux_labels.to(torch.int32)
    else:
        aux_labels = None
    return _k2.fsa_to_str(fsa.arcs, openfst, aux_labels)
コード例 #4
0
ファイル: utils.py プロジェクト: jtrmal/k2
def to_str(fsa: Fsa, openfst: bool = False) -> str:
    '''Convert an Fsa to a string.

    Caution:
      If the input fsa's aux_labels is a ragged tensor, it is ignored.

    Note:
      The returned string can be used to construct an Fsa.

    Args:
      openfst:
        Optional. If true, we negate the scores during the conversion,

    Returns:
      A string representation of the Fsa.
    '''
    if hasattr(fsa, 'aux_labels') and isinstance(fsa.aux_labels, torch.Tensor):
        aux_labels = fsa.aux_labels.to(torch.int32)
    else:
        aux_labels = None
    return _k2.fsa_to_str(fsa.arcs, openfst, aux_labels)
コード例 #5
0
ファイル: utils.py プロジェクト: aarora8/k2
def to_str_simple(fsa: Fsa, openfst: bool = False) -> str:
    '''Convert an Fsa to a string.  This is less complete than Fsa.to_str(),
    fsa.__str__(), or to_str_full(), meaning it prints only fsa.aux_labels and
    no ragged labels, not printing any other attributes.  This is used in
    testing.

    Note:
      The returned string can be used to construct an Fsa.  See also to_str().

    Args:
      openfst:
        Optional. If true, we negate the scores during the conversion.

    Returns:
      A string representation of the Fsa.
    '''
    assert fsa.arcs.num_axes() == 2
    if hasattr(fsa, 'aux_labels') and isinstance(fsa.aux_labels, torch.Tensor):
        aux_labels = [fsa.aux_labels.to(torch.int32)]
    else:
        aux_labels = []
    return _k2.fsa_to_str(fsa.arcs, openfst, aux_labels, [])