'''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
- 粉丝: 93
- 资源: 1万+
最新资源
- 4b070水果蔬菜商城_springboot+vue0.zip
- 基于模糊PID的水下航行器运动控制系统研究 1.适用软件Matlab 2016b及以上 2.课程报告6500字左右共16页 3.课程报告+小报告+仿真+仿真视频 4.请结合以下图片
- 4b065校园朋友圈_springboot+vue0.zip
- 4b047北部湾地区助农平台_springboot+vue.zip
- 4b071郑州旅游景点智能推荐系统_springboot+vue0.zip
- 4b046基于SpringBoot的茶叶商城系统的设计与实现_vue.zip
- 4b045攀枝花水果在线销售系统_springboot+vue.zip
- 4b051基于SpringBoot的农产品电商平台_vue.zip
- 4b048.凉州区助农惠农服务平台_springboot+vue.zip
- 4b074高校实验室预约系统_springboot+vue0.zip
- 4b049基于SpringBoot的游戏账号交易系统的设计与实现_vue.zip
- 4b076酒店点餐管理系统_springboot+vue0.zip
- shp文件编辑器,用VB6编写的,2025年新年礼物,祝大家新年快乐,万事如意
- 利用窄刻槽金属光栅实现石墨烯双通道吸收增强-comsol模型
- 4b053校园数字化图书馆系统_springboot+vue.zip
- 2-去除应用边框强制窗口最大化工具
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈