-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathfake_data.py
73 lines (62 loc) · 1.67 KB
/
fake_data.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
"""生成一些虚假数据
"""
import random
import numpy as np
from word_sequence import WordSequence
def generate(max_len=10, size=1000, same_len=False, seed=0):
"""生成虚假数据
"""
dictionary = {
'a': '1',
'b': '2',
'c': '3',
'd': '4',
'aa': '1',
'bb': '2',
'cc': '3',
'dd': '4',
'aaa': '1',
}
if seed is not None:
random.seed(seed)
input_list = sorted(list(dictionary.keys()))
x_data = []
y_data = []
for _ in range(size):
a_len = int(random.random() * max_len) + 1
x = []
y = []
for _ in range(a_len):
word = input_list[int(random.random() * len(input_list))]
x.append(word)
y.append(dictionary[word])
if not same_len:
if y[-1] == '2':
y.append('2')
elif y[-1] == '3':
y.append('3')
y.append('4')
x_data.append(x)
y_data.append(y)
ws_input = WordSequence()
ws_input.fit(x_data)# + y_data)
# ws_target = ws_input
ws_target = WordSequence()
ws_target.fit(y_data)
return x_data, y_data, ws_input, ws_target
def test():
"""测试自身"""
x_data, y_data, ws_input, ws_target = generate()
print(len(x_data))
assert len(x_data) == 1000
print(len(y_data))
assert len(y_data) == 1000
print(np.max([len(x) for x in x_data]))
assert np.max([len(x) for x in x_data]) == 10
print(len(ws_input))
assert len(ws_input) == 14
print(len(ws_target))
assert len(ws_target) == 9
print('done')
if __name__ == '__main__':
test()