-
Notifications
You must be signed in to change notification settings - Fork 66
/
build.gradle.kts
385 lines (333 loc) · 12.1 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
/*
* Copyright © 2018-today Peter M. Stahl [email protected]
*
* 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.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.Locale
val linguaTaskGroup: String by project
val linguaGroupId: String by project
val linguaArtifactId: 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 "2.1.0"
id("org.jlleitschuh.gradle.ktlint") version "12.1.2"
id("org.jetbrains.dokka") version "1.9.20"
id("com.github.johnrengelman.shadow") version "8.1.1"
id("io.github.gradle-nexus.publish-plugin") version "2.0.0"
`maven-publish`
signing
jacoco
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_1_8
}
}
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.jacocoTestReport {
dependsOn("test")
reports {
xml.required.set(true)
csv.required.set(false)
html.required.set(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.required.set(false)
reports.junitXml.required.set(false)
filter {
detectors.forEach { detector ->
languages.forEach { language ->
includeTestsMatching(
"$linguaGroupId.$linguaArtifactId.report" +
".${detector.lowercase(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.lowercase(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.named("compileAccuracyReportKotlin", KotlinCompile::class) {
kotlinOptions.jvmTarget = JavaVersion.VERSION_17.toString()
}
tasks.named("compileAccuracyReportJava", JavaCompile::class) {
sourceCompatibility = JavaVersion.VERSION_17.toString()
targetCompatibility = JavaVersion.VERSION_17.toString()
}
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("${layout.buildDirectory}/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<ShadowJar>("jarWithDependencies") {
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.15.2")
implementation("com.squareup.moshi:moshi-kotlin:1.15.2")
implementation("it.unimi.dsi:fastutil:8.5.15")
testImplementation("org.junit.jupiter:junit-jupiter:5.11.3")
testImplementation("org.assertj:assertj-core:3.26.3")
testImplementation("io.mockk:mockk:1.13.13")
accuracyReportImplementation("com.optimaize.languagedetector:language-detector:0.6")
accuracyReportImplementation("org.apache.opennlp:opennlp-tools:2.5.1")
accuracyReportImplementation("org.apache.tika:tika-core:3.0.0")
accuracyReportImplementation("org.apache.tika:tika-langdetect-optimaize:3.0.0")
accuracyReportImplementation("org.slf4j:slf4j-nop:2.0.16")
}
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()
}