This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
/
Cli.kt
341 lines (313 loc) · 14.7 KB
/
Cli.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package org.rewedigital.konversation
import org.rewedigital.konversation.generator.Printer
import org.rewedigital.konversation.generator.alexa.AlexaExporter
import org.rewedigital.konversation.generator.dialogflow.DialogflowExporter
import org.rewedigital.konversation.generator.kson.KsonExporter
import org.rewedigital.konversation.parser.Parser
import java.io.File
import java.util.*
import java.util.function.Consumer
import java.util.stream.Stream
import kotlin.system.exitProcess
open class Cli {
val intentDb = mutableMapOf<String, MutableList<Intent>>()
val entityDb = mutableMapOf<String, MutableList<Entities>>()
private var cacheEverything = true // should be not the default value
private var countPermutations = false
private var stats = false
private var alexaIntentSchema: File? = null
private var limit: Int? = null
private var prettyPrint = false
private var ksonDir: File? = null
private var dialogflowDir: File? = null
private var dumpOnly = false
private var invocationName: String? = null
private var inputFileCount = 0
private var inspect = false
fun parseArgs(args: Array<String>) {
val inputFiles = mutableListOf<File>()
if (args.isEmpty()) {
L.error("Missing arguments! Please specify at least the kvs or grammar file you want to process.")
L.error()
help()
exit(-1)
} else {
var argNo = 0
while (argNo < args.size) {
val arg = args[argNo]
val argFile = File(arg)
if (argFile.absoluteFile.exists()) {
inputFiles += File(arg).absoluteFile
} else if (arg.endsWith(".kvs") || arg.endsWith(".grammar") || arg.endsWith(".values")) {
if (arg.contains('*')) {
val matcher = argFile.name.replace(".", "\\.").replace("*", ".*?").toRegex()
argFile.parentFile.listFiles { _, name ->
matcher.matches(name)
}.map { file ->
inputFiles += file.absoluteFile
}
} else {
L.error("Input file \"$arg\" not found!")
exit(-1)
}
} else {
when (arg.toLowerCase()) {
"help",
"-help",
"-h",
"/?" -> {
help()
return
}
"count",
"-count" -> countPermutations = true
"cache",
"-cache" -> cacheEverything = true
"--export-alexa" -> if (++argNo < args.size) {
alexaIntentSchema = File(args[argNo])
} else {
L.error("Target is missing")
exit(-1)
}
"--export-dialogflow" -> if (++argNo < args.size) {
dialogflowDir = File(args[argNo]).absoluteFile
} else {
L.error("Target is missing")
exit(-1)
}
"invocation",
"-invocation" -> if (++argNo < args.size) {
invocationName = args[argNo]
}
"--export-kson" -> if (++argNo < args.size) {
ksonDir = File(args[argNo])
} else {
L.error("Target directory is missing")
exit(-1)
}
"stats",
"-stats" -> stats = true
"limit",
"-limit",
"top",
"-top" -> if (++argNo < args.size) {
try {
limit = args[argNo].toInt()
} catch (e: Throwable) {
L.error("\"${args[argNo]}\" is no valid count of utterances.")
exit(-1)
}
} else {
L.error("Count is missing!")
exit(-1)
}
"prettyprint",
"-prettyprint" -> prettyPrint = true
"dump",
"-dump" -> dumpOnly = true
"-v",
"-version" -> L.log("Konversation CLI version 1.0.1")
else -> L.error("Unknown argument \"$arg\".")
}
}
argNo++
}
inputFiles.forEach { inputFile ->
when {
inputFile.isFile -> {
inputFileCount++
val parser = parseFile(inputFile)
intentDb.getOrPut("") { mutableListOf() } += parser.intents
parser.entities?.let { entityDb.getOrPut("") { mutableListOf() } += it }
}
inputFile.isDirectory -> inputFile
.listFiles { dir: File?, name: String? ->
File(dir, name).isDirectory && (name == "konversation" || name?.startsWith("konversation-") == true)
}.toList()
.flatMap { it.listFiles { _, name -> name.endsWith(".kvs") || name.endsWith(".grammar") || name.endsWith(".values") }.toList() }
.also {
inputFileCount += it.size
}
.forEach { file ->
val prefix = file.parentFile.absolutePath.substring(inputFile.absolutePath.length + 13).trimStart('-')
val parser = parseFile(file)
intentDb.getOrPut(prefix) { mutableListOf() } += parser.intents
parser.entities?.let { entityDb.getOrPut(prefix) { mutableListOf() } += it }
}
else -> {
L.error("Input file not found!")
exit(-1)
}
}
}
showStats()
ksonDir?.let(::exportKson)
alexaIntentSchema?.let(::exportAlexa)
dialogflowDir?.let(::exportDialogflow)
}
}
open fun parseFile(file: File) = Parser(file)
private fun showStats() = intentDb[""]?.let { intents ->
val intentCount = intentDb.values.flatten().distinctBy { it.name }.size
L.info("Parsing of $inputFileCount file${if (inputFileCount != 1) "s" else ""} finished. Found $intentCount intent${if (intentCount != 1) "s" else ""}.")
fun Number.formatted() = String.format(Locale.getDefault(), "%,d", this)
if (countPermutations) {
var total = 0L
intents.forEach { intent: Intent ->
var count = 0L
intent.utterances.forEach { count += it.permutationCount }
if (stats) L.debug("${intent.name} has ${intent.utterances.size} utterances which have in total ${count.formatted()} permutations")
total += count
}
if (stats) L.debug("That are in total ${total.formatted()} permutations!")
}
val all = intents.sumBy { intent ->
val permutations = intent.utterances.sumBy { utterance -> utterance.permutations.size }
if (stats) L.debug("${intent.name} has now ${permutations.formatted()} sample utterances")
val warn = when (permutations) {
in 0..999 -> false
in 1000..1999 -> {
L.warn("${intent.name} has ${permutations.formatted()} utterances, Actions on Google just support up to ${1000.formatted()}!")
true
}
else -> {
L.warn("${intent.name} has ${permutations.formatted()} utterances, Dialogflow just support up to ${2000.formatted()} and Actions on Google just ${1000.formatted()}!")
true
}
}
if (inspect || warn) {
L.info("Intent ${intent.name} has in total ${permutations.formatted()} utterances:")
intent.utterances.forEach { utterance ->
L.log(String.format(Locale.getDefault(), "%,6d utterances for %s", utterance.permutations.size, utterance.source))
}
}
permutations
}
if (stats) L.info("Generated in total ${all.formatted()} Utterances")
//println("- " + all.sorted().joinToString(separator = "\n- "))
if (dumpOnly) {
intents.forEach { intent ->
if (intent.utterances.isEmpty()) {
L.info("Skipping empty intent ${intent.name}...")
} else {
L.log("Dumping ${intent.name}...")
val stream = File("${intent.name}.txt").outputStream()
intent.utterances.forEach { utterance ->
utterance.permutations.forEach { permutation ->
stream.write(permutation.toByteArray())
stream.write(13)
stream.write(10)
}
}
stream.close()
}
}
}
}
private fun exportKson(baseDir: File) = intentDb.forEach { (config, intents) ->
val targetDir = File(baseDir.absolutePath + File.separator + "konversation".join("-", config))
ksonDir?.let {
intents.forEach { intent ->
val exporter = KsonExporter(intent.name)
targetDir.mkdirs()
val stream = File(targetDir, "${intent.name}.kson").outputStream()
val printer: Printer = { line ->
stream.write(line.toByteArray())
}
if (prettyPrint) {
exporter.prettyPrinted(printer, intents, entityDb[config])
} else {
exporter.minified(printer, intents, entityDb[config])
}
stream.close()
}
}
}
private fun exportAlexa(alexaIntentSchema: File) = intentDb.forEach { (config, intents) ->
val targetDir = File(alexaIntentSchema.path + File.separator + "konversation".join("-", config))
alexaIntentSchema.absoluteFile.parentFile.mkdirs()
invocationName?.let { skillName ->
val exporter = AlexaExporter(skillName, targetDir, limit ?: Int.MAX_VALUE)
val stream = alexaIntentSchema.outputStream()
val printer: Printer = { line ->
stream.write(line.toByteArray())
}
if (prettyPrint) {
exporter.prettyPrinted(printer, intents, entityDb[config])
} else {
exporter.minified(printer, intents, entityDb[config])
}
stream.close()
} ?: run {
L.error("Invocation name is missing! Please specify the invocation name with the parameter -invocation <name>.")
exit(-1)
}
}
private fun exportDialogflow(baseDir: File) = intentDb.forEach { (config, intents) ->
invocationName?.let { skillName ->
val exporter = DialogflowExporter(skillName)
val stream = File(baseDir, "dialogflow-$config.zip").outputStream()
if (prettyPrint) {
exporter.prettyPrinted(stream, intents, entityDb[config])
} else {
exporter.minified(stream, intents, entityDb[config])
}
stream.close()
} ?: run {
L.error("Invocation name is missing! Please specify the invocation name with the parameter -invocation <name>.")
exit(-1)
}
}
private fun help() {
L.log("Arguments for konversation:")
L.log("[-help] Print this help")
L.log("[-version] Print the version of this build")
L.log("[-count] Count the permutations and print this to the console")
L.log("[-stats] Print out some statistics while generation")
L.log("[-cache] Cache everything even if an utterance has just a single permutation")
L.log("[--export-alexa <OUTFILE>] Write the resulting json to OUTFILE instead of result.json")
L.log("[--export-dialogflow <OUTDIR>] Write the dialogflow zip file to the OUTDIR")
L.log("[-invocation <NAME>] Define the invocation name for the Alexa export")
L.log("[-limit <COUNT>] While pretty printing the json to the output file limit the utterances count per intent")
L.log("[--export-kson <OUTDIR>] Compiles the kvs file to kson resource files which are required for the runtime")
L.log("[-dump] Dump out all intents to its own txt file")
L.log("[-prettyprint] Generate a well formatted json for easier debugging")
L.log("<FILE> The grammar, kvs or values files to parse")
L.log()
}
open fun exit(status: Int) {
exitProcess(status)
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
Cli().parseArgs(args)
}
var L: LoggerFacade = DefaultLogger()
}
}
fun <T> Stream<T>.forEachIterator(block: Iterator<T>.(element: T) -> Unit) {
val iterator = iterator()
while (iterator.hasNext()) block(iterator, iterator.next())
}
fun <T> Iterable<T>.forEachBreakable(block: BreakableIterator<T>.(element: T) -> Unit) {
val iterator = BreakableIterator(iterator())
while (iterator.hasNext()) block(iterator, iterator.next())
}
fun String.join(delimiter: String, value: String?) =
if (value.isNullOrBlank()) {
this
} else {
"$this$delimiter$value"
}
class BreakableIterator<T>(private val inner: Iterator<T>) : Iterator<T> {
private var resume = true
override fun forEachRemaining(action: Consumer<in T>) =
inner.forEachRemaining(action)
override fun hasNext() = resume && inner.hasNext()
override fun next() = inner.next()
fun stop() {
resume = false
}
}
fun String?.runIfNotNullOrEmpty(callback: (String) -> Unit) = this?.let { if (this.isNotBlank()) callback.invoke(this) }