|
3 | 3 |
|
4 | 4 | """Python implementation of Huffman Coding
|
5 | 5 |
|
6 |
| -This program implements the huffman coding used for encoding a group of symbols |
7 |
| -by a preface |
| 6 | +This script test the implementation software of Huffman Coding at \'main.py\'. |
8 | 7 | """
|
9 | 8 | __author__ = 'Ismael Taboada'
|
10 | 9 | __version__= '1.0'
|
|
22 | 21 | LOG_FILE = 'log.csv'
|
23 | 22 | TEST = "this is an example for huffman encoding"
|
24 | 23 |
|
| 24 | +"""Test for Graphviz software |
| 25 | +""" |
25 | 26 | try:
|
26 | 27 | dot = Digraph()
|
27 | 28 | except Exception as e:
|
28 | 29 | raise
|
29 | 30 | print "Error: Graphviz software not found.\nPlease install Graphviz software on your computer.(http://www.graphviz.org/Download.php)"
|
30 | 31 | exit(1)
|
31 | 32 |
|
32 |
| - |
| 33 | +"""User input |
| 34 | +""" |
33 | 35 | txtin = raw_input("Write some symbols(blank for sample case):")
|
34 | 36 | txtin = TEST if txtin=="" else txtin
|
35 | 37 | txtout = txtin
|
| 38 | + |
| 39 | +"""Extract frecuency of each symbol of set |
| 40 | +""" |
36 | 41 | symb2freq = defaultdict(int)
|
37 | 42 | for ch in txtin:
|
38 | 43 | symb2freq[ch] += 1
|
39 | 44 |
|
| 45 | +"""Implementation of Huffman Algorithm |
| 46 | +""" |
40 | 47 | start = time.time()
|
41 | 48 | huff = HuffmanCoding()
|
42 | 49 | huff.encode(symb2freq)
|
43 | 50 | end = time.time()
|
44 | 51 | time_lapse = end - start
|
| 52 | + |
| 53 | +"""Conversion from Huffman Coding Tree to Coding table |
| 54 | +""" |
45 | 55 | coding_table = huff.tree_to_table()
|
46 | 56 |
|
| 57 | +"""Outputs |
| 58 | +""" |
47 | 59 | print "Codes table"
|
48 | 60 | print "Symbol\tFrec\tCode"
|
49 | 61 | for coding in coding_table:
|
|
55 | 67 | print "\nText input:",txtin
|
56 | 68 | print "Text output:",txtout
|
57 | 69 |
|
| 70 | +"""Huffman tree Graphviz visualization |
| 71 | +""" |
58 | 72 | dot = huff.tree_to_graph()
|
59 | 73 | print "\nDiagram saved at: ",DIA_FILE+'.png'
|
60 | 74 | dot.render(DIA_FILE, view=DEBUG)
|
61 | 75 |
|
| 76 | + |
| 77 | +"""Log of input's size and execution time |
| 78 | +""" |
62 | 79 | log_exits = os.path.isfile(LOG_FILE)
|
63 | 80 |
|
64 | 81 | with open(LOG_FILE, 'ab') as csvfile:
|
|
0 commit comments