-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdummy_loader.py
136 lines (123 loc) · 3.5 KB
/
dummy_loader.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
from frostings import loader
import numpy as np
def simple_dummy_sample(max_len, max_len_spaces):
# setting variables
elem_len = np.random.choice(int(max_len)) # generates a random len seq
max_spaces = max_len_spaces
# setting holders
space_counter = 0
elem_X = ""
elem_t = ""
# generating output
for _ in range(elem_len):
elem_X += str(np.random.choice(10))
rand = np.random.choice(1)
if np.random.choice(max(int(elem_len/max_spaces), 1)) == 1:
if space_counter < max_spaces:
elem_X += " "
space_counter += 1
elem_t = elem_X # X and t are the same (why it's called "simple")
return elem_X, elem_t
advanced_dict = []
advanced_dict.append({
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
'7': 'seven',
'8': 'eight',
'9': 'nine',
' ': ' '
})
advanced_dict.append({
'0': 'nul',
'1': 'en',
'2': 'to',
'3': 'tre',
'4': 'fire',
'5': 'fem',
'6': 'seks',
'7': 'syv',
'8': 'otte',
'9': 'ni',
' ': ' '
})
make_caps = {
'z': 'Z',
'e': 'E',
'r': 'R',
'o': 'O',
'n': 'N',
't': 'T',
'w': 'W',
'h': 'H',
'f': 'F',
'u': 'U',
'i': 'I',
'v': 'V',
's': 'S',
'x': 'X',
'g': 'G',
'l': 'L',
'k': 'K',
'y': 'Y',
'm': 'M',
' ': ' ',
}
def dummy_sampler(max_len, max_len_spaces, sampler='normal'):
assert sampler in ['normal', 'talord', 'talord_caps1', 'talord_caps2',
'talord_caps3']
elem_X, elem_t = simple_dummy_sample(max_len, max_len_spaces)
if sampler == 'normal':
pass # base case
else:
# Turning it to danish/english (list -> join)
elem_X = ''.join([advanced_dict[0][e] for e in elem_X])
elem_t = ''.join([advanced_dict[1][e] for e in elem_t])
if sampler == 'talord_caps1' or sampler=="talord_caps3":
holder = [] # hacked around str assignment
for e in elem_X:
if np.random.choice(2):
holder.append(make_caps[e])
else:
holder.append(e)
elem_X = ''.join(holder)
if sampler == 'talord_caps2' or sampler=="talord_caps3":
if np.random.choice(4) == 0:
elem_t = ''.join([make_caps[e] for e in elem_t])
return elem_X, elem_t
class DummySampleGenerator(loader.SampleGenerator):
"""Generates a sample from a dummy sampler
Extends SampleGenerator
"""
def __init__(self, make_dummy_sample=dummy_sampler,
max_len=4, max_len_spaces=1, sampler='normal'):
self.make_dummy_sample = make_dummy_sample
self.max_len = max_len
self.max_len_spaces = max_len_spaces
self.sampler = sampler
def gen_sample(self):
while True:
yield self.make_dummy_sample(
self.max_len, self.max_len_spaces, self.sampler)
if __name__ == '__main__':
samplers = ['normal', 'talord', 'talord_caps1', 'talord_caps2',
'talord_caps3']
for sampler in samplers:
print
print('@@@@@')
print(sampler)
print('-----')
dummy_sample_gen = DummySampleGenerator(
make_dummy_sample = dummy_sampler,
max_len = 4, max_len_spaces = 1,
sampler=sampler)
count = 0
for sample in dummy_sample_gen.gen_sample():
print(sample)
count += 1
if count > 5:
break