Sebastien Montella commited on
Commit
053885b
1 Parent(s): af9b304

json to jsonl

Browse files
Files changed (1) hide show
  1. train_val_test_split.py +31 -12
train_val_test_split.py CHANGED
@@ -35,10 +35,29 @@ def save_json(data, path):
35
  None
36
 
37
  """
38
-
39
  json.dump(data, open(path, 'w+', encoding='utf-8'))
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def get_types(xml):
43
  """ Extract the different transformation types.
44
 
@@ -300,28 +319,28 @@ def split(args):
300
  if args.split_criteria == 'random':
301
  train, val, test = random_split(pairs, args.training_ratio)
302
  os.makedirs(f'{args.out_dir}/{version}/random_split/', exist_ok=True)
303
- save_json(train, f'{args.out_dir}/{version}/random_split/train.json')
304
- save_json(val, f'{args.out_dir}/{version}/random_split/val.json')
305
- save_json(test, f'{args.out_dir}/{version}/random_split/test.json')
306
 
307
  elif args.split_criteria == 'transformations':
308
  seen_transformations_train, seen_transformations_val, seen_transformations_test, unseen_transformations_test = challenge_seen_unseen_transformation_split(pairs_by_transformation, args.training_ratio)
309
 
310
  os.makedirs(f'{args.out_dir}/{version}/transformations_split/', exist_ok=True)
311
- save_json(seen_transformations_train, f'{args.out_dir}/{version}/transformations_split/train.json')
312
- save_json(seen_transformations_val, f'{args.out_dir}/{version}/transformations_split/val.json')
313
- save_json(seen_transformations_test, f'{args.out_dir}/{version}/transformations_split/seen_transformations_test.json')
314
- save_json(unseen_transformations_test, f'{args.out_dir}/{version}/transformations_split/unseen_transformations_test.json')
315
 
316
  elif args.split_criteria == 'source_dataset':
317
 
318
  itwiki_train, itwiki_val, itwiki_test, tn_test = challenge_seen_unseen_source_dataset_split(pairs_by_source_dataset, args.training_ratio)
319
 
320
  os.makedirs(f'{args.out_dir}/{version}/source_dataset_split/', exist_ok=True)
321
- save_json(itwiki_train, f'{args.out_dir}/{version}/source_dataset_split/itwiki_train.json')
322
- save_json(itwiki_val, f'{args.out_dir}/{version}/source_dataset_split/itwiki_val.json')
323
- save_json(itwiki_test, f'{args.out_dir}/{version}/source_dataset_split/itwiki_test.json')
324
- save_json(tn_test, f'{args.out_dir}/{version}/source_dataset_split/tn_test.json')
325
 
326
 
327
 
 
35
  None
36
 
37
  """
38
+
39
  json.dump(data, open(path, 'w+', encoding='utf-8'))
40
 
41
 
42
+ def save_jsonl(data, path):
43
+ """ Save data as a json file where each line is a dictionary
44
+
45
+ Args:
46
+ data (:obj:`list`): Data to store as json.
47
+ path (:obj:`str`): Path where data will be stored.
48
+
49
+ Returns:
50
+ None
51
+
52
+ """
53
+
54
+
55
+ with open(path, 'w+', encoding='utf-8') as f:
56
+ for sample_dict in data:
57
+ f.write(json.dumps(sample_dict))
58
+ f.write('\n')
59
+
60
+
61
  def get_types(xml):
62
  """ Extract the different transformation types.
63
 
 
319
  if args.split_criteria == 'random':
320
  train, val, test = random_split(pairs, args.training_ratio)
321
  os.makedirs(f'{args.out_dir}/{version}/random_split/', exist_ok=True)
322
+ save_jsonl(train, f'{args.out_dir}/{version}/random_split/train.jsonl')
323
+ save_jsonl(val, f'{args.out_dir}/{version}/random_split/val.jsonl')
324
+ save_jsonl(test, f'{args.out_dir}/{version}/random_split/test.jsonl')
325
 
326
  elif args.split_criteria == 'transformations':
327
  seen_transformations_train, seen_transformations_val, seen_transformations_test, unseen_transformations_test = challenge_seen_unseen_transformation_split(pairs_by_transformation, args.training_ratio)
328
 
329
  os.makedirs(f'{args.out_dir}/{version}/transformations_split/', exist_ok=True)
330
+ save_jsonl(seen_transformations_train, f'{args.out_dir}/{version}/transformations_split/train.jsonl')
331
+ save_jsonl(seen_transformations_val, f'{args.out_dir}/{version}/transformations_split/val.jsonl')
332
+ save_jsonl(seen_transformations_test, f'{args.out_dir}/{version}/transformations_split/seen_transformations_test.jsonl')
333
+ save_jsonl(unseen_transformations_test, f'{args.out_dir}/{version}/transformations_split/unseen_transformations_test.jsonl')
334
 
335
  elif args.split_criteria == 'source_dataset':
336
 
337
  itwiki_train, itwiki_val, itwiki_test, tn_test = challenge_seen_unseen_source_dataset_split(pairs_by_source_dataset, args.training_ratio)
338
 
339
  os.makedirs(f'{args.out_dir}/{version}/source_dataset_split/', exist_ok=True)
340
+ save_jsonl(itwiki_train, f'{args.out_dir}/{version}/source_dataset_split/itwiki_train.jsonl')
341
+ save_jsonl(itwiki_val, f'{args.out_dir}/{version}/source_dataset_split/itwiki_val.jsonl')
342
+ save_jsonl(itwiki_test, f'{args.out_dir}/{version}/source_dataset_split/itwiki_test.jsonl')
343
+ save_jsonl(tn_test, f'{args.out_dir}/{version}/source_dataset_split/tn_test.jsonl')
344
 
345
 
346