diff --git a/app/build.gradle b/app/build.gradle index a34d498e..c55b88aa 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -56,10 +56,10 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.preference:preference:1.1.0' implementation 'com.google.android.material:material:1.2.0-alpha05' - implementation 'me.xdrop:fuzzywuzzy:1.2.0' implementation "androidx.core:core-ktx:1.2.0" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.documentfile:documentfile:1.0.1' + implementation 'info.debatty:java-string-similarity:1.2.1' } repositories { mavenCentral() diff --git a/app/src/main/java/emu/skyline/adapter/HeaderAdapter.kt b/app/src/main/java/emu/skyline/adapter/HeaderAdapter.kt index 09a60035..2dc7e220 100644 --- a/app/src/main/java/emu/skyline/adapter/HeaderAdapter.kt +++ b/app/src/main/java/emu/skyline/adapter/HeaderAdapter.kt @@ -11,11 +11,13 @@ import android.view.ViewGroup import android.widget.BaseAdapter import android.widget.Filter import android.widget.Filterable -import me.xdrop.fuzzywuzzy.FuzzySearch +import info.debatty.java.stringsimilarity.Cosine +import info.debatty.java.stringsimilarity.JaroWinkler import java.io.* import java.util.* import kotlin.collections.ArrayList + enum class ElementType(val type: Int) { Header(0x0), Item(0x1) @@ -115,9 +117,23 @@ internal abstract class HeaderAdapter): Array { + val scoredItems : MutableList = ArrayList() + + val jw = JaroWinkler() + val cos = Cosine() + + keyArray.forEachIndexed { index, item -> scoredItems.add(ScoredItem((jw.similarity(term, item) + cos.similarity(term, item)) / 2, index, item)) } + scoredItems.sortWith(compareByDescending { it.score }) + + return scoredItems.toTypedArray() + } + override fun performFiltering(charSequence: CharSequence): FilterResults { val results = FilterResults() - searchTerm = (charSequence as String).toLowerCase(Locale.getDefault()).replace(" ".toRegex(), "") + searchTerm = (charSequence as String).toLowerCase(Locale.getDefault()) if (charSequence.isEmpty()) { results.values = elementArray.indices.toMutableList() results.count = elementArray.size @@ -132,8 +148,8 @@ internal abstract class HeaderAdapter avgScore) filterData.add(keyIndex[result.index])