Skip to content

Commit

Permalink
Add fuzzy search
Browse files Browse the repository at this point in the history
Signed-off-by: shedaniel <daniel@shedaniel.me>
  • Loading branch information
shedaniel committed Feb 20, 2021
1 parent 8ebb39f commit 7f7a49f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ repositories {
}

dependencies {
compile("me.shedaniel:linkie-core:1.0.62")
compile("me.shedaniel:linkie-core:1.0.63")
compile("com.discord4j:discord4j-core:3.1.3") {
force = true
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/me/shedaniel/linkie/discord/LinkieBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ import me.shedaniel.linkie.namespaces.MojangSrgNamespace
import me.shedaniel.linkie.namespaces.PlasmaNamespace
import me.shedaniel.linkie.namespaces.YarnNamespace
import me.shedaniel.linkie.namespaces.YarrnNamespace
import me.shedaniel.linkie.utils.similarity
import java.io.File
import java.util.*

fun main() {
println("MinecraftClient".similarity("MinecraftClieny"))
(File(System.getProperty("user.dir")) / ".properties").apply {
if (exists()) {
val properties = Properties()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ class QueryClassCommand(private val namespace: Namespace?) : CommandBase {
}
}.block()
return message.getCatching(user) {
val result = MappingsQuery.queryClasses(QueryContext(
val context = QueryContext(
provider = provider,
searchKey = searchKey,
)).toSimpleMappingsMetadata().map { it.map { it.value }.toList() }
)
var result = MappingsQuery.queryClasses(context).toSimpleMappingsMetadata().map { it.map { it.value }.toList() }
if (result.value.isEmpty()) {
MappingsQuery.errorNoResultsFound(MappingsEntryType.CLASS, searchKey)
result = MappingsQuery.queryClasses(context.copy(accuracy = MatchAccuracy.Fuzzy)).toSimpleMappingsMetadata().map { it.map { it.value }.toList() }
if (result.value.isEmpty()) {
MappingsQuery.errorNoResultsFound(MappingsEntryType.CLASS, searchKey)
}
}
maxPage.set(ceil(result.value.size / 5.0).toInt())
return@getCatching result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,40 @@ class QueryCompoundCommand(private val namespace: Namespace?) : CommandBase {
result.sortByDescending { it.score }

if (result.isEmpty()) {
if (searchKey.onlyClass().firstOrNull()?.isDigit() == true && !searchKey.onlyClass().isValidIdentifier()) {
throw NullPointerException("No results found! `${searchKey.onlyClass()}` is not a valid java identifier!")
runBlocking {
launch {
try {
classes = MappingsQuery.queryClasses(context.copy(accuracy = MatchAccuracy.Fuzzy)).value
} catch (e: NullPointerException) {

}
}
launch {
try {
methods = MappingsQuery.queryMethods(context.copy(accuracy = MatchAccuracy.Fuzzy)).value
} catch (e: NullPointerException) {

}
}
launch {
try {
fields = MappingsQuery.queryFields(context.copy(accuracy = MatchAccuracy.Fuzzy)).value
} catch (e: NullPointerException) {

}
}
}
classes?.also(result::addAll)
methods?.also(result::addAll)
fields?.also(result::addAll)
result.sortByDescending { it.score }

if (result.isEmpty()) {
if (searchKey.onlyClass().firstOrNull()?.isDigit() == true && !searchKey.onlyClass().isValidIdentifier()) {
throw NullPointerException("No results found! `${searchKey.onlyClass()}` is not a valid java identifier!")
}
throw NullPointerException("No results found!")
}
throw NullPointerException("No results found!")
}

maxPage.set(ceil(result.size / 5.0).toInt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ class QueryFieldCommand(private val namespace: Namespace?) : CommandBase {
description = desc
}.block()
return message.getCatching(user) {
val result = MappingsQuery.queryFields(QueryContext(
val context = QueryContext(
provider = provider,
searchKey = searchKey,
)).map { it.map { it.value }.toList() }
)
var result = MappingsQuery.queryFields(context).map { it.map { it.value }.toList() }
if (result.value.isEmpty()) {
MappingsQuery.errorNoResultsFound(MappingsEntryType.FIELD, searchKey)
result = MappingsQuery.queryFields(context.copy(accuracy = MatchAccuracy.Fuzzy)).map { it.map { it.value }.toList() }
if (result.value.isEmpty()) {
MappingsQuery.errorNoResultsFound(MappingsEntryType.FIELD, searchKey)
}
}
maxPage.set(ceil(result.value.size / 5.0).toInt())
return@getCatching result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ class QueryMethodCommand(private val namespace: Namespace?) : CommandBase {
description = desc
}.block()
return message.getCatching(user) {
val result = MappingsQuery.queryMethods(QueryContext(
val context = QueryContext(
provider = provider,
searchKey = searchKey,
)).map { it.map { it.value }.toList() }
)
var result = MappingsQuery.queryMethods(context).map { it.map { it.value }.toList() }
if (result.value.isEmpty()) {
MappingsQuery.errorNoResultsFound(MappingsEntryType.METHOD, searchKey)
result = MappingsQuery.queryMethods(context.copy(accuracy = MatchAccuracy.Fuzzy)).map { it.map { it.value }.toList() }
if (result.value.isEmpty()) {
MappingsQuery.errorNoResultsFound(MappingsEntryType.METHOD, searchKey)
}
}
maxPage.set(ceil(result.value.size / 5.0).toInt())
return@getCatching result
Expand Down

0 comments on commit 7f7a49f

Please sign in to comment.