abydos.compression package¶
abydos.compression.
The compression package defines compression and compression-related functions for use within Abydos, including implementations of the following:
- arithmetic coding functions (ac_train, ac_encode, & ac_decode)
- Burrows-Wheeler transform encoder/decoder (bwt_encode & bwt_decode)
- Run-Length Encoding encoder/decoder (rle_encode & rle_decode)
-
abydos.compression.
bwt_decode
(code, terminator='\x00')[source]¶ Return a word decoded from BWT form.
The Burrows-Wheeler transform is an attempt at placing similar characters together to improve compression. This function reverses the transform. Cf. [BW94].
Parameters: - code (str) – the word to transform from BWT form
- terminator (str) – a character added to word to signal the end of the string
Returns: word decoded by BWT
Return type: str
>>> bwt_decode('n\x00ilag') 'align' >>> bwt_decode('annb\x00aa') 'banana' >>> bwt_decode('annb@aa', '@') 'banana'
-
abydos.compression.
bwt_encode
(word, terminator='\x00')[source]¶ Return the Burrows-Wheeler transformed form of a word.
The Burrows-Wheeler transform is an attempt at placing similar characters together to improve compression. Cf. [BW94].
Parameters: - word (str) – the word to transform using BWT
- terminator (str) – a character to add to word to signal the end of the string
Returns: word encoded by BWT
Return type: str
>>> bwt_encode('align') 'n\x00ilag' >>> bwt_encode('banana') 'annb\x00aa' >>> bwt_encode('banana', '@') 'annb@aa'
-
abydos.compression.
rle_decode
(text, use_bwt=True)[source]¶ Perform decoding of run-length-encoding (RLE).
Cf. [RC67].
Based on http://rosettacode.org/wiki/Run-length_encoding#Python [Cod18b]. This is licensed GFDL 1.2.
Digits 0-9 cannot have been in the original text.
Parameters: - text (str) – a text string to decode
- use_bwt (bool) – boolean indicating whether to perform BWT decoding after RLE decoding
Returns: word decoded by RLE
Return type: str
>>> rle_decode('n\x00ilag') 'align' >>> rle_decode('align', use_bwt=False) 'align'
>>> rle_decode('annb\x00aa') 'banana' >>> rle_decode('banana', use_bwt=False) 'banana'
>>> rle_decode('ab\x00abbab5a') 'aaabaabababa' >>> rle_decode('3abaabababa', False) 'aaabaabababa'
-
abydos.compression.
rle_encode
(text, use_bwt=True)[source]¶ Perform encoding of run-length-encoding (RLE).
Cf. [RC67].
Based on http://rosettacode.org/wiki/Run-length_encoding#Python [Cod18b]. This is licensed GFDL 1.2.
Digits 0-9 cannot be in text.
Parameters: - text (str) – a text string to encode
- use_bwt (bool) – boolean indicating whether to perform BWT encoding before RLE encoding
Returns: word decoded by RLE
Return type: str
>>> rle_encode('align') 'n\x00ilag' >>> rle_encode('align', use_bwt=False) 'align'
>>> rle_encode('banana') 'annb\x00aa' >>> rle_encode('banana', use_bwt=False) 'banana'
>>> rle_encode('aaabaabababa') 'ab\x00abbab5a' >>> rle_encode('aaabaabababa', False) '3abaabababa'
-
abydos.compression.
ac_decode
(longval, nbits, probs)[source]¶ Decode the number to a string using the given statistics.
This is based on Andrew Dalke’s public domain implementation [Dal05]. It has been ported to use the fractions.Fraction class.
Parameters: - longval (int) – The first part of an encoded tuple from ac_encode
- nbits (int) – The second part of an encoded tuple from ac_encode
- probs (dict) – A probability statistics dictionary generated by ac_train
Returns: The arithmetically decoded text
Return type: str
>>> pr = ac_train('the quick brown fox jumped over the lazy dog') >>> ac_decode(16720586181, 34, pr) 'align'
-
abydos.compression.
ac_encode
(text, probs)[source]¶ Encode a text using arithmetic coding with the provided probabilities.
Text and the 0-order probability statistics -> longval, nbits
The encoded number is Fraction(longval, 2**nbits)
This is based on Andrew Dalke’s public domain implementation [Dal05]. It has been ported to use the fractions.Fraction class.
Parameters: - text (str) – A string to encode
- probs (dict) – A probability statistics dictionary generated by ac_train
Returns: The arithmetically coded text
Return type: tuple
>>> pr = ac_train('the quick brown fox jumped over the lazy dog') >>> ac_encode('align', pr) (16720586181, 34)
-
abydos.compression.
ac_train
(text)[source]¶ Generate a probability dict from the provided text.
Text -> 0-order probability statistics as a dict
This is based on Andrew Dalke’s public domain implementation [Dal05]. It has been ported to use the fractions.Fraction class.
Parameters: text (str) – The text data over which to calculate probability statistics. This must not contain the NUL (0x00) character because that’s used to indicate the end of data. Returns: a probability dict Return type: dict >>> ac_train('the quick brown fox jumped over the lazy dog') {' ': (Fraction(0, 1), Fraction(8, 45)), 'o': (Fraction(8, 45), Fraction(4, 15)), 'e': (Fraction(4, 15), Fraction(16, 45)), 'u': (Fraction(16, 45), Fraction(2, 5)), 't': (Fraction(2, 5), Fraction(4, 9)), 'r': (Fraction(4, 9), Fraction(22, 45)), 'h': (Fraction(22, 45), Fraction(8, 15)), 'd': (Fraction(8, 15), Fraction(26, 45)), 'z': (Fraction(26, 45), Fraction(3, 5)), 'y': (Fraction(3, 5), Fraction(28, 45)), 'x': (Fraction(28, 45), Fraction(29, 45)), 'w': (Fraction(29, 45), Fraction(2, 3)), 'v': (Fraction(2, 3), Fraction(31, 45)), 'q': (Fraction(31, 45), Fraction(32, 45)), 'p': (Fraction(32, 45), Fraction(11, 15)), 'n': (Fraction(11, 15), Fraction(34, 45)), 'm': (Fraction(34, 45), Fraction(7, 9)), 'l': (Fraction(7, 9), Fraction(4, 5)), 'k': (Fraction(4, 5), Fraction(37, 45)), 'j': (Fraction(37, 45), Fraction(38, 45)), 'i': (Fraction(38, 45), Fraction(13, 15)), 'g': (Fraction(13, 15), Fraction(8, 9)), 'f': (Fraction(8, 9), Fraction(41, 45)), 'c': (Fraction(41, 45), Fraction(14, 15)), 'b': (Fraction(14, 15), Fraction(43, 45)), 'a': (Fraction(43, 45), Fraction(44, 45)), '\x00': (Fraction(44, 45), Fraction(1, 1))}