-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCasterModule.py
More file actions
67 lines (58 loc) · 1.93 KB
/
Copy pathCasterModule.py
File metadata and controls
67 lines (58 loc) · 1.93 KB
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
from CONSTS import *
import numpy as np
class Caster:
'''
Caster()
converts strings to id maps and vice versa
'''
# str_to_map()
# Converts a string to character mapping
# returns: a list of integers in format [<s>, string, <e>]
def str_to_map(self, string, showInvalid=False):
string = string.lower()
mapping = [START_ID]
for char in string:
charID = ord(char) - 96 # Will form alphabet into a:1 - z:26, space:32
if charID >= 1 and charID <= 26:
mapping.append(charID)
elif charID == -64:
mapping.append(SPACE_ID)
elif showInvalid:
print(f"WARNING: Invalid character ({char}:{charID}) processed. Was not encoded but did appear")
mapping.append(END_ID)
return np.array(mapping)
# map_to_str()
# Converts a character mapping to a string
def map_to_str(self, idMap, showInvalid=False):
mapping = []
for charID in idMap:
if charID == START_ID:
mapping.append('<s>')
elif charID == END_ID:
mapping.append('<e>')
elif charID == SPACE_ID:
mapping.append(' ')
elif charID >= 1 and charID <= 26:
mapping.append(chr(charID + 96))
elif showInvalid:
print(f"WARNING: Invalid character ({chr(charID)}:{charID}) processed. Was not encoded but did appear")
return np.array(mapping)
# ctc_strip()
# Cleans a character mapping according to the ctc loss function
# rules so a loss value can be calculated
def ctc_strip(self, idMap):
mapping, lastChar, i = [], '', 0
for curChar in idMap:
if curChar != lastChar:
lastChar = curChar
if curChar != BLANK_ID:
mapping.append(curChar)
return np.array(mapping)
# padded_map()
# Converts to a string mapping and fills the rest with blank chars
# returns: a list of integers in format [<s>, string, <e>, 0, ..., 0], label length
def padded_str_to_map(self, string, padLen):
mapping = np.zeros(padLen)
rawMap = self.str_to_map(string)
mapping[:rawMap.shape[0]] = rawMap
return mapping, np.count_nonzero(mapping)