一、Run-Length Encoding
这种数据压缩方法背后的思想是:如果一个数据项d在输入流中连续出现n次,则用单对nd替换n次出现。然后,一个数据项的连续出现被称为运行长度为n,这种进行数据压缩的方法被称为运行长度编码或RLE。我们首先将这个想法应用于文本压缩,然后再应用于图像压缩。
二、文本游程编码
1. 文本游程编码方法
在每次重复之前使用一个特殊的转义字符。我们使用字符@作为转义字符,那么字符串可以明确地解压缩@,将重复且连续出现多次的字符使用@记录并记录字符出现的次数,假设只有当字符串重复的字符达到四个才记录来避免压缩后的字符串长度变长的情况。
2. 文本游程编码代码实现
class Run_Length_Encoding:
def __init__(self):
self.lowbit = '0'
self.highbit = '1'
self.compression_factor_text = 4
self.escape_character = '@'
def run_length_encoding_text(self, input: str):
if len(input) == 0:
return ''
outstring = ''
count = 1
for alphabet_index in range(0, len(input)):
if alphabet_index + 1 < len(input) and input[alphabet_index] == input[alphabet_index + 1]:
count += 1
else:
if count < self.compression_factor_text:
for p in range(count):
outstring = outstring + input[alphabet_index]
else:
outstring = outstring + input[alphabet_index] + self.escape_character + str(
count - self.compression_factor_text)
count = 1
return outstring
input = aaaaaaaaaabbbbbbbcccccdd
output : a@6b@3c@1dd
总结
以上就是今天要讲的内容,本文仅仅简单介绍了文本游程编码的相关定义和其中简单代码实的实现方法。
参考
[1] Salomon, David, and Giovanni Motta. Handbook of data compression. London; New York: Springer, 2010.
本文部分参考自《Handbook of Data Compression 》,该内容仅供学习研究之用,如若喜欢请支持购买正品书籍。
转载本文请联系原作者获取授权,同时请注明本文来自池鱼醉故渊博客。