Created
April 28, 2020 22:33
-
-
Save cosinekitty/b7fdfeb71fc2539a7c055c96d953f204 to your computer and use it in GitHub Desktop.
First attempt at data compression output, edited for brevity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| Char=((('e',('l',('p','m'))),('\n',('s','t'))),((('o','n'),('r',('d','u'))),(((('b',((('j','q'),('z','x')),'v')),('y','g')),'i'),('a',('c',('h',('f',('w','k')))))))) | |
| NumChars=636945 | |
| Bits=r''' | |
| 5d1Wx9X+u6rf0F+XYLsOXYd7l2He1nXYd7/XYd59niXYd67LsOtaJXYd+ddhxaJ5wuw4tE8+xK7DiiQu | |
| w45hQXYdNoldh02iRZdh02iTiXXYdT765c+xK7DmC7DmFl2HMDiXXYc3y7Dm+Fl2HN8cS67Dm3l2HNvl | |
| 2HOF2HODiXXYc+zmuw59Zdhz4iL0uw531l2HO8bS7DsXvEuww5dhh3sl2GHPvEuww599xLsMOfWXYYBd | |
| ... | |
| dYq/vfbOrFXK6xV633yxX8NFENzFLFf2jDvf6xX9re+ErFZZyD8sVlnIPz94cFiss5B+fiDLFZZx7vPd | |
| liss4t3nyJYrLOD8sVlnB4o32+WKyzg+Xg4ZYrLOD5eh+WKyzg32RF3hwWKyzg32RFc6xWWcGcVyxWWc | |
| OFiss4cSFiss4ffLFZH5gsVkeG1lisjw++WKyPes8k | |
| ''' | |
| class BitReader: | |
| def __init__(self, encoded): | |
| self.encoded = encoded.replace('\n', '') | |
| self.position = 0 | |
| self.accum = 0 | |
| self.nbits = 0 | |
| def GetNextBit(self): | |
| if self.nbits == 0: | |
| c = self.encoded[self.position] | |
| self.position += 1 | |
| self.accum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.index(c) | |
| self.nbits = 6 | |
| bit = 1 & (self.accum >> 5) | |
| self.accum = 0b111111 & (self.accum << 1) | |
| self.nbits -= 1 | |
| return bit | |
| def Huffman(reader, node): | |
| while isinstance(node, tuple): | |
| node = node[reader.GetNextBit()] | |
| return node | |
| def Expand(): | |
| reader = BitReader(Bits) | |
| return ''.join(Huffman(reader, Char) for _ in range(NumChars)) | |
| if __name__ == "__main__": | |
| print(Expand()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment