Skip to content

floor114/easy_encoding

easy_encoding

Build Status

Easy encoding encoding is a tool that allows you to encode and decode data via:

  • Huffman encoding;
  • Shannon-Fano encoding.

Installation

Add this line to your application's Gemfile:

gem 'easy_encoding'

And then execute:

$ bundle 

Or install it yourself as:

$ gem install easy_encoding 

Configuration

You can configure symbol for left and right node of tree.

EasyEncoding.configure do |config| config.right_node_symbol = '0' config.left_node_symbol = '1' end

Usage

Huffman coding

Using with string:

huffman = EasyEncoding::Huffman.new('code') huffman.frequencies #=> {:e=>0.25, :d=>0.25, :o=>0.25, :c=>0.25} huffman.char_codes #=> {:c=>"00", :o=>"01", :d=>"10", :e=>"11"}

Using with frequencies:

huffman = EasyEncoding::Huffman.new({ x7: 0.42, x3: 0.28, x5: 0.1, x6: 0.1, x4: 0.05, x2: 0.03, x1: 0.02 }) huffman.frequencies #=> {:x7=>0.42, :x3=>0.28, :x6=>0.1, :x5=>0.1, :x4=>0.05, :x2=>0.03, :x1=>0.02} huffman.char_codes #=> {:x7=>"1", :x3=>"01", :x5=>"0000", :x6=>"0001", :x4=>"0011", :x2=>"00100", :x1=>"00101"}

Shannon-Fano coding

Using with string:

shannon_fano = EasyEncoding::ShannonFano.new('Code') shannon_fano.frequencies #=> {"e"=>0.25, "d"=>0.25, "o"=>0.25, "c"=>0.25} shannon_fano.char_codes #=> {"e"=>"11", "d"=>"10", "o"=>"01", "c"=>"00"}

Using with frequencies:

shannon_fano = EasyEncoding::ShannonFano.new({ x7: 0.42, x3: 0.28, x5: 0.1, x6: 0.1, x4: 0.05, x2: 0.03, x1: 0.02 }) shannon_fano.frequencies #=> {:x7=>0.42, :x3=>0.28, :x5=>0.1, :x6=>0.1, :x4=>0.05, :x2=>0.03, :x1=>0.02} shannon_fano.char_codes #=> {:x7=>"1", :x3=>"01", :x6=>"0011", :x5=>"0010", :x4=>"0001", :x2=>"00001", :x1=>"00000"}

Contributing

See CONTRIBUTING.md.

License

The gem is available as open source under the terms of the MIT License.

About

Easy encoding is a tool that allows you to encode and decode data.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages