Guest User

Untitled

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. following the algorithm given above
  2. #import std
  3. #import nat
  4. #import flo
  5.  
  6. code_table = # takes a training dataset to a table <char: code...>
  7.  
  8. -+
  9. *^ ~&v?\~&iNC @v ~&t?\~&h ~&plrDSLrnPlrmPCAS/'01',
  10. ~&itB->h fleq-<&d; ^C\~&tt @hthPX ^V\~&lrNCC plus@bd,
  11. ^V(div@rrPlX,~&rlNVNC)^*D(plus:-0.@rS,~&)+ *K2 ^/~&h float+ length+-
  12.  
  13. #cast %csAL
  14.  
  15. table = code_table 'this is an example for huffman encoding'
  16. a quick walk through the code starting from the bottom:
  17. *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
  18. ^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
  19. ~&itB->h while the list contains more than one tree, do the following, and when done take the head of the list
  20. fleq-<&d; sort the trees in increasing order by their roots
  21. ^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
  22. *^ visit the following function on each node of the tree obtained from the loop and propagate the results upward from the leaves
  23. ~&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)
  24. @v ~&t?\~&h if there is only a single subtree, propagate the result already obtained for it
  25. ~&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