forked from pemistahl/lingua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
439 lines (381 loc) · 14.2 KB
/
build.gradle.kts
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* Copyright © 2018-today Peter M. Stahl pemistahl@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expressed or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.adarshr.gradle.testlogger.theme.ThemeType
import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import ru.vyarus.gradle.plugin.python.task.PythonTask
import java.util.Locale
val linguaTaskGroup: String by project
val linguaGroupId: String by project
val linguaArtifactId: String by project
val linguaVersion: String by project
val linguaName: String by project
val linguaDescription: String by project
val linguaLicenseName: String by project
val linguaLicenseUrl: String by project
val linguaWebsiteUrl: String by project
val linguaDeveloperId: String by project
val linguaDeveloperName: String by project
val linguaDeveloperEmail: String by project
val linguaDeveloperUrl: String by project
val linguaScmConnection: String by project
val linguaScmDeveloperConnection: String by project
val linguaScmUrl: String by project
val linguaSupportedDetectors: String by project
val linguaSupportedLanguages: String by project
val linguaMainClass: String by project
val linguaCsvHeader: String by project
val githubPackagesUrl: String by project
val compileTestKotlin: KotlinCompile by tasks
group = linguaGroupId
description = linguaDescription
plugins {
kotlin("jvm") version "1.7.20"
id("org.jlleitschuh.gradle.ktlint") version "11.0.0"
id("com.adarshr.test-logger") version "3.2.0"
id("com.asarkar.gradle.build-time-tracker") version "3.0.1" // newer versions need Java 11+
id("org.jetbrains.dokka") version "1.7.20"
id("ru.vyarus.use-python") version "3.0.0"
id("org.moditect.gradleplugin") version "1.0.0-rc3"
id("com.github.johnrengelman.shadow") version "7.1.2"
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
`maven-publish`
signing
jacoco
}
jacoco.toolVersion = "0.8.8"
sourceSets {
main {
resources {
exclude("training-data/**")
}
}
create("accuracyReport") {
compileClasspath += sourceSets.main.get().output
runtimeClasspath += sourceSets.main.get().output
}
}
val accuracyReportImplementation by configurations.getting {
extendsFrom(configurations.testImplementation.get())
}
configurations["accuracyReportRuntimeOnly"].extendsFrom(configurations.runtimeOnly.get())
tasks.withType<Test> {
useJUnitPlatform { failFast = true }
}
tasks.test {
maxParallelForks = 1
}
// Suppress warnings about incubating test suites feature
@Suppress("UnstableApiUsage")
testing {
suites {
// Separate test suite for module testing
val testJavaModule by registering(JvmTestSuite::class) {
dependencies {
implementation(project)
}
}
}
}
// Module tests require Java 9 or newer to compile and execute
if (JavaVersion.current().isJava9Compatible) {
tasks.check {
@Suppress("UnstableApiUsage")
dependsOn(testing.suites.named("testJavaModule"))
}
}
tasks.jacocoTestReport {
dependsOn("test")
reports {
xml.isEnabled = true
csv.isEnabled = false
html.isEnabled = true
}
classDirectories.setFrom(
files(
classDirectories.files.map {
fileTree(it) {
exclude("**/app/**")
}
}
)
)
}
tasks.register<Test>("accuracyReport") {
group = linguaTaskGroup
description = "Runs Lingua on provided test data, and writes detection accuracy reports for each language."
testClassesDirs = sourceSets["accuracyReport"].output.classesDirs
classpath = sourceSets["accuracyReport"].runtimeClasspath
val allowedDetectors = linguaSupportedDetectors.split(',')
val detectors = if (project.hasProperty("detectors"))
project.property("detectors").toString().split(Regex("\\s*,\\s*"))
else allowedDetectors
detectors.filterNot { it in allowedDetectors }.forEach {
throw GradleException(
"""
detector '$it' does not exist
supported detectors: ${allowedDetectors.joinToString(
", "
)}
""".trimIndent()
)
}
val allowedLanguages = linguaSupportedLanguages.split(',')
val languages = if (project.hasProperty("languages"))
project.property("languages").toString().split(Regex("\\s*,\\s*"))
else allowedLanguages
languages.filterNot { it in allowedLanguages }.forEach {
throw GradleException("language '$it' is not supported")
}
val availableCpuCores = Runtime.getRuntime().availableProcessors()
val cpuCoresRepr = if (project.hasProperty("cpuCores"))
project.property("cpuCores").toString()
else "1"
val cpuCores = try {
cpuCoresRepr.toInt()
} catch (e: NumberFormatException) {
throw GradleException("'$cpuCoresRepr' is not a valid value for argument -PcpuCores")
}
if (cpuCores !in 1..availableCpuCores) {
throw GradleException(
"""
$cpuCores cpu cores are not supported
minimum: 1
maximum: $availableCpuCores
""".trimIndent()
)
}
maxHeapSize = "4096m"
maxParallelForks = cpuCores
reports.html.isEnabled = false
reports.junitXml.isEnabled = false
testlogger {
theme = ThemeType.STANDARD_PARALLEL
showPassed = false
showSkipped = false
}
filter {
detectors.forEach { detector ->
languages.forEach { language ->
includeTestsMatching(
"$linguaGroupId.$linguaArtifactId.report" +
".${detector.toLowerCase(Locale.ROOT)}.${language}DetectionAccuracyReport"
)
}
}
}
}
tasks.register("writeAggregatedAccuracyReport") {
group = linguaTaskGroup
description = "Creates a table from all accuracy detection reports and writes it to a CSV file."
doLast {
val accuracyReportsDirectoryName = "accuracy-reports"
val accuracyReportsDirectory = file(accuracyReportsDirectoryName)
if (!accuracyReportsDirectory.exists()) {
throw GradleException("directory '$accuracyReportsDirectoryName' does not exist")
}
val detectors = linguaSupportedDetectors.split(',')
val languages = linguaSupportedLanguages.split(',')
val csvFile = file("$accuracyReportsDirectoryName/aggregated-accuracy-values.csv")
val stringToSplitAt = ">> Exact values:"
if (csvFile.exists()) csvFile.delete()
csvFile.createNewFile()
csvFile.appendText(linguaCsvHeader)
csvFile.appendText("\n")
for (language in languages) {
csvFile.appendText(language)
for (detector in detectors) {
val languageReportFileName =
"$accuracyReportsDirectoryName/${detector.toLowerCase(Locale.ROOT)}/$language.txt"
val languageReportFile = file(languageReportFileName)
val sliceLength = if (detector == "Lingua") (1..8) else (1..4)
if (languageReportFile.exists()) {
for (line in languageReportFile.readLines()) {
if (line.startsWith(stringToSplitAt)) {
val accuracyValues = line
.split(stringToSplitAt)[1]
.split(' ')
.slice(sliceLength)
.joinToString(",")
csvFile.appendText(",")
csvFile.appendText(accuracyValues)
}
}
} else {
if (detector == "Lingua") {
csvFile.appendText(",NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN")
} else {
csvFile.appendText(",NaN,NaN,NaN,NaN")
}
}
}
csvFile.appendText("\n")
}
println("file 'aggregated-accuracy-values.csv' written successfully")
}
}
tasks.register<PythonTask>("drawAccuracyPlots") {
dependsOn("writeAggregatedAccuracyReport")
group = linguaTaskGroup
description = "Draws plots showing the results of the accuracy detection reports."
command = "src/python-scripts/draw_accuracy_plots.py"
}
tasks.register<PythonTask>("writeAccuracyTable") {
dependsOn("writeAggregatedAccuracyReport")
group = linguaTaskGroup
description = "Creates HTML table from all accuracy detection results and writes it to a markdown file."
command = "src/python-scripts/write_accuracy_table.py"
}
tasks.addMainModuleInfo {
// Create Multi-Release JAR with Java 9 as lowest version
jvmVersion.set("9")
// Overwrite the output JAR file (if any) from a previous Gradle execution
overwriteExistingFiles.set(true)
module {
moduleInfoFile = File("$projectDir/src/main/java-9/module-info.java")
}
// Manually specify input; otherwise task seems to be erroneously considered UP-TO-DATE
// despite the JAR having changed, see https://github.com/moditect/moditect-gradle-plugin/pull/17
inputs.file(mainModule.get().inputJar)
}
// Workaround to avoid circular dependencies between tasks, see https://github.com/moditect/moditect-gradle-plugin/issues/14
project.afterEvaluate {
val compileJavaTask = tasks.compileJava.get()
compileJavaTask.setDependsOn(compileJavaTask.dependsOn - tasks.addDependenciesModuleInfo.get())
}
tasks.withType<DokkaTask>().configureEach {
dokkaSourceSets.configureEach {
jdkVersion.set(8)
reportUndocumented.set(false)
perPackageOption {
matchingRegex.set(".*\\.(app|internal).*")
suppress.set(true)
}
}
}
tasks.register<Jar>("dokkaJavadocJar") {
dependsOn("dokkaJavadoc")
group = "Build"
description = "Assembles a jar archive containing Javadoc documentation."
archiveClassifier.set("javadoc")
from("$buildDir/dokka/javadoc")
}
tasks.register<Jar>("sourcesJar") {
group = "Build"
description = "Assembles a jar archive containing the main source code."
archiveClassifier.set("sources")
from("src/main/kotlin")
}
tasks.register<ConfigureShadowRelocation>("relocateDependencies") {
group = "shadow"
description = "Specifies the ShadowJar task for which to relocate the dependencies."
target = tasks["jarWithDependencies"] as ShadowJar
}
tasks.register<ShadowJar>("jarWithDependencies") {
dependsOn("relocateDependencies")
group = "Build"
description = "Assembles a jar archive containing the main classes and all external dependencies."
archiveClassifier.set("with-dependencies")
from(sourceSets.main.get().output)
configurations = listOf(project.configurations.runtimeClasspath.get())
manifest { attributes("Main-Class" to linguaMainClass) }
}
tasks.register<JavaExec>("runLinguaOnConsole") {
group = linguaTaskGroup
description = "Starts a REPL (read-evaluate-print loop) to try Lingua on the command line."
mainClass.set(linguaMainClass)
standardInput = System.`in`
classpath = sourceSets["main"].runtimeClasspath
}
dependencies {
implementation("com.squareup.moshi:moshi:1.14.0")
implementation("com.squareup.moshi:moshi-kotlin:1.14.0")
implementation("it.unimi.dsi:fastutil:8.5.9")
testImplementation("org.junit.jupiter:junit-jupiter:5.9.0")
testImplementation("org.assertj:assertj-core:3.23.1")
testImplementation("io.mockk:mockk:1.13.2")
accuracyReportImplementation("com.optimaize.languagedetector:language-detector:0.6")
accuracyReportImplementation("org.apache.opennlp:opennlp-tools:1.9.4")
accuracyReportImplementation("org.apache.tika:tika-core:2.5.0")
accuracyReportImplementation("org.apache.tika:tika-langdetect-optimaize:2.5.0")
accuracyReportImplementation("org.slf4j:slf4j-nop:2.0.3")
}
python {
pip("matplotlib:3.6.0")
pip("seaborn:0.12.1")
pip("pandas:1.5.1")
pip("numpy:1.23.0")
}
publishing {
publications {
create<MavenPublication>("lingua") {
groupId = linguaGroupId
artifactId = linguaArtifactId
version = project.version.toString()
from(components["kotlin"])
artifact(tasks["sourcesJar"])
artifact(tasks["jarWithDependencies"])
artifact(tasks["dokkaJavadocJar"])
pom {
name.set(linguaName)
description.set(linguaDescription)
url.set(linguaWebsiteUrl)
licenses {
license {
name.set(linguaLicenseName)
url.set(linguaLicenseUrl)
}
}
developers {
developer {
id.set(linguaDeveloperId)
name.set(linguaDeveloperName)
email.set(linguaDeveloperEmail)
url.set(linguaDeveloperUrl)
}
}
scm {
connection.set(linguaScmConnection)
developerConnection.set(linguaScmDeveloperConnection)
url.set(linguaScmUrl)
}
}
}
}
repositories {
maven {
name = "GitHubPackages"
url = uri(githubPackagesUrl)
credentials {
username = linguaDeveloperId
password = project.findProperty("ghPackagesToken") as String?
}
}
}
}
nexusPublishing {
repositories {
sonatype()
}
}
signing {
sign(publishing.publications["lingua"])
}
repositories {
mavenCentral()
}