Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- following the algorithm given above
- #import std
- #import nat
- #import flo
- code_table = # takes a training dataset to a table <char: code...>
- -+
- *^ ~&v?\~&iNC @v ~&t?\~&h ~&plrDSLrnPlrmPCAS/'01',
- ~&itB->h fleq-<&d; ^C\~&tt @hthPX ^V\~&lrNCC plus@bd,
- ^V(div@rrPlX,~&rlNVNC)^*D(plus:-0.@rS,~&)+ *K2 ^/~&h float+ length+-
- #cast %csAL
- table = code_table 'this is an example for huffman encoding'
- a quick walk through the code starting from the bottom:
- *K2 ^/~&h float+ length compute character frequencies by partitioning the input list of characters by equality, and transforming each equivalence class to a pair containing its member and its cardinality represented as a floating point number
- ^V(div@rrPlX,~&rlNVNC)^*D(plus:-0.@rS,~&) construct a list of unary trees, one for each character class, with its normalized frequency in the root, and the character in the leaf
- ~&itB->h while the list contains more than one tree, do the following, and when done take the head of the list
- fleq-<&d; sort the trees in increasing order by their roots
- ^C\~&tt @hthPX ^V\~&lrNCC plus@bd change the first two trees in the sorted list to a single binary tree whose root is the sum of their roots
- *^ visit the following function on each node of the tree obtained from the loop and propagate the results upward from the leaves
- ~&v?\~&iNC if the node is a leaf, construct a singleton list containing the pair of its root (a character) and the empty string (of bits)
- @v ~&t?\~&h if there is only a single subtree, propagate the result already obtained for it
- ~&plrDSLrnPlrmPCAS/'01' otherwise there are two subtrees, hence two lists previously computed results propagating upward, so insert a zero into all of the bit strings in the results on the left, and a one into all the ones on the right, concatenate the left and right results, and propagate the contatenation upward
Add Comment
Please, Sign In to add comment