-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use python script for generating list.eval and list.train.
This decreases dependency on `bc` program and solves the problem with EOL on Windows.
- Loading branch information
Showing
2 changed files
with
40 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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.writelines(lines[:split_point]) | ||
f2.writelines(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) |