utils.py
def decode(self, t, length, raw=False):
        """Decode encoded texts back into strs.

        Args:
            torch.LongTensor [length_0 + length_1 + ... length_{n - 1}]: encoded texts.
            torch.LongTensor [n]: length of each text.

        Raises:
            AssertionError: when the texts and its length does not match.

        Returns:
            text (str or list of str): texts to convert.
        """
        if length.numel() == 1:
            length = length[0]
            assert t.numel() == length, "text with length: {} does not match declared length: {}".format(t.numel(), length)
            if raw:
                return ''.join([self.alphabet[i - 1] for i in t])
            else:
                char_list = []
                for i in range(length):
                    if t[i] != 0 and (not (i > 0 and t[i - 1] == t[i])):
                        char_list.append(self.alphabet[t[i] - 1])
                return ''.join(char_list)
        else:
            # batch mode
            assert t.numel() == length.sum(), "texts with length: {} does not match declared length: {}".format(t.numel(), length.sum())
            texts = []
            index = 0
            for i in range(length.numel()):
                l = length[i]
                texts.append(
                    self.decode(
                        t[index:index + l], torch.LongTensor([l]), raw=raw))
                index += l
            return texts
Decode encoded texts back into strs.

        Args:
            torch.LongTensor [length_0 + length_1 + ... length_{n - 1}]: encoded texts.
            torch.LongTensor [n]: length of each text.

        Raises:
            AssertionError: when the texts and its length does not match.

        Returns:
            text (str or list of str): texts to convert.
        
Decode encoded texts back into strs.
Decode encoded texts back into strs .
def decode(self, t, length, raw=False):
        if length.numel() == 1:
            length = length[0]
            assert t.numel() == length, "text with length: {} does not match declared length: {}".format(t.numel(), length)
            if raw:
                return ''.join([self.alphabet[i - 1] for i in t])
            else:
                char_list = []
                for i in range(length):
                    if t[i] != 0 and (not (i > 0 and t[i - 1] == t[i])):
                        char_list.append(self.alphabet[t[i] - 1])
                return ''.join(char_list)
        else:
            assert t.numel() == length.sum(), "texts with length: {} does not match declared length: {}".format(t.numel(), length.sum())
            texts = []
            index = 0
            for i in range(length.numel()):
                l = length[i]
                texts.append(
                    self.decode(
                        t[index:index + l], torch.LongTensor([l]), raw=raw))
                index += l
            return texts
def decode ( self , t , length , raw = False ) : if length . numel ( ) == 1 : length = length [ 0 ] assert t . numel ( ) == length , "text with length: {} does not match declared length: {}" . format ( t . numel ( ) , length ) if raw : return '' . join ( [ self . alphabet [ i - 1 ] for i in t ] ) else : char_list = [ ] for i in range ( length ) : if t [ i ] != 0 and ( not ( i > 0 and t [ i - 1 ] == t [ i ] ) ) : char_list . append ( self . alphabet [ t [ i ] - 1 ] ) return '' . join ( char_list ) else : assert t . numel ( ) == length . sum ( ) , "texts with length: {} does not match declared length: {}" . format ( t . numel ( ) , length . sum ( ) ) texts = [ ] index = 0 for i in range ( length . numel ( ) ) : l = length [ i ] texts . append ( self . decode ( t [ index : index + l ] , torch . LongTensor ( [ l ] ) , raw = raw ) ) index += l return texts
Decode encoded texts back into strs.
Decode encoded texts back into strs .
"""Decode encoded texts back into strs.

        Args:
            torch.LongTensor [length_0 + length_1 + ... length_{n - 1}]: encoded texts.
            torch.LongTensor [n]: length of each text.

        Raises:
            AssertionError: when the texts and its length does not match.

        Returns:
            text (str or list of str): texts to convert.
        """ # batch mode
text (str or list of str): texts to convert.
text ( str or list of str ) : texts to convert .
when the texts and its length does not match.
when the texts and its length does not match .
AssertionError
self
t
length
raw
torch.LongTensor [length_0 + length_1 + ... length_{n - 1}]
None
torch.LongTensor [n]
length of each text.
length of each text .