-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalphabet.py
49 lines (39 loc) · 1.59 KB
/
alphabet.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
import collections
import itertools
import string
def alphabet_factory():
char_blank = "*"
char_space = " "
char_apostrophe = "'"
labels = char_blank + char_space + char_apostrophe + string.ascii_lowercase
alphabet = Alphabet(char_blank, char_space, labels)
return alphabet
class Alphabet:
"""Maps characters to integers and vice versa"""
def __init__(self, char_blank, char_space, labels):
self.char_space = char_space
self.char_blank = char_blank
labels = list(labels)
self.length = len(labels)
enumerated = list(enumerate(labels))
flipped = [(sub[1], sub[0]) for sub in enumerated]
d1 = collections.OrderedDict(enumerated)
d2 = collections.OrderedDict(flipped)
self.mapping = {**d1, **d2}
def __len__(self):
return self.length
def text_to_int(self, text):
""" Use a character map and convert text to an integer sequence """
if isinstance(text, list):
return [self.text_to_int(i) for i in text]
else:
return [self.mapping[i] + self.mapping[self.char_blank] for i in text]
def int_to_text(self, labels):
""" Use a character map and convert integer labels to an text sequence """
if len(labels) > 0 and isinstance(labels[0], list):
return [self.int_to_text(label) for label in labels]
else:
string = [self.mapping[i] for i in labels]
string = "".join(i for i, _ in itertools.groupby(string))
string = ''.join(string).replace(self.char_blank, '')
return string