tdudzik

Untitled

Nov 4th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 9.56 KB | None | 0 0
  1. package patmat
  2.  
  3. import common._
  4.  
  5. /**
  6.   * Assignment 4: Huffman coding
  7.   *
  8.   */
  9. object Huffman {
  10.  
  11.   /**
  12.     * A huffman code is represented by a binary tree.
  13.     *
  14.     * Every `Leaf` node of the tree represents one character of the alphabet that the tree can encode.
  15.     * The weight of a `Leaf` is the frequency of appearance of the character.
  16.     *
  17.     * The branches of the huffman tree, the `Fork` nodes, represent a set containing all the characters
  18.     * present in the leaves below it. The weight of a `Fork` node is the sum of the weights of these
  19.     * leaves.
  20.     */
  21.   abstract class CodeTree
  22.  
  23.   case class Fork(left: CodeTree, right: CodeTree, chars: List[Char], weight: Int) extends CodeTree
  24.  
  25.   case class Leaf(char: Char, weight: Int) extends CodeTree
  26.  
  27.  
  28.   // Part 1: Basics
  29.   def weight(tree: CodeTree): Int = tree match {
  30.     case Fork(_, _, _, weight) => weight
  31.     case Leaf(_, weight) => weight
  32.   }
  33.  
  34.   def chars(tree: CodeTree): List[Char] = tree match {
  35.     case Fork(_, _, chars, _) => chars
  36.     case Leaf(char, _) => List(char)
  37.   }
  38.  
  39.   def makeCodeTree(left: CodeTree, right: CodeTree) =
  40.     Fork(left, right, chars(left) ::: chars(right), weight(left) + weight(right))
  41.  
  42.  
  43.   // Part 2: Generating Huffman trees
  44.  
  45.   /**
  46.     * In this assignment, we are working with lists of characters. This function allows
  47.     * you to easily create a character list from a given string.
  48.     */
  49.   def string2Chars(str: String): List[Char] = str.toList
  50.  
  51.   /**
  52.     * This function computes for each unique character in the list `chars` the number of
  53.     * times it occurs. For example, the invocation
  54.     *
  55.     * times(List('a', 'b', 'a'))
  56.     *
  57.     * should return the following (the order of the resulting list is not important):
  58.     *
  59.     * List(('a', 2), ('b', 1))
  60.     *
  61.     * The type `List[(Char, Int)]` denotes a list of pairs, where each pair consists of a
  62.     * character and an integer. Pairs can be constructed easily using parentheses:
  63.     *
  64.     * val pair: (Char, Int) = ('c', 1)
  65.     *
  66.     * In order to access the two elements of a pair, you can use the accessors `_1` and `_2`:
  67.     *
  68.     * val theChar = pair._1
  69.     * val theInt  = pair._2
  70.     *
  71.     * Another way to deconstruct a pair is using pattern matching:
  72.     *
  73.     * pair match {
  74.     * case (theChar, theInt) =>
  75.     * println("character is: "+ theChar)
  76.     * println("integer is  : "+ theInt)
  77.     * }
  78.     */
  79.   def times(chars: List[Char]): List[(Char, Int)] = {
  80.     chars.groupBy(_.toChar).map(x => (x._1, x._2.length)).toList
  81.   }
  82.  
  83.   /**
  84.     * Returns a list of `Leaf` nodes for a given frequency table `freqs`.
  85.     *
  86.     * The returned list should be ordered by ascending weights (i.e. the
  87.     * head of the list should have the smallest weight), where the weight
  88.     * of a leaf is the frequency of the character.
  89.     */
  90.   def makeOrderedLeafList(freqs: List[(Char, Int)]): List[Leaf] = freqs.sortBy(_._2).map(f => Leaf(f._1, f._2))
  91.  
  92.   /**
  93.     * Checks whether the list `trees` contains only one single code tree.
  94.     */
  95.   def singleton(trees: List[CodeTree]): Boolean = trees.size == 1
  96.  
  97.   /**
  98.     * The parameter `trees` of this function is a list of code trees ordered
  99.     * by ascending weights.
  100.     *
  101.     * This function takes the first two elements of the list `trees` and combines
  102.     * them into a single `Fork` node. This node is then added back into the
  103.     * remaining elements of `trees` at a position such that the ordering by weights
  104.     * is preserved.
  105.     *
  106.     * If `trees` is a list of less than two elements, that list should be returned
  107.     * unchanged.
  108.     */
  109.   def combine(trees: List[CodeTree]): List[CodeTree] = trees match {
  110.     case left :: right :: cs => (makeCodeTree(left, right) :: cs).sortWith((t1, t2) => weight(t1) < weight(t2))
  111.     case _ => trees
  112.   }
  113.  
  114.   /**
  115.     * This function will be called in the following way:
  116.     *
  117.     * until(singleton, combine)(trees)
  118.     *
  119.     * where `trees` is of type `List[CodeTree]`, `singleton` and `combine` refer to
  120.     * the two functions defined above.
  121.     *
  122.     * In such an invocation, `until` should call the two functions until the list of
  123.     * code trees contains only one single tree, and then return that singleton list.
  124.     *
  125.     * Hint: before writing the implementation,
  126.     * - start by defining the parameter types such that the above example invocation
  127.     * is valid. The parameter types of `until` should match the argument types of
  128.     * the example invocation. Also define the return type of the `until` function.
  129.     * - try to find sensible parameter names for `xxx`, `yyy` and `zzz`.
  130.     */
  131.   def until(singleton: List[CodeTree] => Boolean, combine: List[CodeTree] => List[CodeTree])(trees: List[CodeTree]): List[CodeTree] = {
  132.     if (singleton(trees)) trees
  133.     else until(singleton, combine)(combine(trees))
  134.   }
  135.  
  136.   /**
  137.     * This function creates a code tree which is optimal to encode the text `chars`.
  138.     *
  139.     * The parameter `chars` is an arbitrary text. This function extracts the character
  140.     * frequencies from that text and creates a code tree based on them.
  141.     */
  142.   def createCodeTree(chars: List[Char]): CodeTree = until(singleton, combine)(makeOrderedLeafList(times(chars))).head
  143.  
  144.  
  145.   // Part 3: Decoding
  146.  
  147.   type Bit = Int
  148.  
  149.   /**
  150.     * This function decodes the bit sequence `bits` using the code tree `tree` and returns
  151.     * the resulting list of characters.
  152.     */
  153.   def decode(tree: CodeTree, bits: List[Bit]): List[Char] = {
  154.     def helper(tree: List[Bit], bits: List[Char]): List[Char] = match tree {
  155.       case Fork(_, _, _, _) => helper(null, null)
  156.     }
  157.  
  158.     helper(tree)
  159.   }
  160.  
  161.   /**
  162.     * A Huffman coding tree for the French language.
  163.     * Generated from the data given at
  164.     * http://fr.wikipedia.org/wiki/Fr%C3%A9quence_d%27apparition_des_lettres_en_fran%C3%A7ais
  165.     */
  166.   val frenchCode: CodeTree = Fork(Fork(Fork(Leaf('s', 121895), Fork(Leaf('d', 56269), Fork(Fork(Fork(Leaf('x', 5928), Leaf('j', 8351), List('x', 'j'), 14279), Leaf('f', 16351), List('x', 'j', 'f'), 30630), Fork(Fork(Fork(Fork(Leaf('z', 2093), Fork(Leaf('k', 745), Leaf('w', 1747), List('k', 'w'), 2492), List('z', 'k', 'w'), 4585), Leaf('y', 4725), List('z', 'k', 'w', 'y'), 9310), Leaf('h', 11298), List('z', 'k', 'w', 'y', 'h'), 20608), Leaf('q', 20889), List('z', 'k', 'w', 'y', 'h', 'q'), 41497), List('x', 'j', 'f', 'z', 'k', 'w', 'y', 'h', 'q'), 72127), List('d', 'x', 'j', 'f', 'z', 'k', 'w', 'y', 'h', 'q'), 128396), List('s', 'd', 'x', 'j', 'f', 'z', 'k', 'w', 'y', 'h', 'q'), 250291), Fork(Fork(Leaf('o', 82762), Leaf('l', 83668), List('o', 'l'), 166430), Fork(Fork(Leaf('m', 45521), Leaf('p', 46335), List('m', 'p'), 91856), Leaf('u', 96785), List('m', 'p', 'u'), 188641), List('o', 'l', 'm', 'p', 'u'), 355071), List('s', 'd', 'x', 'j', 'f', 'z', 'k', 'w', 'y', 'h', 'q', 'o', 'l', 'm', 'p', 'u'), 605362), Fork(Fork(Fork(Leaf('r', 100500), Fork(Leaf('c', 50003), Fork(Leaf('v', 24975), Fork(Leaf('g', 13288), Leaf('b', 13822), List('g', 'b'), 27110), List('v', 'g', 'b'), 52085), List('c', 'v', 'g', 'b'), 102088), List('r', 'c', 'v', 'g', 'b'), 202588), Fork(Leaf('n', 108812), Leaf('t', 111103), List('n', 't'), 219915), List('r', 'c', 'v', 'g', 'b', 'n', 't'), 422503), Fork(Leaf('e', 225947), Fork(Leaf('i', 115465), Leaf('a', 117110), List('i', 'a'), 232575), List('e', 'i', 'a'), 458522), List('r', 'c', 'v', 'g', 'b', 'n', 't', 'e', 'i', 'a'), 881025), List('s', 'd', 'x', 'j', 'f', 'z', 'k', 'w', 'y', 'h', 'q', 'o', 'l', 'm', 'p', 'u', 'r', 'c', 'v', 'g', 'b', 'n', 't', 'e', 'i', 'a'), 1486387)
  167.  
  168.   /**
  169.     * What does the secret message say? Can you decode it?
  170.     * For the decoding use the 'frenchCode' Huffman tree defined above.
  171.     **/
  172.   val secret: List[Bit] = List(0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1)
  173.  
  174.   /**
  175.     * Write a function that returns the decoded secret
  176.     */
  177.   def decodedSecret: List[Char] = decode(frenchCode, secret)
  178.  
  179.  
  180.   // Part 4a: Encoding using Huffman tree
  181.  
  182.   /**
  183.     * This function encodes `text` using the code tree `tree`
  184.     * into a sequence of bits.
  185.     */
  186.   def encode(tree: CodeTree)(text: List[Char]): List[Bit] = ???
  187.  
  188.   // Part 4b: Encoding using code table
  189.  
  190.   type CodeTable = List[(Char, List[Bit])]
  191.  
  192.   /**
  193.     * This function returns the bit sequence that represents the character `char` in
  194.     * the code table `table`.
  195.     */
  196.   def codeBits(table: CodeTable)(char: Char): List[Bit] = ???
  197.  
  198.   /**
  199.     * Given a code tree, create a code table which contains, for every character in the
  200.     * code tree, the sequence of bits representing that character.
  201.     *
  202.     * Hint: think of a recursive solution: every sub-tree of the code tree `tree` is itself
  203.     * a valid code tree that can be represented as a code table. Using the code tables of the
  204.     * sub-trees, think of how to build the code table for the entire tree.
  205.     */
  206.   def convert(tree: CodeTree): CodeTable = ???
  207.  
  208.   /**
  209.     * This function takes two code tables and merges them into one. Depending on how you
  210.     * use it in the `convert` method above, this merge method might also do some transformations
  211.     * on the two parameter code tables.
  212.     */
  213.   def mergeCodeTables(a: CodeTable, b: CodeTable): CodeTable = ???
  214.  
  215.   /**
  216.     * This function encodes `text` according to the code tree `tree`.
  217.     *
  218.     * To speed up the encoding process, it first converts the code tree to a code table
  219.     * and then uses it to perform the actual encoding.
  220.     */
  221.   def quickEncode(tree: CodeTree)(text: List[Char]): List[Bit] = ???
  222. }
Advertisement
Add Comment
Please, Sign In to add comment