摩迪利安 / huffman-php
PHP库,用于在PHP中使用哈夫曼算法
v2.0.6
2023-12-04 12:18 UTC
Requires
- php: >=7.1 || >=8.0
- mordilion/mutils: ^1.2
Requires (Dev)
- phpunit/phpunit: ^9.6
- vimeo/psalm: ^4.30
README
一个实现哈夫曼算法以压缩字符串的库。此外,它还提供了将结果二进制字符串压缩成URL安全字符串或自定义字符串基的功能。
典型用法
<?php use Mordilion\HuffmanPHP\Dictionary; use Mordilion\HuffmanPHP\Huffman; $text = 'This is a Text to compress with the Huffman-Algorithm'; // 53 chars $dictionary = new Dictionary([$text]); $huffman = new Huffman($dictionary); // result: 10000010101000011011010000110110010101110000101100010111001111010100110001001010100100001100100101100110011011000010010011001010111100101101101100000100000000011000111001001011111111110111011110010001101000100010011001011001 echo $huffman->encode($text, false); // result: 3QGgsnulxJqC2QweIz-V6SWj~pYoqYfA005HCR // length: 38 chars echo $huffman->encode($text, true); // result: mtxhycztntclyrustzsjioonyevmiijcdwrxqflkwsymxtsb // length: 48 chars $huffman = new Huffman($dictionary, 'abcdefghijklmnopqrstuvwxyz'); echo $huffman->encode($text, true);
使用MAX_LENGTH_WHOLE_WORDS以保持字典小和压缩高
<?php use Mordilion\HuffmanPHP\Dictionary; use Mordilion\HuffmanPHP\Huffman; $text = 'A Text with multiple Text Segments, to demonstrate the compression with multiple Text Segments'; // 94 chars $dictionary = new Dictionary(array_merge( array_unique(explode(' ', $text)), [' '] ), Dictionary::MAX_LENGTH_WHOLE_WORDS); $huffman = new Huffman($dictionary); // result: 0101011010001100110110010011010001100010110000011111011110011101011001101100100110100011100 echo $huffman->encode($text, false); // result: 27thCP8gJKOciDUU // length: 16 chars echo $huffman->encode($text, true); // result: eijxrqetkuoldtduacye // length: 20 chars $huffman = new Huffman($dictionary, 'abcdefghijklmnopqrstuvwxyz'); echo $huffman->encode($text, true);