README.md 1.6 KB
Newer Older
Konstantin Narkhov's avatar
Konstantin Narkhov committed
1
# Lempel–Ziv–Welch (LZW) algorithm implementation in Perl 6
Konstantin Narkhov's avatar
Konstantin Narkhov committed
2

Konstantin Narkhov's avatar
Konstantin Narkhov committed
3 4
## Synopsis

Konstantin Narkhov's avatar
Konstantin Narkhov committed
5
```perl
Konstantin Narkhov's avatar
Konstantin Narkhov committed
6 7
my $data = 'Тексты - это не энциклопедические и не лингвистические системы.' x 255;

Konstantin Narkhov's avatar
Konstantin Narkhov committed
8
# create LWZ object with default dictionary size (57344)
Konstantin Narkhov's avatar
Konstantin Narkhov committed
9 10
my $lzw = LZW::Revolunet.new;

Konstantin Narkhov's avatar
Konstantin Narkhov committed
11 12
# OR create LWZ object with user defined dictionary size
my $lzw = LZW::Revolunet.new( dictsize => 97000 );
Konstantin Narkhov's avatar
Konstantin Narkhov committed
13 14 15 16 17

# compress
my $cmp  = $lzw.compress( $lzw.encode_utf8($data) );

# decompress
Konstantin Narkhov's avatar
Konstantin Narkhov committed
18
my $dcmp = $lzw.decode_utf8( $lzw.decompress($cmp) );
Konstantin Narkhov's avatar
Konstantin Narkhov committed
19 20 21 22 23 24 25 26 27 28 29 30

# validate
if ( !($dcmp eq $data) ) {
    die "decompressed data is corrupted";
}

# compress/decompress statistics
('compression ratio ' ~ floor($cmp.chars/$dcmp.chars*100) ~ '%').say;
```

## Description

Konstantin Narkhov's avatar
Konstantin Narkhov committed
31
**LZW::Revolunet** — Perl6 implementation of universal lossless data compression [algorithm](https://en.wikipedia.org/wiki/Lzw) created by Abraham Lempel, Jacob Ziv, and Terry Welch. This module is based on JavaScript implementation ([lzw_encoder.js](https://gist.github.com/revolunet/843889)) by [Julien Bouquillon](https://github.com/revolunet)
Konstantin Narkhov's avatar
Konstantin Narkhov committed
32 33 34 35

## License

**LZW::Revolunet** is free and opensource software, so you can redistribute it and/or modify it under the terms of the [The Artistic License 2.0](https://opensource.org/licenses/Artistic-2.0).
Konstantin Narkhov's avatar
Konstantin Narkhov committed
36

Konstantin Narkhov's avatar
Konstantin Narkhov committed
37
## Author
Konstantin Narkhov's avatar
Konstantin Narkhov committed
38

Konstantin Narkhov's avatar
Konstantin Narkhov committed
39
Please contact me via [LinkedIn](https://www.linkedin.com/in/knarkhov/) or [Twitter](https://twitter.com/CondemnedCell). Your feedback is welcome at [narkhov.pro](https://narkhov.pro/contact-information.html).
Konstantin Narkhov's avatar
Konstantin Narkhov committed
40

Konstantin Narkhov's avatar
Konstantin Narkhov committed
41 42
## See also

Konstantin Narkhov's avatar
Konstantin Narkhov committed
43
[LZW Data Compression](https://www2.cs.duke.edu/csed/curious/compression/lzw.html)