
Efficiently performs high-speed common prefix searches using a Double Array Trie, ideal for NLP tasks like Input Method Editors, morphological analyzers, and predictive text engines.
KDary is a Double Array Trie (DAT) library for Kotlin, providing efficient common prefix searches. This library is designed to facilitate Natural Language Processing (NLP) tasks such as developing Input Method Editors (IMEs) and morphological analyzers using Kotlin Multiplatform.
The core logic is ported from darts-clone, originally implemented in C++.
Add the following dependency to your build.gradle.kts:
dependencies {
implementation("io.github.tokuhirom.kdary:kdary:<VERSION>")
}See https://central.sonatype.com/artifact/io.github.tokuhirom.kdary/kdary for the latest version.
import io.github.tokuhirom.kdary.KDary
val dictionary = listOf("apple", "app", "application", "apply")
val dat = KDary.build(dictionary)val prefix = "app"
val results = dat.commonPrefixSearch(prefix)
println("Common prefixes for '$prefix': $results")
// Output: Common prefixes for 'app': [app, apple, application, apply]val word = "apple"
val exactMatch = dat.exactMatchSearch(word)
println("Exact match for '$word': $exactMatch")
// Output: Exact match for 'apple': trueval traverseResults = dat.traverse("app") { prefix, word ->
println("Traversed word: $word with prefix: $prefix")
}import io.github.tokuhirom.kdary.saveKDary
import io.github.tokuhirom.kdary.loadKDary
// Save to a file
saveKDary(dat, "dat.trie")
// Load from a file
val loadedDat = loadKDary("dat.trie")fun build(
keys: List<ByteArray>,
values: List<Int>? = null,
progressCallback: ((Int) -> Unit)? = null,
): KDary
Creates a Double Array Trie from the given list of words.
fun commonPrefixSearch(
key: ByteArray,
maxNumResults: Int? = null,
nodePos: Int = 0,
): List<CommonPrefixSearchResult>
Returns a list of words that are common prefixes of the given query.
fun exactMatchSearch(
key: ByteArray,
nodePos: Int = 0,
): ExactMatchSearchResult
Returns true if the exact word exists in the trie, false otherwise.
fun traverse(
key: ByteArray,
nodePos: Int,
keyPos: Int,
): TraverseResult
Traverses the trie starting from the given prefix, applying the provided action to each word found.
fun saveKDary(kdary: KDary, fileName: String)
Saves the given KDary instance to the specified file path.
fun loadKDary(fileName: String): KDary
Loads a KDary instance from the specified file path.
The ported version of this library, written in Kotlin, is distributed under the MIT License as described below.
The MIT License (MIT)
Copyright © 2024 Tokuhiro Matsuno, http://64p.org/ <tokuhirom@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO, THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
This library is a port of the original darts-clone, which was originally written in C++. The original darts-clone is distributed under the BSD 2-clause license, as described below.
# The BSD 2-clause license
Copyright (c) 2008-2014, Susumu Yata
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Special thanks to Susumu Yata for the original darts-clone implementation.
KDary is a Double Array Trie (DAT) library for Kotlin, providing efficient common prefix searches. This library is designed to facilitate Natural Language Processing (NLP) tasks such as developing Input Method Editors (IMEs) and morphological analyzers using Kotlin Multiplatform.
The core logic is ported from darts-clone, originally implemented in C++.
Add the following dependency to your build.gradle.kts:
dependencies {
implementation("io.github.tokuhirom.kdary:kdary:<VERSION>")
}See https://central.sonatype.com/artifact/io.github.tokuhirom.kdary/kdary for the latest version.
import io.github.tokuhirom.kdary.KDary
val dictionary = listOf("apple", "app", "application", "apply")
val dat = KDary.build(dictionary)val prefix = "app"
val results = dat.commonPrefixSearch(prefix)
println("Common prefixes for '$prefix': $results")
// Output: Common prefixes for 'app': [app, apple, application, apply]val word = "apple"
val exactMatch = dat.exactMatchSearch(word)
println("Exact match for '$word': $exactMatch")
// Output: Exact match for 'apple': trueval traverseResults = dat.traverse("app") { prefix, word ->
println("Traversed word: $word with prefix: $prefix")
}import io.github.tokuhirom.kdary.saveKDary
import io.github.tokuhirom.kdary.loadKDary
// Save to a file
saveKDary(dat, "dat.trie")
// Load from a file
val loadedDat = loadKDary("dat.trie")fun build(
keys: List<ByteArray>,
values: List<Int>? = null,
progressCallback: ((Int) -> Unit)? = null,
): KDary
Creates a Double Array Trie from the given list of words.
fun commonPrefixSearch(
key: ByteArray,
maxNumResults: Int? = null,
nodePos: Int = 0,
): List<CommonPrefixSearchResult>
Returns a list of words that are common prefixes of the given query.
fun exactMatchSearch(
key: ByteArray,
nodePos: Int = 0,
): ExactMatchSearchResult
Returns true if the exact word exists in the trie, false otherwise.
fun traverse(
key: ByteArray,
nodePos: Int,
keyPos: Int,
): TraverseResult
Traverses the trie starting from the given prefix, applying the provided action to each word found.
fun saveKDary(kdary: KDary, fileName: String)
Saves the given KDary instance to the specified file path.
fun loadKDary(fileName: String): KDary
Loads a KDary instance from the specified file path.
The ported version of this library, written in Kotlin, is distributed under the MIT License as described below.
The MIT License (MIT)
Copyright © 2024 Tokuhiro Matsuno, http://64p.org/ <tokuhirom@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO, THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
This library is a port of the original darts-clone, which was originally written in C++. The original darts-clone is distributed under the BSD 2-clause license, as described below.
# The BSD 2-clause license
Copyright (c) 2008-2014, Susumu Yata
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Special thanks to Susumu Yata for the original darts-clone implementation.