-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTextFilter.py
executable file
·152 lines (124 loc) · 4.14 KB
/
TextFilter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
"""
>>> vowel_counter = CharCounter()
>>> vowel_counter("dog fish and cat fish", "aeiou") # returns: 5
5
>>> vowel_counter("there's too many junk lawsuits suing too many doctors",
... "aeiouAEIOU")
16
>>> text = "The Title\\n=========\\nThe text\\n"
>>> rle_encoder = RunLengthEncode()
>>> rle = rle_encoder(text)
>>> rle, len(rle), len(text)
(b'The Title\\n\\x00\\t=\\nThe text\\n', 23, 29)
>>> rle_decoder = RunLengthDecode()
>>> string = rle_decoder(rle)
>>> string, len(string), string == text
('The Title\\n=========\\nThe text\\n', 29, True)
>>> text = "=============++++++++++--------------"
>>> rle = rle_encoder(text)
>>> rle, len(rle), len(text)
(b'\\x00\\r=\\x00\\n+\\x00\\x0e-', 9, 37)
>>> rle_decoder(rle) == text
True
"""
import abc
class TextFilter(metaclass=abc.ABCMeta):
@abc.abstractproperty
def is_transformer(self):
raise NotImplementedError()
@abc.abstractmethod
def __call__(self):
raise NotImplementedError()
class CharCounter(TextFilter):
@property
def is_transformer(self):
return False
def __call__(self, text, chars):
count = 0
for c in text:
if c in chars:
count += 1
return count
class RunLengthEncode(TextFilter):
@property
def is_transformer(self):
return True
def __call__(self, utf8_string):
byte = None
count = 0
binary = bytearray()
for b in utf8_string.encode("utf8"):
if byte is None:
if b == 0:
binary.extend((0, 1, 0))
else:
byte = b
count = 1
else:
if byte == b:
count += 1
if count == 255:
binary.extend((0, count, b))
byte = None
count = 0
else:
if count == 1:
binary.append(byte)
elif count == 2:
binary.extend((byte, byte))
elif count > 2:
binary.extend((0, count, byte))
if b == 0:
binary.extend((0, 1, 0))
byte = None
count = 0
else:
byte = b
count = 1
if count == 1:
binary.append(byte)
elif count == 2:
binary.extend((byte, byte))
elif count > 2:
binary.extend((0, count, byte))
return bytes(binary)
class RunLengthDecode(TextFilter):
@property
def is_transformer(self):
return True
def __call__(self, rle_bytes):
binary = bytearray()
length = None
for b in rle_bytes:
if length == 0:
length = b
elif length is not None:
binary.extend([b for x in range(length)])
length = None
elif b == 0:
length = 0
else:
binary.append(b)
length = None
if length:
binary.extend([b for x in range(length)])
return binary.decode("utf8")
if __name__ == "__main__":
text = "The Story\n=========\n\nOnce upon a time..."
rle_encoder = RunLengthEncode()
rle_text = rle_encoder(text)
rle_decoder = RunLengthDecode()
original_text = rle_decoder(rle_text)
assert text == original_text
import doctest
doctest.testmod()