Replace xdrop/fuzzywuzzy with tdebatty/java-string-similarity

This commit replaced `xdrop/fuzzywuzzy` with `tdebatty/java-string-similarity` as the former is under an incompatible GPLv3 license.
This commit is contained in:
◱ PixelyIon 2020-03-28 16:21:19 +05:30 committed by ◱ PixelyIon
parent 753448d774
commit 72272fa4c2
2 changed files with 21 additions and 5 deletions

View File

@ -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()

View File

@ -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<ItemType : BaseItem?, HeaderType : BaseHea
override fun getFilter(): Filter {
return object : Filter() {
inner class ScoredItem(val score: Double, val index: Int, val item:String) {}
fun extractSorted(term: String, keyArray: ArrayList<String>): Array<ScoredItem> {
val scoredItems : MutableList<ScoredItem> = 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<ItemType : BaseItem?, HeaderType : BaseHea
keyArray.add(item.key()!!.toLowerCase(Locale.getDefault()))
}
}
val topResults = FuzzySearch.extractSorted(searchTerm, keyArray)
val avgScore: Int = topResults.sumBy { it.score } / topResults.size
val topResults = extractSorted(searchTerm, keyArray)
val avgScore = topResults.sumByDouble { it.score } / topResults.size
for (result in topResults)
if (result.score > avgScore)
filterData.add(keyIndex[result.index])