mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 00:35:26 +02:00
Search by short name #11
This commit is contained in:
parent
d715bc61fa
commit
f7b625df50
@ -32,8 +32,10 @@ import java.math.BigDecimal
|
|||||||
* basicUnit. For example, in [UnitGroup.LENGTH] basic unit is an attometer (1), then nanometer is
|
* basicUnit. For example, in [UnitGroup.LENGTH] basic unit is an attometer (1), then nanometer is
|
||||||
* 1.0E+9 times bigger than that. This number (1.0E+9) is a basic unit for nanometer
|
* 1.0E+9 times bigger than that. This number (1.0E+9) is a basic unit for nanometer
|
||||||
* @property group [UnitGroup] of this unit
|
* @property group [UnitGroup] of this unit
|
||||||
* @property renderedName Used a cache. Stores long name string for this specific device. Need for
|
* @property renderedName Used as cache. Stores long name string for this specific device. Need for
|
||||||
* search functionality
|
* search functionality.
|
||||||
|
* @property renderedShortName Used as cache. Stores short name string for this specific device. Need for
|
||||||
|
* search functionality.
|
||||||
* @property isFavorite Whether this unit is favorite.
|
* @property isFavorite Whether this unit is favorite.
|
||||||
* @property isEnabled Whether we need to show this unit or not
|
* @property isEnabled Whether we need to show this unit or not
|
||||||
* @property pairedUnit Latest paired unit on the right
|
* @property pairedUnit Latest paired unit on the right
|
||||||
@ -46,6 +48,7 @@ abstract class AbstractUnit(
|
|||||||
var basicUnit: BigDecimal,
|
var basicUnit: BigDecimal,
|
||||||
val group: UnitGroup,
|
val group: UnitGroup,
|
||||||
var renderedName: String = String(),
|
var renderedName: String = String(),
|
||||||
|
var renderedShortName: String = String(),
|
||||||
var isFavorite: Boolean = false,
|
var isFavorite: Boolean = false,
|
||||||
var isEnabled: Boolean = true,
|
var isEnabled: Boolean = true,
|
||||||
var pairedUnit: String? = null,
|
var pairedUnit: String? = null,
|
||||||
@ -79,21 +82,31 @@ fun Sequence<AbstractUnit>.sortByLev(stringA: String): Sequence<AbstractUnit> {
|
|||||||
// List of pair: Unit and it's levDist
|
// List of pair: Unit and it's levDist
|
||||||
val unitsWithDist = mutableListOf<Pair<AbstractUnit, Int>>()
|
val unitsWithDist = mutableListOf<Pair<AbstractUnit, Int>>()
|
||||||
this.forEach { unit ->
|
this.forEach { unit ->
|
||||||
val unitName: String = unit.renderedName.lowercase()
|
val unitShortName: String = unit.renderedShortName.lowercase()
|
||||||
|
/**
|
||||||
|
* There is a chance that we search for unit with a specific short name. Such cases are
|
||||||
|
* should be higher in the list. Best possible match.
|
||||||
|
*/
|
||||||
|
if (stringToCompare == unitShortName) {
|
||||||
|
unitsWithDist.add(Pair(unit, 0))
|
||||||
|
return@forEach
|
||||||
|
}
|
||||||
|
|
||||||
|
val unitFullName: String = unit.renderedName.lowercase()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* There is chance that unit name doesn't need any edits (contains part of the query)
|
* There is chance that unit name doesn't need any edits (contains part of the query)
|
||||||
* So computing levDist is a waste of resources
|
* So computing levDist is a waste of resources
|
||||||
*/
|
*/
|
||||||
when {
|
when {
|
||||||
// It's the best possible match if it start with
|
// It's the second best possible match if it start with
|
||||||
unitName.startsWith(stringToCompare) -> {
|
unitFullName.startsWith(stringToCompare) -> {
|
||||||
unitsWithDist.add(Pair(unit, 0))
|
unitsWithDist.add(Pair(unit, 1))
|
||||||
return@forEach
|
return@forEach
|
||||||
}
|
}
|
||||||
// It's a little bit worse when it just contains part of the query
|
// It's a little bit worse when it just contains part of the query
|
||||||
unitName.contains(stringToCompare) -> {
|
unitFullName.contains(stringToCompare) -> {
|
||||||
unitsWithDist.add(Pair(unit, 1))
|
unitsWithDist.add(Pair(unit, 2))
|
||||||
return@forEach
|
return@forEach
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,8 +124,8 @@ fun Sequence<AbstractUnit>.sortByLev(stringA: String): Sequence<AbstractUnit> {
|
|||||||
*
|
*
|
||||||
* With substring levDist will be 3 so unit will be included
|
* With substring levDist will be 3 so unit will be included
|
||||||
*/
|
*/
|
||||||
val levDist = unitName
|
val levDist = unitFullName
|
||||||
.substring(0, minOf(stringToCompare.length, unitName.length))
|
.substring(0, minOf(stringToCompare.length, unitFullName.length))
|
||||||
.lev(stringToCompare)
|
.lev(stringToCompare)
|
||||||
|
|
||||||
// Threshold
|
// Threshold
|
||||||
|
@ -126,9 +126,8 @@ class AllUnitsRepository @Inject constructor() {
|
|||||||
// Query is empty, i.e. we want to see all units and they need to be sorted by usage
|
// Query is empty, i.e. we want to see all units and they need to be sorted by usage
|
||||||
basicFilteredUnits.sortedByDescending { it.counter }
|
basicFilteredUnits.sortedByDescending { it.counter }
|
||||||
} else {
|
} else {
|
||||||
// We are searching for a specific unit, we don't care about popularity
|
// We sort by popularity and Levenshtein distance (short and long name).
|
||||||
// We need search accuracy
|
basicFilteredUnits.sortedByDescending { it.counter }.sortByLev(searchQuery)
|
||||||
basicFilteredUnits.sortByLev(searchQuery)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return unitsToShow.groupBy { it.group }
|
return unitsToShow.groupBy { it.group }
|
||||||
@ -145,6 +144,7 @@ class AllUnitsRepository @Inject constructor() {
|
|||||||
allUnits.forEach {
|
allUnits.forEach {
|
||||||
// Loading unit names so that we can search through them
|
// Loading unit names so that we can search through them
|
||||||
it.renderedName = context.getString(it.displayName)
|
it.renderedName = context.getString(it.displayName)
|
||||||
|
it.renderedShortName = context.getString(it.shortName)
|
||||||
val based = allBasedUnits.firstOrNull { based -> based.unitId == it.unitId }
|
val based = allBasedUnits.firstOrNull { based -> based.unitId == it.unitId }
|
||||||
// Loading paired units
|
// Loading paired units
|
||||||
it.pairedUnit = based?.pairedUnitId
|
it.pairedUnit = based?.pairedUnitId
|
||||||
|
Loading…
x
Reference in New Issue
Block a user