资源简介
用python实现算术编码,能够实现编码与译码。编码的方法是通过对一篇已知文章求取概率空间后,对待翻译文章进行算术编码,并能够基于已知文章重新进行算术解码。
代码片段和文件信息
import random
def get_probability(char_li):
p_space = {}
chars = set(char_li)
len_char_li = len(char_li)
for char in chars:
times = char_li.count(char)
p_space.update({char: times/len_char_li})
return p_space
def arithmetic_encode(p_space origin_code):
code = 0
arit = 1
keys = list(p_space.keys())
for char in origin_code:
index = keys.index(char)
pd = 0
for i in range(index):
pd = pd + p_space[keys[i]]
code = code + pd * arit
arit = p_space[char] * arit
# print(char code arit)
interval = [code code + arit]
return interval
def arithmetic_decode(p_space interval l):
decoding_code = []
arit_range = {}
c = 0
for char in p_space.keys():
a = p_space[char]
arit_range.update({char: (c c+a)})
c += a
code = random.uniform(interval[0] interval[1])
ori_range = arit_range.copy()
while len(decoding_code) < l:
for char rang in ori_range.items():
if rang[0] < code < rang[1]:
decoding_code.append(char)
c = rang[0]
for ch ran in arit_range.items():
a = ran[1] - ran[0]
ori_range.update({ch: (c
c + a * (rang[1] - rang[0]))})
c = c + a * (rang[1] - rang[0])
break
return decoding_code
def main():
info_source = [0 1 1 2 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6]
origin_txt = [6 5 0 1 1]
# origin_txt = “dead!“
p_space = get_probability(info_source)
# p_space = {“a“: 0.2 “b“: 0.1 “c“: 0.1 “d“: 0.3 “e“: 0.2 “!“: 0.1}
interval = arithmetic_encode(p_space origin_txt)
with open(“encoding.txt“ “w“ encoding=“gbk“) as f:
f.write(“信源为: “ + “ “.join([str(i) for i in info_source]) + “\n“)
f.write(“源码为: “ + ““.join([str(i) for i in origin_txt]) + “\n“)
f.write(“算术编码区间为: “ + str(interval) + “\n“)
decoding = arithmetic_decode(p_space interval len(origin_txt))
with open(“decoding.txt“ “w“ encoding=“gbk“) as f:
f.write(“信源为: “ + “ “.join([str(i) for i in info_source]) + “\n“)
f.write(“算术编码区间为: “ + str(interval) + “\n“)
f.write(“译文为: “ + “ “.join([str(i) for i in decoding]) + “\n“)
print(“对数字符号编码结果为: “ interval)
print(“对数字符号译码结果为: “ decoding)
# 此处的origin_txt 的内容可以更改,但是其字符必须出于Origin.txt 一文
origin_txt = “hello World“
with open(“Origin.txt“ “r“ encoding=“gbk“) as text:
# print(text.readline())
ori = text.readline()
# print(len(ori))
p_space = get_probability(ori)
interval = arithmetic_encode(p_space origin_txt)
with open(“ArithC.txt“ “w“) as text:
# print(len(txt) txt)
text.write(“基于来自文章‘Origin.txt‘的概率空间对字符 %s 的编码为: “ % str(o
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-11-22 08:58 Arithmetic_Code\
目录 0 2019-11-22 08:58 Arithmetic_Code\.idea\
文件 444 2019-10-30 10:14 Arithmetic_Code\.idea\Arithmetic_Code.iml
文件 491 2019-11-22 08:34 Arithmetic_Code\.idea\encodings.xm
文件 297 2019-10-30 10:14 Arithmetic_Code\.idea\misc.xm
文件 289 2019-10-30 10:09 Arithmetic_Code\.idea\modules.xm
文件 10044 2019-11-22 08:44 Arithmetic_Code\.idea\workspace.xm
文件 105 2019-11-22 08:36 Arithmetic_Code\ArithC.txt
文件 3641 2019-11-22 08:35 Arithmetic_Code\arithmetic.py
文件 137 2019-11-22 08:36 Arithmetic_Code\decoding.txt
文件 133 2019-11-22 08:36 Arithmetic_Code\encoding.txt
文件 763 2019-10-25 10:18 Arithmetic_Code\Origin.txt
文件 110 2019-11-22 08:36 Arithmetic_Code\Translation.txt
评论
共有 条评论