Commit e7824a71 authored by Konstantin Narkhov's avatar Konstantin Narkhov

Version 0.0.6

1. Refactor tests, add META6.json, move gist.html to ./js
parent d5ef07bc
{
"perl" : "6.*",
"name" : "LZW::Revolunet",
"version" : "0.0.6",
"description" : "Lempel–Ziv–Welch (LZW) algorithm implementation in Perl 6",
"depends" : [ ],
"license": "Artistic-2.0",
"provides" : {
"LZW::Revolunet" : "lib/LZW/Revolunet.pm6"
},
"source-url" : "git://gitlab.com/pheix/lzw-revolunet-perl6.git"
}
# lzw-js-perl6
# Lempel–Ziv–Welch (LZW) algorithm implementation in Perl 6
Port of javascript LZW algo implementation to Perl 6.
## Synopsis
```Perl
my $data = 'Тексты - это не энциклопедические и не лингвистические системы.' x 255;
# create LWZ object
my $lzw = LZW::Revolunet.new;
# set dictionary size, by default 57344
$lzw.set_dictsize( 97000 );
# compress
my $cmp = $lzw.compress( $lzw.encode_utf8($data) );
# decompress
my $dcmp = $lzw.decode_utf8( $lzw.decompress( $cmp ) );
# validate
if ( !($dcmp eq $data) ) {
die "decompressed data is corrupted";
}
# compress/decompress statistics
('compression ratio ' ~ floor($cmp.chars/$dcmp.chars*100) ~ '%').say;
```
## Description
**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)
## 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).
## Author
......@@ -8,4 +42,4 @@ Please contact me via [LinkedIn](https://www.linkedin.com/in/knarkhov/) or [Twit
## See also
[lzw_encoder.js](https://gist.github.com/revolunet/843889)
[LZW Data Compression](https://www2.cs.duke.edu/csed/curious/compression/lzw.html)
......@@ -2,10 +2,10 @@
<head>
<meta charset="utf-8">
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="../js/lzw.js"></script>
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="lzw.js"></script>
</head>
<body>
<input type="text" id="input">&nbsp;<button onclick="javascript:gist()">Gist</button>
......@@ -35,7 +35,6 @@
$('#result_deuriesc').text( decodeURIComponent(escape( unescape(encodeURIComponent($('#input').val())) )) );
}
var text = 'Привет, мир! Здравствуй, мир!';
//console.log(LZW.compress(text));
console.log(LZW.decompress(LZW.compress(text)));
</script>
</html>
......@@ -45,7 +45,6 @@ LZW = {
}
else {
phrase = dict['_'+currCode] ? dict['_'+currCode] : (oldPhrase + currChar);
//console.log(phrase);
}
out.push(phrase);
currChar = phrase.charAt(0);
......
......@@ -7,6 +7,7 @@ plan 1;
use LZW::Revolunet;
my $frlen = 4096;
my $dspath = './t/datasets';
my $phrase = (
'Тексты - это не энциклопедические и не лингвистические ' ~
' системы. Тексты сужают бесконечные или неопределенные ' ~
......@@ -16,10 +17,10 @@ my $phrase = (
'От интернета к Гуттенбергу: текст и гипертекст - URL: ' ~
'http://kiev.philosophy.ru/library/eco/internet.html Точность ' ~
'цитирования - почти дословно'
) x 2;
) x 255;
subtest {
plan 5;
plan 6;
my $obj = LZW::Revolunet.new;
my $dsz = 97000;
$obj.set_dictsize( $dsz );
......@@ -38,6 +39,10 @@ subtest {
'compress and decompress, ' ~
$cmp.chars ~ '/' ~ $phrase.chars ~ ' bytes'
);
ok( process_datasets( $obj, True ), 'process datasets');
}, 'subtest 1';
sub process_datasets( Any $obj, Bool $print ) returns Bool {
my @ds = get_datasets;
for @ds -> $ds {
my @frms = dataset_to_frames($ds);
......@@ -48,10 +53,12 @@ subtest {
$dstat += $cmp.chars
}
my $cntlen = @frms.join(q{}).chars;
($ds ~ ': compress ratio ' ~ floor($dstat/$cntlen*100) ~ '%').say;
if $print {
($ds ~ ': compress ratio ' ~ floor($dstat/$cntlen*100) ~ '%').say;
}
}
}, 'subtest 1';
True;
}
sub dataset_to_frames( Str $fname ) returns List {
my @frames;
......@@ -62,7 +69,6 @@ sub dataset_to_frames( Str $fname ) returns List {
# ( $ff~','~$hf~','~($hf+$ff*$frlen)~'='~$cnt.chars).say;
for (0..$ff) {
if ( $_ < $ff ) {
#@frames.push( $cnt.substr( $_*$frlen, ($_*$frlen+($frlen-1)), $frlen) );
@frames.push( $cnt.substr( $_*$frlen, $frlen) );
} else {
@frames.push( $cnt.substr( $_*$frlen, $_*$frlen+$hf) );
......@@ -74,7 +80,7 @@ sub dataset_to_frames( Str $fname ) returns List {
sub get_datasets returns List {
my @files;
my $dir = './t/datasets';
my $dir = $dspath;
my @todo = $dir.IO;
while @todo {
for @todo.pop.dir -> $path {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment