Add jsonl utility
Browse files- utils/tojsonl.py +21 -0
utils/tojsonl.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import jsonlines
|
2 |
+
import json
|
3 |
+
import argparse
|
4 |
+
import glob
|
5 |
+
|
6 |
+
from tqdm import tqdm
|
7 |
+
|
8 |
+
parser = argparse.ArgumentParser(description='Process data into jsonlines')
|
9 |
+
parser.add_argument('-d', '--data_dir', type=str, help='directory of data', default='data')
|
10 |
+
parser.add_argument('-o', '--output', type=str, help='output file', default='data.jsonl')
|
11 |
+
args = parser.parse_args()
|
12 |
+
|
13 |
+
print(f'Writing to {args.output}')
|
14 |
+
|
15 |
+
files = glob.glob(f'{args.data_dir}/*/*.txt')
|
16 |
+
with jsonlines.open(args.output, mode='w') as writer:
|
17 |
+
for file in tqdm(files):
|
18 |
+
with open(file, 'r', encoding='utf-8') as f:
|
19 |
+
writer.write({'text': f.read()})
|
20 |
+
|
21 |
+
print('Done')
|