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
def _init_properties(self) -> None: if self.arcs.num_axes() == 2: properties = _k2.get_fsa_basic_properties(self.arcs) else: properties = _k2.get_fsa_vec_basic_properties(self.arcs) self._properties = properties if properties & 1 != 1: raise ValueError( "Fsa is not valid, properties are: {} = {}, arcs are: {}". format(properties, _k2.fsa_properties_as_str(properties), str(self.arcs)))
def __str__(self) -> str: '''Return a string representation of this object (note: does not contain all the information in it for now)''' if hasattr(self, 'aux_labels'): aux_labels = self.aux_labels.to(torch.int32) else: aux_labels = None ans = "k2.Fsa: " + _fsa_to_str(self.arcs, False, aux_labels) ans += "properties_str = " + _k2.fsa_properties_as_str( self._properties) + "." return ans
def properties_to_str(p: int) -> str: '''Convert properties to a string for debug purpose. Args: p: An integer returned by :func:`get_properties`. Returns: A string representation of the input properties. ''' return _k2.fsa_properties_as_str(p)
def __getattr__(self, name: str) -> Any: if name == 'labels': return self.arcs.values()[:, 2] elif name in self._tensor_attr: return self._tensor_attr[name] elif name in self._non_tensor_attr: return self._non_tensor_attr[name] elif name in self._grad_cache: return self._grad_cache[name] elif name == 'properties': return self._properties elif name == 'properties_str': return _k2.fsa_properties_as_str(self._properties) raise AttributeError(f'Unknown attribute {name}')
def __str__(self) -> str: '''Return a string representation of this object (note: does not contain all the information in it for now)''' if hasattr(self, 'aux_labels'): aux_labels = self.aux_labels.to(torch.int32) else: aux_labels = None if self.arcs.num_axes() == 2: ans = "k2.Fsa: " + _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) + "]: " + _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) + "." return ans
def properties_str(self) -> str: return _k2.fsa_properties_as_str(self.properties)