Easy encoding is a tool that allows you to encode and decode data.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.rubocop.yml
.ruby-version
.travis.yml
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Gemfile
LICENSE.txt
README.md
Rakefile
easy_encoding.gemspec

README.md

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.