-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommandLineArguments.kt
268 lines (229 loc) · 9.16 KB
/
CommandLineArguments.kt
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
* ------------------------------------------------------------------*/
package com.kotlinnlp.nlpserver
import com.xenomachina.argparser.ArgParser
import com.xenomachina.argparser.SystemExitException
import com.xenomachina.argparser.default
/**
* The interpreter of command line arguments.
*
* @param args the array of command line arguments
*/
class CommandLineArguments(args: Array<String>) {
/**
* The parser of the string arguments.
*/
private val parser = ArgParser(args)
/**
* The port listened by the server (default = 3000).
*/
val port: Int by parser.storing(
"-p",
"--port",
help="the port listened by the server"
) { toInt() }.default(3000)
/**
* Whether to print debugging messages.
*/
val debug: Boolean by parser.flagging(
"-d",
"--debug",
help="whether to print debugging messages"
)
/**
* The number of threads used to parallelize operations when possible (default 1).
*/
val threads: Int by parser.storing(
"-t",
"--threads",
help="the number of threads used to parallelize operations when possible (default 1)"
) { toInt() }
.default(1)
.addValidator { if (value < 1) throw SystemExitException("The number of threads must be >= 1", 1) }
/**
* Whether to enable CORS requests.
*/
val enableCORS: Boolean by parser.flagging(
"--enable-cors",
help="whether to enable CORS requests"
)
/**
* The directory containing the serialized models of the NeuralTokenizer, one per language.
*/
val tokenizerModelsDir: String? by parser.storing(
"--tokenizers",
help="the directory containing the serialized models of the neural tokenizers (one per language)"
).default(null)
/**
* The filename of the LanguageDetector serialized model.
*/
val langDetectorModel: String? by parser.storing(
"--lang-detector",
help="the filename of the language detector serialized model"
).default(null)
/**
* The filename of the CJK NeuralTokenizer serialized model.
*/
val cjkTokenizerModel: String? by parser.storing(
"--cjk-tokenizer",
help="the filename of the CJK neural tokenizer model used by the language detector"
).default(null)
/**
* The filename of the FrequencyDictionary.
*/
val freqDictionary: String? by parser.storing(
"--freq-dict",
help="the filename of the frequency dictionary used by the language detector"
).default(null)
/**
* The directory containing the morphology dictionaries used by the parser (one per language).
*/
val morphoDictionaryDir: String? by parser.storing(
"--morpho-dict",
help="the directory containing the morphology dictionaries used by the parser (one per language)"
).default(null)
/**
* The directory containing the serialized models of the LHRParser, one per language.
*/
val lhrParserModelsDir: String? by parser.storing(
"--parsers",
help="the directory containing the serialized models of the neural parsers (one per language)"
).default(null)
/**
* The directory containing the serialized models of the frame extractors, one per domain.
*/
val frameExtractorModelsDir: String? by parser.storing(
"--frame-extractors",
help="the directory containing the serialized models of the frame extractors, one per domain"
).default(null)
/**
* The directory containing the pre-trained word embeddings files for the frame extractors, one per domain (the file
* name must end with '__' followed by the domain name).
*/
val framesExtractorEmbeddingsDir: String? by parser.storing(
"--frame-extractors-emb",
help="the directory containing the pre-trained word embeddings files for the frame extractors, one per domain " +
"(the file name must end with '__' followed by the domain name)"
).default(null)
/**
* The directory containing the serialized models of the classifiers, one per domain.
*/
val classifierModelsDir: String? by parser.storing(
"--classifiers",
help="the directory containing the serialized models of the classifiers, one per domain"
).default(null)
/**
* The directory containing the pre-trained word embeddings files for the classifiers, one per domain (the file name
* must end with '__' followed by the domain name).
*/
val classifierEmbeddingsDir: String? by parser.storing(
"--classifiers-emb",
help="the directory containing the pre-trained word embeddings files for the classifiers, one per domain " +
"(the file name must end with '__' followed by the domain name)"
).default(null)
/**
* The directory containing the serialized models of the tokens labelers, one per domain.
*/
val labelerModelsDir: String? by parser.storing(
"--labelers",
help="the directory containing the serialized models of the tokens labelers, one per domain"
).default(null)
/**
* The filename of the LocationsDictionary.
*/
val locationsDictionary: String? by parser.storing(
"--locations-dict",
help="the filename of the serialized locations dictionary"
).default(null)
/**
* The directory containing the generic pre-trained word embeddings files, one per language (the file name
* must end with '__' followed by the language ISO 639-1 code).
*/
val wordEmbeddingsDir: String? by parser.storing(
"--word-embeddings",
help="the directory containing the generic pre-trained word embeddings files, one per language " +
"(the file name must end with '__' followed by the language ISO 639-1 code)"
).default(null)
/**
* The directory containing the blacklists of terms for the comparison, one per language (the file name
* must end with '__' followed by the language ISO 639-1 code).
*/
val comparisonBlacklistsDir: String? by parser.storing(
"--comparison-blacklists",
help="the directory containing the blacklists of terms for the comparison, one per language " +
"(the file name must end with '__' followed by the language ISO 639-1 code)"
).default(null)
/**
* The directory containing the blacklists of terms for the summary, one per language (the file name
* must end with '__' followed by the language ISO 639-1 code).
*/
val summaryBlacklistsDir: String? by parser.storing(
"--summary-blacklists",
help="the directory containing the blacklists of terms for the summary, one per language " +
"(the file name must end with '__' followed by the language ISO 639-1 code)"
).default(null)
/**
* Force parsing all arguments (only read ones are parsed by default).
* Check dependencies.
*/
init {
parser.force()
this.checkDependencies()
}
/**
* Check dependencies of all arguments.
*
* @throws ArgumentDependencyNotSatisfied if at least one dependency of an argument is not satisfied
*/
private fun checkDependencies() {
this.checkDependency(
arg = this.langDetectorModel, argName = "language detector",
dep = this.cjkTokenizerModel, depName = "cjk tokenizer",
checkReverse = true)
this.checkDependency(
arg = this.lhrParserModelsDir, argName = "parsers",
dep = this.tokenizerModelsDir, depName = "tokenizers")
this.checkDependency(
arg = this.locationsDictionary, argName = "locations dictionary",
dep = this.tokenizerModelsDir, depName = "tokenizers")
this.checkDependency(
arg = this.frameExtractorModelsDir, argName = "frame extractors",
dep = this.tokenizerModelsDir, depName = "tokenizers")
this.checkDependency(
arg = this.classifierModelsDir, argName = "classifiers",
dep = this.tokenizerModelsDir, depName = "tokenizers")
this.checkDependency(
arg = this.labelerModelsDir, argName = "labelers",
dep = this.tokenizerModelsDir, depName = "tokenizers")
this.checkDependency(
arg = this.classifierEmbeddingsDir, argName = "classifiers embeddings",
dep = this.classifierModelsDir, depName = "classifiers")
this.checkDependency(
arg = this.comparisonBlacklistsDir, argName = "comparison blacklists",
dep = this.wordEmbeddingsDir, depName = "pre-trained word embeddings")
this.checkDependency(
arg = this.comparisonBlacklistsDir, argName = "comparison blacklists",
dep = this.lhrParserModelsDir, depName = "parsers")
}
/**
* Check the dependency of an argument.
*
* @param arg the argument
* @param argName the argument name
* @param dep the dependency to check
* @param depName the dependency name
* @param checkReverse whether to check the dependency in the reverse order
*
* @throws ArgumentDependencyNotSatisfied if at least one dependency of an argument is not satisfied
*/
private fun checkDependency(arg: Any?, argName: String, dep: Any?, depName: String, checkReverse: Boolean = false) {
if (arg != null && dep == null)
throw ArgumentDependencyNotSatisfied(argName = argName, dependency = depName)
if (checkReverse && dep != null && arg == null)
throw ArgumentDependencyNotSatisfied(argName = depName, dependency = argName)
}
}