|
Compress::AsciiFlate - deflates text, outputs text not binary |
Compress::AsciiFlate - deflates text, outputs text not binary
use Compress::AsciiFlate;
my $af = new Compress::AsciiFlate;
my $text = 'some words some words some words';
$af->deflate($text);
print $text; # prints: "some words _1 _2 _1 _2"
$af->inflate($text);
print $text; # now prints: "some words some words some words"
print $af->olength; # original length: 33
print $af->dlength; # deflated length: 23
print $af->difference; # 10
print $af->ratio; # 0.696969696969697
print $af->ratio(3); # 0.697
print $af->percentage; # 69.69
print $af->percentage(4); # 69.697
print $af->count; # how many different words: 2
print join(' ',$af->table); # _1 some _2 words
Compress::AsciiFlate provides methods to deflate text to a non-binary state. The resulting text will retain one copy of each word so that it is still searchable, say, in a database field. This also means one can store the deflated text in a non-binary field and perform case- insensitive searches if required.
The core algorithm is very similar to the LZW algorithm. It works in the following way:
deflating...
if this word exists in my table:
output the code from my table
else
store the word with the next code and output the word
deflating
if this word is a code that exists in my table:
output the word from my table
else
store the word with the next code and output the word
A couple of details... the codes that are output are TEXT. The codes are 62ary using 0-9, A-Z and a-z as digits. The codes are prepended by an underscore in the output to distinguish them from normal words. If there are normal words in the source that happen to start with underscores, they too are prepended by another underscore to distinguish them from codes. So if every word in your source was different and started with an underscore, the "delfated" version would be larger!
Since the minimum length of a code is 2, the underscore and one digit, words below a length of 3 are not encoded. In fact, the algorithm checks to see that the code is actually shorter than the word so that, firstly, the output is not larger than the input and, secondly, codes are not wasted on words of the same size.
new() creates a new Compress::AsciiFlate object and returns it. If the argument
'lite' is also supplied, the object will not store the table it creates during de/inflation.
deflate($text|@text)inflate($text|@text)deflate() on a scalar or array. The table created is stored unless 'lite'
has been specified (see new()).
Jimi-Carlo Bukowski-Wills <jimi@webu.co.uk>
Copyright (C) 2006 by Jimi-Carlo Bukowski-Wills
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.
|
Compress::AsciiFlate - deflates text, outputs text not binary |