-
Notifications
You must be signed in to change notification settings - Fork 1
/
tool.py
executable file
·179 lines (137 loc) · 8.85 KB
/
tool.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
import argparse, os
from scripts import tokenizer
from scripts import conll
from scripts import preprocessing
from scripts import utils
from scripts import helpers
from scripts import tokens
import time
batch_job_options = argparse.ArgumentParser(add_help=False)
batch_group = batch_job_options.add_argument_group('Batch Job Script options')
batch_group.add_argument('--name', '-n', type=str, help='Option for setting a manual name for batch job.')
batch_group.add_argument('--cpus-per-task', '-n_cpus', default=None, help=f'Sets number of CPUs allocated for the job. (RAM per core 4500MB) Default: {utils.DEFAULT_N_CPUs}')
batch_group.add_argument('--duration', default=None, help=f'Sets the TimeLimit for the job. Default: {utils.DEFAULT_TimeLimit}')
batch_group.add_argument('--partition', choices=['batch', 'largemem'], default=None, help=f'Setting partition for the job to run on (default={utils.DEFAULT_Partition}).')
batch_group.add_argument('--account', default=utils.DEFAULT_Account, type=str, help='Sets the account the job will run on.')
other_options = argparse.ArgumentParser(add_help=False)
other_options_group = batch_job_options.add_argument_group('Other options')
#other_options_group.add_argument('--split', '-s', nargs='?', const=True, default=False, help='If set True, text file will be splitted before tokenizing.')
other_options_group.add_argument('--split_size', type=int, default=100000, help='Sets split size (in lines).')
other_options_group.add_argument('--nl2x', action='store_true', help='If set, newlines will be doubled while processing.')
other_options_group.add_argument('--shell', action='store_true', help='If set, job will be executed via local shell instead of slurm-srun.')
arg_parser = argparse.ArgumentParser()
subparsers = arg_parser.add_subparsers(title="commands", dest="command", help='Tool options:')
sub_name = 'utils'
utils_args = subparsers.add_parser(sub_name, help='Options for Preprocessing ', parents=[batch_job_options])
# use store true, set split size separately
utils_group = utils_args.add_mutually_exclusive_group(required=True)
utils_group.add_argument('--split', '-s', type=str, help='Selected file will be splited.')
utils_group.add_argument('--fast_text', type=str, nargs=3, metavar=['lang1', 'lang2', 'output'], help='Expects path to two parallel language token files. Files will be converted into a single file in fast_text format.')
utils_group.add_argument('--fast_text_dir', type=str, help='Expects path to directory with parallel token files. Files will be converted to fast_text format.')
utils_group.add_argument('--add_nl2x', type=str, help='Adds a \\n to every line in the input file.')
utils_group.add_argument('--del_nl2x', type=str, help='Reduces the number of \\n evenly.')
utils_group.add_argument('--ft_add_n', type=str, help='Writes lines from empty.dict into file.')
utils_group.add_argument('--sublinks', type=str, help='Replaces links and mail addresses at the end of the line by placeholders.')
utils_group.add_argument('--resublinks', type=str, help='Replaces placeholders by original links and mail addresses.')
utils_group.add_argument('--download', type=str, nargs='*', help='Downloads specified corpus. ("news", "udpmodels2.4", "ud2.4", "jw300.l1-l2" e.g. "jw300.no-en")')
sub_name = 'conll'
conll_args = subparsers.add_parser(sub_name, help='Options for .conll files', parents=[batch_job_options])
conll_args.add_argument('--split', '-s', help='Argument must be a match-string that all respective parts share e.g. the original file name like: "europarl-v7.de-en.de"')
conll_group= conll_args.add_mutually_exclusive_group(required=True)
conll_group.add_argument('--parse', '-p', type=str,
help='Argument musst be a .conll file or a directoy containing split .conll-files when --split is set.')
conll_group.add_argument('--extract_tokens', '-e', type=str, help='Expects path to directory with conll-files from that tokens will be extracted.')
conll_group.add_argument('--chr', '-c', type=str, help='Expects path to directory with conll-files that will be converted.')
conll_group.add_argument('--train', '-t', type=str, help='Trains UUParser on a given treebank, expects treebank language code as input such as "de_gsd"')
conll_group.add_argument('--merge', '-m', metavar=['Directory', 'MatchString', 'OutputName'], type=str, nargs='+',
help='Merges all files in the given Directory that match the MatchString e.g. the original file name like: "europarl-v7.de-en.de". OutputName can be set optionally.')
conll_group.add_argument('--resublinks', type=str, help='Replaces placeholders by original links and mail addresses.')
sub_name = 'text'
text_args = subparsers.add_parser(sub_name, help='Text options such as UDPipe Tokenizer', parents=[batch_job_options])
text_args.add_argument('--split', '-s', action='store_true', help='If set, input file will be splitted before tokenizing.')
train_tokenize = text_args.add_mutually_exclusive_group(required=False)
train_tokenize.add_argument('--tokenize', type=str, help='Expects path to .txt file that will be tokenized using UDPipe')
train_tokenize.add_argument('--train', '-t', type=str, help='Trains UDPipe on a given treebank, expects treebank language code as input such as "de_gsd"')
train_tokenize.add_argument('--custom_model', '-c', action='store_true', help='If set UDPipe will load custom models instead of the official pre-trained ones.')
#train_tokenize.add_argument('--custom_model', '-c', action='store_true', help='If set UDPipe will load custom models instead of the official pre-trained ones.')
sub_name = 'token'
eflomal_args = subparsers.add_parser(sub_name, help='Options for Eflomal alignment tool', parents=[batch_job_options])
eflomal_group = eflomal_args.add_mutually_exclusive_group(required=True)
eflomal_group.add_argument('--align', '-a', type=str, help='Expects path to token directory. Word alignment is performed for all files.')
#delete_parser.add_argument('--file', '-r', default=False, action='store_true',
# help='Remove the contents of the directory, too',
# )
args = arg_parser.parse_args()
print (args)
if args.command == 'text':
if args.tokenize:
preprocessing.replace_chars_file(args.tokenize)
if args.split:
tokenizer.split_and_tokenize(args.tokenize, args.split_size, nl2x=args.nl2x)
else:
tokenizer.tokenize(args.tokenize)
elif args.command == 'utils':
if args.split:
preprocessing.split(args.split, args.split_size, nl2x=args.nl2x)
elif args.fast_text:
tokens.file2fast_text(*args.fast_text)
elif args.fast_text_dir:
raise NotImplementedError()
tokens.align(args.align, args.shell)
elif args.add_nl2x:
preprocessing.add_nl2x(args.add_nl2x)
elif args.del_nl2x:
preprocessing.del_nl2x(args.del_nl2x)
elif args.sublinks:
preprocessing.sublinks(args.sublinks)
elif args.resublinks:
preprocessing.resublinks(args.resublinks)
elif args.download:
if args.download[0] == 'news':
preprocessing.news_commentary_v14()
elif args.download[0] == 'udpmodels2.5':
preprocessing.download_udpipe_models(version='2_5')
elif args.download[0] == 'udpmodels2.4':
preprocessing.download_udpipe_models(version='2_4')
elif args.download[0] == 'ud2.4':
preprocessing.download_ud_treebank(version='2_4')
elif args.download[0] == 'ud2.5':
preprocessing.download_ud_treebank(version='2_5')
elif args.download[0].startswith('jw300.'):
l1, l2 = args.download[0].split('.')[-1].split('-')
preprocessing.download_JW300(l1, l2)
elif args.download[0].startswith('europarl.'):
l1, l2 = args.download[0].split('.')[-1].split('-')
preprocessing.download_europarl(l1, l2)
else:
print('Unkown corpus:', args.download[0])
elif args.ft_add_n:
preprocessing.add_fast_text_n(args.ft_add_n)
elif args.command == 'token':
if args.align:
tokens.align(args.align, use_shell=args.shell, args=args)
elif args.command == 'conll':
if args.extract_tokens:
conll.extract_tokens(args.extract_tokens, nl2x=args.nl2x)
elif args.merge:
t1 = time.time()
if args.nl2x:
conll.merge_conll_nl2x(*args.merge)
else:
conll.merge_conll(*args.merge)
print(round(time.time()-t1, 1))
elif args.chr:
conll.chr_format_dir(args.chr)
elif args.parse:
if args.split:
# add --lang -l for manually choosing language
input_dir = args.parse
match_string = args.split
helpers.handle_split(input_dir, match_string, do=conll.parse, args=args)
else:
conll.parse(args.parse, args=args)
elif args.train:
conll.train_parser(args.train, args=args)
elif args.resublinks:
conll.resublinks(args.resublinks)