-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_utils.py
More file actions
56 lines (47 loc) · 1.58 KB
/
Copy pathstring_utils.py
File metadata and controls
56 lines (47 loc) · 1.58 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
import numpy as np
def str2label(value, characterToIndex={}, unknown_index=None):
if unknown_index is None:
unknown_index = len(characterToIndex)
label = []
for v in value:
if v not in characterToIndex:
continue
label.append(characterToIndex[v])
return np.array(label, np.uint32)
def label2input(value, num_of_inputs, char_break_interval):
idx1 = len(value) * (char_break_interval + 1) + char_break_interval
idx2 = num_of_inputs + 1
input_data = [[0 for i in range(idx2)] for j in range(idx1)]
cnt = 0
for i in range(char_break_interval):
input_data[cnt][idx2-1] = 1
cnt += 1
for i in range(len(value)):
if value[i] == 0:
input_data[cnt][idx2-1] = 1
else:
input_data[cnt][value[i]-1] = 1
cnt += 1
for i in range(char_break_interval):
input_data[cnt][idx2-1] = 1
cnt += 1
return np.array(input_data)
def label2str(label, indexToCharacter, asRaw, spaceChar = "~"):
string = u""
for i in range(len(label)):
if label[i] == 0:
if asRaw:
string += spaceChar
else:
break
else:
val = label[i]
string += indexToCharacter[val]
return string
def naive_decode(output):
rawPredData = np.argmax(output, axis=1)
predData = []
for i in range(len(output)):
if rawPredData[i] != 0 and not ( i > 0 and rawPredData[i] == rawPredData[i-1] ):
predData.append(rawPredData[i])
return predData, list(rawPredData)