File size: 2,913 Bytes
877a3f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Author: Roman Janík
# Script for creating splits of a CoNLL format of my dataset.
#

import argparse
import os

from random import shuffle
from itertools import islice


def get_args():
    parser = argparse.ArgumentParser(description="Script for creating splits of a CoNLL format of my dataset.")
    parser.add_argument("-s", "--source_file", required=True, help="Path to source Label Studio json annotations file.")
    parser.add_argument("-o", "--output_dir", required=True, help="Output dir for CoNLL dataset splits.")
    parser.add_argument("-m", "--shuffle", default=True, dest="shuffle", action="store_true",
                        help="Shuffle annotations.")
    parser.add_argument("--shuffle_chunk", type=int, default=1, dest="shuffle_chunk",
                        help="Shuffle annotations chunk. Chunk annotations, shuffle and join back.")
    parser.add_argument("-p", "--split_ratios", required=True,
                        help="Ratios for train/dev/test splits, nums separated by ':'")
    args = parser.parse_args()
    return args


def parse_splits(split_str: str):
    split_list = split_str.split(":")
    assert len(split_list) == 3
    splits = {k: float(v) for k, v in zip(["train", "dev", "test"], split_list)}
    assert sum(splits.values()) == 1.0

    return splits


def batched_func(iterable, chunk_size):
    iterator = iter(iterable)
    return list(iter(
        lambda: list(islice(iterator, chunk_size)),
        list()
    ))


def main():
    args = get_args()

    print("Script for creating a CoNLL format of my dataset from text files and Label Studio annotations. "
          "Script goes through page text files and their annotations json record. "
          "Output CoNLL dataset split files are saved to output directory.")

    splits_ratios = parse_splits(args.split_ratios)

    with open(args.source_file, encoding="utf-8") as f:
        content = f.read()
        annotations = content.split("\n\n")

    print(f"Total training examples: {len(annotations)}\nStarting documents processing...")

    if args.shuffle:
        if args.shuffle_chunk > 1:
            chunked_annotations = batched_func(annotations, args.shuffle_chunk)
            shuffle(chunked_annotations)
            annotations = [item for sublist in chunked_annotations for item in sublist]
        else:
            shuffle(annotations)

    splits = {}
    lower_bound = 0
    for k, ratio in splits_ratios.items():
        upper_bound = lower_bound + int(ratio * len(annotations))
        splits[k] = annotations[lower_bound:upper_bound]
        lower_bound = upper_bound

    for name, split in splits.items():
        with open(os.path.join(args.output_dir, "poner_" + name + ".conll"), "w", encoding="utf-8") as f:
            for example in split:
                f.write(example + "\n\n")

        print(f"Split '{name}' is processed.")


if __name__ == '__main__':
    main()