0.0
No commit activity in last 3 years
No release in over 3 years
Easy encoding is a tool that allows you to encode and decode data.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.13
~> 10.0
~> 3.0
~> 0.45.0
 Project Readme

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.