forked from ZhenYangIACAS/unsupervised-NMT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shuffle.py
44 lines (26 loc) · 753 Bytes
/
shuffle.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
import os
import sys
import random
from tempfile import mkstemp
from subprocess import call
def main(files):
tf_os, tpath = mkstemp()
tf = open(tpath, 'w')
fds = [open(ff) for ff in files]
for l in fds[0]:
lines = [l.strip()] + [ff.readline().strip() for ff in fds[1:]]
print >>tf, "|||".join(lines)
[ff.close() for ff in fds]
tf.close()
tf = open(tpath, 'r')
lines = tf.readlines()
random.shuffle(lines)
fds = [open(ff+'.shuf','w') for ff in files]
for l in lines:
s = l.strip().split('|||')
for ii, fd in enumerate(fds):
print >>fd, s[ii]
[ff.close() for ff in fds]
os.remove(tpath)
if __name__ == '__main__':
main(sys.argv[1:])