-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitter_train.py
249 lines (193 loc) · 8.65 KB
/
splitter_train.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def last(src, morph, tag, max_len=200):
src_bufs = src.rsplit(" ")
morph_bufs = morph.rsplit(" ")
assert len(src_bufs) == len(morph_bufs), f"src, morph len different"
# append deliimter
for i, (src_buf, morph_buf) in enumerate(zip(src_bufs, morph_bufs)):
if src_bufs[-1] != src_buf:
src_bufs[i] += " "
morph_bufs[i] += " "
src_bufs2 = []
morph_bufs2 = []
src_result = []
morph_result = []
# splitted by space unit
for src_buf, morph_buf in zip(src_bufs, morph_bufs):
src_bufs2.append(src_buf)
morph_bufs2.append(morph_buf)
if len("".join(src_bufs2)) > (max_len / 2) or len("".join(morph_bufs2)) > (max_len / 2):
src_result.append("".join(src_bufs2))
morph_result.append("".join(morph_bufs2))
src_bufs2 = []
morph_bufs2 = []
src_result.append("".join(src_bufs2))
morph_result.append("".join(morph_bufs2))
src_bufs = src_result
morph_bufs = morph_result
if src_bufs[-1] == "" and morph_bufs[-1] == "":
src_bufs.pop()
morph_bufs.pop()
assert "".join(src_bufs) == src, f"src not splitted"
assert "".join(morph_bufs) == morph, f"morph not splitted"
# append tag
tag_bufs = []
tag_origin = tag
tag = tag.split(" ")
for morph_buf in morph_bufs:
tag_buf = []
for i in range(len(morph_buf)):
tag_buf.append(tag.pop(0))
tag_buf = " ".join(tag_buf)
tag_bufs.append(tag_buf)
assert " ".join(tag_bufs) == tag_origin, f"wrong tag"
return src_bufs, morph_bufs, tag_bufs
def left_token(src, morph, tag, src_token, morph_token, max_len=200):
src_bufs = src.rsplit(src_token)
morph_bufs = morph.rsplit(morph_token)
# append delimiter
for i, (src_buf, morph_buf) in enumerate(zip(src_bufs, morph_bufs)):
if src_bufs[0] != src_buf:
src_bufs[i] = src_token + src_bufs[i]
morph_bufs[i] = morph_token + morph_bufs[i]
# when first is empty
if src_bufs[0] == "" and morph_bufs[0] == "":
src_bufs.pop(0)
morph_bufs.pop(0)
assert "".join(src_bufs) == src, f"src not splitted"
assert "".join(morph_bufs) == morph, f"morph not splitted"
# append tag
tag_bufs = []
tag_origin = tag
tag = tag.split(" ")
for morph_buf in morph_bufs:
tag_buf = []
for i in range(len(morph_buf)):
tag_buf.append(tag.pop(0))
tag_buf = " ".join(tag_buf)
tag_bufs.append(tag_buf)
assert " ".join(tag_bufs) == tag_origin, f"wrong tag"
return src_bufs, morph_bufs, tag_bufs
def token(src, morph, tag, src_token, morph_token, max_len=200):
src_bufs = src.rsplit(src_token)
morph_bufs = morph.rsplit(morph_token)
assert len(src_bufs) == len(morph_bufs), f"src, morph len different"
# append delimter
for i, (src_buf, morph_buf) in enumerate(zip(src_bufs, morph_bufs)):
if src_bufs[-1] != src_buf:
src_bufs[i] += src_token
morph_bufs[i] += morph_token
assert "".join(src_bufs) == src, f"src not splitted"
assert "".join(morph_bufs) == morph, f"morph not splitted"
# append tag
tag_bufs = []
tag_origin = tag
tag = tag.split(" ")
for morph_buf in morph_bufs:
tag_buf = []
for i in range(len(morph_buf)):
tag_buf.append(tag.pop(0))
tag_buf = " ".join(tag_buf)
tag_bufs.append(tag_buf)
assert " ".join(tag_bufs) == tag_origin, f"wrong tag"
return src_bufs, morph_bufs, tag_bufs
def split(src : str, morph : str, tag : str, max_len = 200):
src_spl = []
morph_spl = []
tag_spl = []
if len(src) < max_len and len(morph) < max_len :
src_spl.append(src)
morph_spl.append(morph)
tag_spl.append(tag)
else :
src_bufs, morph_bufs, tag_bufs = token(src, morph, tag, src_token=".\"", morph_token=".+\"", max_len=max_len)
for _ in range(len(src_bufs)):
src_cur = src_bufs.pop(0)
morph_cur = morph_bufs.pop(0)
tag_cur = tag_bufs.pop(0)
if len(src_cur) < max_len and len(morph_cur) < max_len:
src_bufs.append(src_cur)
morph_bufs.append(morph_cur)
tag_bufs.append(tag_cur)
else :
src_bufs2, morph_bufs2, tag_bufs2 = token(src_cur, morph_cur, tag_cur, "──", "─+─", max_len=max_len)
for src_buf2, morph_buf2, tag_buf2 in zip(src_bufs2, morph_bufs2, tag_bufs2):
src_bufs.append(src_buf2)
morph_bufs.append(morph_buf2)
tag_bufs.append(tag_buf2)
for _ in range(len(src_bufs)):
src_cur = src_bufs.pop(0)
morph_cur = morph_bufs.pop(0)
tag_cur = tag_bufs.pop(0)
if len(src_cur) < max_len and len(morph_cur) < max_len:
src_bufs.append(src_cur)
morph_bufs.append(morph_cur)
tag_bufs.append(tag_cur)
else :
src_bufs2, morph_bufs2, tag_bufs2 = token(src_cur, morph_cur, tag_cur, src_token=", ", morph_token=", ", max_len=max_len)
for src_buf2, morph_buf2, tag_buf2 in zip(src_bufs2, morph_bufs2, tag_bufs2):
src_bufs.append(src_buf2)
morph_bufs.append(morph_buf2)
tag_bufs.append(tag_buf2)
for _ in range(len(src_bufs)):
src_cur = src_bufs.pop(0)
morph_cur = morph_bufs.pop(0)
tag_cur = tag_bufs.pop(0)
if len(src_cur) < max_len and len(morph_cur) < max_len:
src_bufs.append(src_cur)
morph_bufs.append(morph_cur)
tag_bufs.append(tag_cur)
else :
src_bufs2, morph_bufs2, tag_bufs2 = left_token(src_cur, morph_cur, tag_cur, "▲ ", "▲ ", max_len=max_len)
for src_buf2, morph_buf2, tag_buf2 in zip(src_bufs2, morph_bufs2, tag_bufs2):
src_bufs.append(src_buf2)
morph_bufs.append(morph_buf2)
tag_bufs.append(tag_buf2)
for _ in range(len(src_bufs)):
src_cur = src_bufs.pop(0)
morph_cur = morph_bufs.pop(0)
tag_cur = tag_bufs.pop(0)
if len(src_cur) < max_len and len(morph_cur) < max_len:
src_bufs.append(src_cur)
morph_bufs.append(morph_cur)
tag_bufs.append(tag_cur)
else :
src_bufs2, morph_bufs2, tag_bufs2 = token(src_cur, morph_cur, tag_cur, "/ ", "/ ", max_len=max_len)
for src_buf2, morph_buf2, tag_buf2 in zip(src_bufs2, morph_bufs2, tag_bufs2):
src_bufs.append(src_buf2)
morph_bufs.append(morph_buf2)
tag_bufs.append(tag_buf2)
for _ in range(len(src_bufs)):
src_cur = src_bufs.pop(0)
morph_cur = morph_bufs.pop(0)
tag_cur = tag_bufs.pop(0)
if len(src_cur) < max_len and len(morph_cur) < max_len:
src_bufs.append(src_cur)
morph_bufs.append(morph_cur)
tag_bufs.append(tag_cur)
else :
src_bufs2, morph_bufs2, tag_bufs2 = left_token(src_cur, morph_cur, tag_cur, "△ ", "△ ", max_len=max_len)
for src_buf2, morph_buf2, tag_buf2 in zip(src_bufs2, morph_bufs2, tag_bufs2):
src_bufs.append(src_buf2)
morph_bufs.append(morph_buf2)
tag_bufs.append(tag_buf2)
#last
for _ in range(len(src_bufs)):
src_cur = src_bufs.pop(0)
morph_cur = morph_bufs.pop(0)
tag_cur = tag_bufs.pop(0)
if len(src_cur) < max_len and len(morph_cur) < max_len:
src_bufs.append(src_cur)
morph_bufs.append(morph_cur)
tag_bufs.append(tag_cur)
else :
src_bufs2, morph_bufs2, tag_bufs2 = last(src_cur, morph_cur, tag_cur, max_len=max_len)
for src_buf2, morph_buf2, tag_buf2 in zip(src_bufs2, morph_bufs2, tag_bufs2):
src_bufs.append(src_buf2)
morph_bufs.append(morph_buf2)
tag_bufs.append(tag_buf2)
for src_buf, morph_buf, tag_buf in zip(src_bufs, morph_bufs, tag_bufs):
src_spl.append(src_buf)
morph_spl.append(morph_buf)
tag_spl.append(tag_buf)
assert len(src_buf) < max_len and len(morph_buf) < max_len, f"not splitted"
return src_spl, morph_spl, tag_spl