blob: 8402e57bcf784027e3931c750a790aa186bd22ae [file] [log] [blame]
dazaff42f632020-10-08 14:46:32 +02001import argparse
dazae3bc92e2020-11-04 11:06:26 +01002from lib.CoNLL_Annotation import read_conll_generator, get_token_type
dazaff42f632020-10-08 14:46:32 +02003
4# TODO: Parallelize this for HUGE Files: All sentences can be processed independently
5
6if __name__ == "__main__":
7 """
8 EXAMPLE:
9 For TreeTagger:
dazae3bc92e2020-11-04 11:06:26 +010010 python my_utils/conll_to_tok.py \
11 -s /home/daza/datasets/TIGER_conll/tiger_release_aug07.corrected.16012013.conll09 \
12 -ss "</S>" \
13 --token_type CoNLL09_Token
dazafb308a22021-01-27 16:20:08 +010014
dazad7d70752021-01-12 18:17:49 +010015 *** GERMAN UNIVERSAL DEPS TEST ***
dazae3bc92e2020-11-04 11:06:26 +010016 python my_utils/conll_to_tok.py \
17 -s /home/daza/datasets/ud-treebanks-v2.2/UD_German-GSD/de_gsd-ud-test.conllu \
18 -ss "</S>" \
19 --token_type CoNLLUP_Token
dazafb308a22021-01-27 16:20:08 +010020
dazad7d70752021-01-12 18:17:49 +010021 *** TIGER TEST NEW ORTH ***
22
23 python my_utils/conll_to_tok.py \
24 -s /home/daza/datasets/TIGER_conll/data_splits/test/Tiger.NewOrth.test.conll \
25 -ss "</S>" \
26 --token_type CoNLLUP_Token
dazad7d70752021-01-12 18:17:49 +010027
dazaff42f632020-10-08 14:46:32 +020028 For RNNTagger
dazae3bc92e2020-11-04 11:06:26 +010029 python my_utils/conll_to_tok.py \
30 -s /home/daza/datasets/TIGER_conll/tiger_release_aug07.corrected.16012013.conll09 \
31 --token_type CoNLL09_Token
32
33 python my_utils/conll_to_tok.py \
34 -s /home/daza/datasets/ud-treebanks-v2.2/UD_German-GSD/de_gsd-ud-test.conllu \
35 --token_type CoNLLUP_Token
dazaff42f632020-10-08 14:46:32 +020036 """
37
38 parser = argparse.ArgumentParser()
39 parser.add_argument("-s", "--src_file", help="CoNLLU File to Convert into the .tok input for RNNTagger/TreeTagger", required=True)
dazafb308a22021-01-27 16:20:08 +010040 parser.add_argument("-o", "--output_file", help="Output Formatted Corpus", default=None)
dazae3bc92e2020-11-04 11:06:26 +010041 parser.add_argument("-t", "--token_type", help="Type of Token of the INPUT file", required=True)
dazaff42f632020-10-08 14:46:32 +020042 parser.add_argument("-ss", "--sent_sep", help="Special separator to distinguish sentence boundaries", default="")
dazae3bc92e2020-11-04 11:06:26 +010043 parser.add_argument("-c", "--com_str", help="Skip line if it starts with this string (comment market)", default="# ")
dazaff42f632020-10-08 14:46:32 +020044 args = parser.parse_args()
45
dazafb308a22021-01-27 16:20:08 +010046 output_prefix = args.src_file if not args.output_file else args.output_file
47
dazad7d70752021-01-12 18:17:49 +010048 if args.sent_sep == "":
dazafb308a22021-01-27 16:20:08 +010049 output_file = open(f"{output_prefix}.tok","w")
dazad7d70752021-01-12 18:17:49 +010050 else:
dazafb308a22021-01-27 16:20:08 +010051 output_file = open(f"{output_prefix}.sep.tok","w")
dazaff42f632020-10-08 14:46:32 +020052
dazae3bc92e2020-11-04 11:06:26 +010053 for conll_obj in read_conll_generator(args.src_file, token_class=get_token_type(args.token_type), comment_str=args.com_str):
dazaff42f632020-10-08 14:46:32 +020054 for tok in conll_obj.tokens:
55 output_file.write(tok.word+"\n")
56 output_file.write(args.sent_sep+"\n")