-
Notifications
You must be signed in to change notification settings - Fork 190
/
generate_eval_train.py
39 lines (31 loc) · 1 KB
/
generate_eval_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pathlib
import sys
def split_file(input_file, ratio):
"""
Splits a text file into list.train and list.eval with lines ratio.
"""
if not isinstance(input_file, pathlib.Path):
input_file = pathlib.Path(input_file)
if not input_file.exists():
print(f"'{input_file}' not exists!")
return False
lines = input_file.read_text().splitlines()
split_point = int(ratio * len(lines))
output_dir = input_file.resolve().parent
train_list = pathlib.Path(output_dir, 'list.train')
eval_list = pathlib.Path(output_dir, 'list.eval')
with open(train_list, 'w', newline='\n') as f1, open(
eval_list, 'w', newline='\n'
) as f2:
f1.write('\n'.join(lines[:split_point]))
f2.write('\n'.join(lines[split_point:]))
return True
ratio = 0.95
input_file = None
if len(sys.argv) > 1:
input_file = sys.argv[1]
if len(sys.argv) > 2:
ratio = float(sys.argv[2])
split_file(input_file, ratio)