'''Copyright (c) 2013, Ceyhun Cakar
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by the <organization>.
4. Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
import sys
hidden_chars = {' ': 'SPACE', '\n': 'LF', '\r': 'CR', '\t': 'TAB'}
class Huffman:
def __init__(self, probabilities):
sorted_list = sorted([(v, k) for k, v in probabilities])
self.characters = [[val[1]] for val in sorted_list]
self.probabilities = [val[0] for val in sorted_list]
self.codes = [['']] * len(self.probabilities)
self.isReady = False
def displayAlphabet(self):
if self.isReady:
max_code_size = max([len(code) for code in self.codes[0]])
print " -------------------" + "-"*max_code_size
print "| New Alphabet " + " "*max_code_size + "|"
print " -------------------" + "-"*max_code_size
print "| Letter | Code" + " "*max_code_size + "|"
print " -------------------" + "-"*max_code_size
for (ch, code) in zip(self.characters[0], self.codes[0]):
temp_c, shift_size = ch, 4
if ch in hidden_chars:
temp_c, shift_size = hidden_chars[ch], 4- len(hidden_chars[ch])
print "| %s " % temp_c + ' '*shift_size + " | %s " % code + (" "*(max_code_size - len(code))) + "|"
print " -------------------" + "-"*max_code_size
else:
print "Huffman Alphabet is NOT READY"
def combine(self):
least_probable_first = self.characters[0], self.probabilities[0], self.codes[0]
least_probable_second = self.characters[1], self.probabilities[1], self.codes[1]
self.characters, self.probabilities, self.codes = \
self.characters[2:], self.probabilities[2:], self.codes[2:]
least_probable_group = [least_probable_first[0]+least_probable_second[0],
least_probable_first[1]+least_probable_second[1],
['0' + it for it in least_probable_first[2]] + ['1' + it for it in least_probable_second[2]]]
self.characters = [least_probable_group[0]] + self.characters
self.probabilities = [least_probable_group[1]] + self.probabilities
self.codes = [least_probable_group[2]] + self.codes
combined = sorted(zip(self.probabilities, self.characters, self.codes))
self.probabilities = [val[0] for val in combined]
self.characters = [val[1] for val in combined]
self.codes = [val[2] for val in combined]
self.isReady = (len(combined) == 1)
return self.isReady
if __name__ == '__main__':
inp = 'karaparass'
num_of_occurrence = {}
for ch in inp:
if not ch in num_of_occurrence.keys():
num_of_occurrence[ch] = 1
else:
num_of_occurrence[ch] += 1
probs = {k:(v*1.0)/sum(num_of_occurrence.values()) \
for k,v in num_of_occurrence.items()}
huff = Huffman(probs.items())
#huff.display()
while not huff.combine():
pass
huff.displayAlphabet()
Kinonoyomeo
- 粉丝: 91
- 资源: 1万+
最新资源
- (源码)基于Arduino和Firebase的智能家庭管理系统NodeSmartHome.zip
- (源码)基于C++的East Zone DSTADSO Robotics Challenge 2019机器人控制系统.zip
- (源码)基于Arduino平台的焊接站控制系统.zip
- (源码)基于ESPboy系统的TZXDuino WiFi项目.zip
- (源码)基于Java的剧场账单管理系统.zip
- (源码)基于Java Swing的船只资料管理系统.zip
- (源码)基于Python框架的模拟购物系统.zip
- (源码)基于C++的图书管理系统.zip
- (源码)基于Arduino的简易温度显示系统.zip
- (源码)基于Arduino的智能电动轮椅系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈