README.md 1.62 KB
Newer Older
Konstantin Narkhov's avatar
Konstantin Narkhov committed
1
# Lempel–Ziv–Welch (LZW) algorithm implementation in Raku (formerly 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 Int $r = 0;
my Str $d = 'Тексты - это не энциклопедические и не лингвистические системы.' x 255;
Konstantin Narkhov's avatar
Konstantin Narkhov committed
8

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

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

# compress
Konstantin Narkhov's avatar
Konstantin Narkhov committed
16
my $cmp  = $lzw.compress( $lzw.encode_utf8($d), :ratio($r) );
Konstantin Narkhov's avatar
Konstantin Narkhov committed
17 18

# decompress
Konstantin Narkhov's avatar
Konstantin Narkhov committed
19
my $dcmp = $lzw.decode_utf8( $lzw.decompress($cmp) );
Konstantin Narkhov's avatar
Konstantin Narkhov committed
20 21

# validate
Konstantin Narkhov's avatar
Konstantin Narkhov committed
22
if ( !($dcmp eq $d) ) {
Konstantin Narkhov's avatar
Konstantin Narkhov committed
23 24 25 26
    die "decompressed data is corrupted";
}

# compress/decompress statistics
Konstantin Narkhov's avatar
Konstantin Narkhov committed
27
('compression ratio ' ~ $r ~ '%').say;
Konstantin Narkhov's avatar
Konstantin Narkhov committed
28 29 30 31
```

## Description

Konstantin Narkhov's avatar
Konstantin Narkhov committed
32
**LZW::Revolunet**[Raku](https://raku.org) 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
33 34 35 36

## 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
37

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

Konstantin Narkhov's avatar
Konstantin Narkhov committed
40
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
41

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

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