Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/arseny/auto download #4

Merged
merged 6 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.6.21"
kotlin("jvm") version "1.7.10"
id("me.champeau.jmh") version "0.6.6"
id("com.github.johnrengelman.shadow") version "7.1.2"
}

group = "com.worskap.nlp"
version = "0.2-SNAPSHOT"
version = "0.2"

repositories {
mavenCentral()
}

dependencies {
compileOnly("com.worksap.nlp:sudachi:0.5.3")
testImplementation("com.worksap.nlp:sudachi:0.5.3")
implementation("org.jetbrains.kotlinx:kotlinx-cli:0.3.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.2")
compileOnly("com.worksap.nlp:sudachi:0.6.2")
testImplementation("com.worksap.nlp:sudachi:0.6.2")
implementation("org.jetbrains.kotlinx:kotlinx-cli:0.3.5")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("org.jetbrains.kotlinx:kotlinx-html:0.7.5")
implementation("com.github.luben:zstd-jni:1.5.2-3")
implementation("com.google.guava:guava:31.1-jre")
Expand All @@ -33,7 +33,6 @@ tasks.test {

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
}

tasks.withType<ShadowJar> {
Expand Down
26 changes: 21 additions & 5 deletions src/main/kotlin/com/worksap/nlp/sudachi/diff/Main.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.worksap.nlp.sudachi.diff

import com.worksap.nlp.sudachi.diff.analyze.SudachiResolver
import kotlinx.cli.*
import java.nio.file.Files
import java.nio.file.Path
import java.util.function.BiPredicate
import java.util.logging.LogManager
import kotlin.io.path.name
import kotlin.io.path.notExists

private fun String.existingPath(): Path {
fun String.existingPath(): Path {
val path = Path.of(this)
if (path.notExists()) {
throw IllegalArgumentException("File $path does not exist")
Expand All @@ -34,11 +34,27 @@ object Main {
val output by option(ArgType.String, description = "path for outputting analyzed files").required()
val jar by option(ArgType.String, description = "path to sudachi jar file")
val config by option(ArgType.String, description = "path to sudachi configuration")
val filter by option(ArgType.String, description = "file filter to analyze, *.txt by default")
val cacheDirectory by option(ArgType.String, description = "cache directory, by default ~/.local/cache/sudachi-tools")
val systemDict by option(ArgType.String, description = "system dictionary to use instead of the configured one (Sudachi 0.6.0+)")
val userDict by option(ArgType.String, description = "additional user dictionary (Sudachi 0.6.0+)").multiple()

private fun resolveCacheDirectory(value: String?): Path {
if (value != null) {
return Path.of(value)
}
val homeDir = run {
val homeVar = System.getenv("HOME")
homeVar ?: System.getenv("USERPROFILE")
}
return Path.of(homeDir).resolve(".local/cache/sudachi-tools")
}

override fun execute() {
val cfg = SudachiRuntimeConfig(jar?.existingPath(), null, null, config?.existingPath())
val runner = SudachiAnalysisTaskRunner(cfg)
runner.process(input.existingPath(), Path.of(output))
val addSettings = SudachiAdditionalSettings(systemDict?.existingPath(), userDict.map { it.existingPath() })
val support = SudachiResolver(resolveCacheDirectory(cacheDirectory))
val runner = SudachiAnalysisTaskRunner(support.config(jar, config, addSettings))
runner.process(input.existingPath(), Path.of(output), filter ?: "*.txt")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/worksap/nlp/sudachi/diff/SuAnalyzer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ interface SuAnalyzer {

interface SuRuntime {
fun analyzer(mode: String = "C"): SuAnalyzer
fun run(input: Path, output: Path): Unit
fun run(input: Path, output: Path, filter: String): Unit
}
43 changes: 12 additions & 31 deletions src/main/kotlin/com/worksap/nlp/sudachi/diff/SudachiFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import kotlin.io.path.absolute
import kotlin.io.path.name
import kotlin.io.path.notExists

data class SudachiAdditionalSettings(
val systemDict: Path? = null,
val userDicts: List<Path> = emptyList(),
)

data class SudachiRuntimeConfig(
val sudachiJar: Path?,
val javaxJsonJar: Path?,
val jdartsCloneJar: Path?,
val classpath: List<Path>,
val sudachiConfigFile: Path?,
val addSettings: String = "{}"
val addSettings: String = "{}",
val addSettings2: SudachiAdditionalSettings? = null
)

private fun Path.absoluteUrl(): URL {
Expand All @@ -22,32 +26,9 @@ private fun Path.absoluteUrl(): URL {
}
return absolute().toUri().toURL()
}

private fun resolve(first: Path?, base: Path, namePart: String): URL? {
if (first != null) {
return first.absoluteUrl()
}
val parentDir = base.parent
val found = Files.find(parentDir, 1, { p, _ -> p.name.contains(namePart) }).findFirst()
if (found.isEmpty) {
return null
}
return found.get().absoluteUrl()
}

class SudachiAnalysisTaskRunner(private val config: SudachiRuntimeConfig) {
private val jars = kotlin.run {
val jars = ArrayList<URL>()
if (config.sudachiJar == null) {
config.javaxJsonJar?.let { jars.add(it.absoluteUrl()) }
config.jdartsCloneJar?.let { jars.add(it.absoluteUrl()) }
} else {
jars.add(config.sudachiJar.absoluteUrl())
resolve(config.jdartsCloneJar, config.sudachiJar, "jdartsclone")?.let { jars.add(it) }
resolve(config.javaxJsonJar, config.sudachiJar, "javax.json")?.let { jars.add(it) }
}
jars.toArray(emptyArray<URL>())
}
private val jars = config.classpath.map { it.toUri().toURL() }.toTypedArray()


// this classloader needs to load
// 1. Classes from com.worksap.nlp.sudachi.diff.iface package
Expand Down Expand Up @@ -76,12 +57,12 @@ class SudachiAnalysisTaskRunner(private val config: SudachiRuntimeConfig) {
}
}

fun process(input: Path, output: Path) {
fun process(input: Path, output: Path, filter: String) {
// runtime will be loaded by explicit classloader here, we should operate with it only via reflection
val runtime = classloader.loadClass("com.worksap.nlp.sudachi.diff.iface.SudachiRuntime")
val constructor = runtime.getConstructor(ClassLoader::class.java, SudachiRuntimeConfig::class.java)
val instance = constructor.newInstance(classloader, config) as SuRuntime
instance.run(input, output)
instance.run(input, output, filter)
classloader.close()
}
}
Loading