Skip to content

Commit

Permalink
Remove unused file(s), Add Test-Case & Bump Version for release
Browse files Browse the repository at this point in the history
  • Loading branch information
Lundez committed Feb 21, 2020
1 parent 6141128 commit f6df4d4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 368,162 deletions.
15 changes: 13 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`maven-publish`
id("org.jetbrains.dokka") version "0.10.0"
id("com.github.ben-manes.versions") version "0.27.0"

kotlin("jvm") version "1.3.60"
}

group = "com.londogard"
version = "1.0.1-beta"
version = "1.0.1"

repositories {
mavenCentral()
mavenLocal()
maven("https://oss.sonatype.org/content/repositories/snapshots")
}

dependencies {
implementation(kotlin("stdlib-jdk8"))
testImplementation("junit:junit:4.12")
testImplementation("org.slf4j:slf4j-simple:1.7.26")
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}

tasks.withType<Test> {
useJUnit()
}

tasks.dokka {
outputFormat = "html"
outputDirectory = "$buildDir/javadoc"
Expand Down
63 changes: 53 additions & 10 deletions src/main/kotlin/com/londogard/fuzzymatch/FuzzyMatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,70 @@ class FuzzyMatcher(private val scoreConfig: ScoreConfig = ScoreConfig()) {
* @param textLen: recursion param (ignore it)
* @param fullText: recursion param (ignore it)
*/
fun fuzzyMatchFunc(text: String, pattern: String, res: Result = emptyResult, textLen: Int = text.length, fullText: String = text): Result {
private fun fuzzyMatchFunc(
text: String,
pattern: String,
res: Result = emptyResult,
textLen: Int = text.length,
fullText: String = text
): Result {
return when {
pattern.length > text.length || text.isEmpty()-> emptyResult
pattern.length > text.length || text.isEmpty() -> emptyResult
pattern.isEmpty() -> res
else -> {
val recursiveParams = pattern.foldIndexed(DataHolder(text, res.indices, emptyList())) { index, (textLeft, matches, recursiveRes), patternChar ->
val recursiveParams = pattern.foldIndexed(
DataHolder(
text,
res.indices,
emptyList()
)
) { index, (textLeft, matches, recursiveRes), patternChar ->
when {
textLeft.isEmpty() -> return emptyResult
patternChar.equals(textLeft[0], true) -> {
val recursiveResult = fuzzyMatchFunc(textLeft.drop(1), pattern.substring(index), res.copy(indices = matches), textLen, fullText)
val recursiveResult = fuzzyMatchFunc(
textLeft.drop(1),
pattern.substring(index),
res.copy(indices = matches),
textLen,
fullText
)

DataHolder(textLeft.drop(1), matches + (textLen - textLeft.length), recursiveRes + recursiveResult)
DataHolder(
textLeft.drop(1),
matches + (textLen - textLeft.length),
recursiveRes + recursiveResult
)
}
else -> {
val updatedText = textLeft.dropWhile { !it.equals(patternChar, true) }
if (updatedText.isEmpty()) return emptyResult

val recursiveResult = fuzzyMatchFunc(updatedText.drop(1), pattern.substring(index), res.copy(indices = matches), textLen, fullText)
val recursiveResult = fuzzyMatchFunc(
updatedText.drop(1),
pattern.substring(index),
res.copy(indices = matches),
textLen,
fullText
)

DataHolder(updatedText.drop(1), matches + (textLen - updatedText.length), recursiveRes + recursiveResult)
DataHolder(
updatedText.drop(1),
matches + (textLen - updatedText.length),
recursiveRes + recursiveResult
)
}
}
}
val results = recursiveParams.recursiveResults + Result(recursiveParams.matches, 10)

if (recursiveParams.matches.size != pattern.length) emptyResult
else results.filter { it.score > 0 }.map { Result(it.indices, scoringFunction(it.indices, fullText)) }.maxBy { it.score }?.copy(text = fullText)!!
else results.filter { it.score > 0 }.map {
Result(
it.indices,
scoringFunction(it.indices, fullText)
)
}.maxBy { it.score }?.copy(text = fullText)!!
}
}
}
Expand All @@ -75,7 +112,12 @@ class FuzzyMatcher(private val scoreConfig: ScoreConfig = ScoreConfig()) {
* @param topN: Number of results to return
*/
fun fuzzyMatch(texts: List<String>, pattern: String, topN: Int = 20): List<Result> =
texts
if (pattern.length == 1) texts.asSequence()
.filter { it.contains(pattern) }
.take(topN)
.map { match -> Result(listOf(match.indexOf(pattern)), scoreConfig.firstLetterMatch, match) }
.toList()
else texts
.map { fuzzyMatchFunc(it, pattern) }
.filter { it.score > 0 }
.sortedByDescending { it.score }
Expand All @@ -88,7 +130,8 @@ class FuzzyMatcher(private val scoreConfig: ScoreConfig = ScoreConfig()) {
val firstLetter = if (indexWindow.first() == 0) scoreConfig.firstLetterMatch else 0
val consecutive = if (indexWindow.first() == indexWindow.last() - 1) scoreConfig.consecutiveMatch else 0
val neighbour = text[indexWindow.first()]
val camelCase = if (neighbour.isLowerCase() && text[indexWindow.last()].isUpperCase()) scoreConfig.camelCaseMatch else 0
val camelCase =
if (neighbour.isLowerCase() && text[indexWindow.last()].isUpperCase()) scoreConfig.camelCaseMatch else 0
val separator = if (neighbour == ' ' || neighbour == '_') scoreConfig.separatorMatch else 0
val unmatched = (indices.lastOrNull() ?: text.length) * scoreConfig.unmatchedLetter

Expand Down
17 changes: 17 additions & 0 deletions src/test/kotlin/com/londogard/fuzzymatch/FuzzyMatcherTest.kt
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
package com.londogard.fuzzymatch

import org.junit.Test


class FuzzyMatcherTest {
val lines = javaClass.getResourceAsStream("english_355k_words.txt").bufferedReader().readLines()
val fuzzyMatcher = FuzzyMatcher()
@Test
fun `fuzzy match should match something`() {
assert(fuzzyMatcher.fuzzyMatch(lines, "2nd").contains(FuzzyMatcher.Result(listOf(0, 1, 2), 41, "2nd")))
assert(fuzzyMatcher.fuzzyMatch(lines, "a").size == 20)
}

@Test
fun `fuzzy match should return N results`() {
assert(fuzzyMatcher.fuzzyMatch(lines, "a", 5).size == 5)
}
}
File renamed without changes.
Loading

0 comments on commit f6df4d4

Please sign in to comment.