Rapid, Low Power/cpu String Matching In Android
I'm working on an app that takes voice input, and matches that input to known items in a manifest. Each item in the manifest has a list of aliases, so that items with long titles c
Solution 1:
You can easily do this with a Map
assuming no duplicate aliases or product names. Kotlin version is:
dataclassProduct(val name: String, val aliases: Array<String>)
funtest() {
val products = listOf<Product>( ... )
// Do this once, create a map from name and aliases to the productval productIndex = products.asSequence().flatMap { product ->
val allKeys = sequenceOf(product.name) + product.aliases
allKeys.map { it.toLowerCase() to product }
}.toMap()
// now name => product and each alias => product are mapped in productIndexval matchingProduct = productIndex["something"] // search lower case name
println(matchingProduct?.name ?: "<not found>")
}
A Trie does not make sense unless you are doing prefix matches. A Set makes no sense because you can only tell "does it exist" and not "which thing is it that matches". A Map will go from anything to the original Product
from which you can get the name.
Also, your brute-force matching algorithm re-written in Kotlin is in an answer to your other question: https://stackoverflow.com/a/52565549/3679676
Post a Comment for "Rapid, Low Power/cpu String Matching In Android"