generated from mwc/lab_compression
41 lines
1.0 KiB
Python
41 lines
1.0 KiB
Python
import string
|
|
import codecs
|
|
from text_codecs.register import register_codec
|
|
from easybits import Bits
|
|
|
|
vowels = 'aeiouyAEIOUY'
|
|
allowed_chars = string.ascii_letters + string.digits
|
|
|
|
def encode(text):
|
|
|
|
ascii_chars = []
|
|
last_character_was_space = False
|
|
for char in text:
|
|
if char in allowed_chars:
|
|
ascii_chars.append(char)
|
|
last_character_was_space = False
|
|
elif char in string.whitespace and not last_character_was_space:
|
|
ascii_chars.append(' ')
|
|
last_character_was_space = True
|
|
ascii_text = ''.join(ascii_chars)
|
|
|
|
no_vowels = []
|
|
for ch in ascii_text:
|
|
if ch not in vowels:
|
|
no_vowels.append(ch)
|
|
|
|
return Bits(no_vowels, encoding = 'ascii7')
|
|
|
|
|
|
def decode(data):
|
|
bits = Bits(bytes(data))
|
|
text = ""
|
|
for i in range(0, len(bits), 7):
|
|
byte = Bits('0').concat(bits[i:i+7])
|
|
text += Bits(byte).ascii
|
|
return text
|
|
|
|
|
|
register_codec(encode, decode, "noVow7")
|
|
|
|
|