Skip to content

Commit

Permalink
Make classes with constructor parameters state objects instead
Browse files Browse the repository at this point in the history
Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
  • Loading branch information
solonovamax committed Oct 19, 2023
1 parent 6982d4e commit 499c349
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import kotlin.math.max
*
* @author solonovamax
*/
public class LCS : MetricStringDistance, StringSimilarity, StringDistance {
public object LCS : MetricStringDistance, StringSimilarity, StringDistance {
override fun distance(s1: String, s2: String): Double {
return s1.length + s2.length - (similarity(s1, s2)) * 2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import ca.solostudios.stringsimilarity.interfaces.NormalizedStringSimilarity
*
* @author [Ligi](https://github.com/dxpux), solonovamax, Ported to java from .net by denmase
*/
public class RatcliffObershelp : NormalizedStringSimilarity, NormalizedStringDistance {
public object RatcliffObershelp : NormalizedStringSimilarity, NormalizedStringDistance {
/**
* Compute the Ratcliff-Obershelp similarity between strings.
*
Expand Down Expand Up @@ -79,39 +79,37 @@ public class RatcliffObershelp : NormalizedStringSimilarity, NormalizedStringDis
return 1.0 - similarity(s1, s2)
}

private companion object {
private fun getMatchCount(s1: String, s2: String): Int {
val anchor = findAnchor(s1, s2)
return when {
anchor.isEmpty() -> 0
else -> {
val s1MatchIndex = s1.indexOf(anchor)
val s2MatchIndex = s2.indexOf(anchor)
val frontS1 = s1.take(s1MatchIndex)
val frontS2 = s2.take(s2MatchIndex)
val endS1 = s1.substring(s1MatchIndex + anchor.length)
val endS2 = s2.substring(s2MatchIndex + anchor.length)
private fun getMatchCount(s1: String, s2: String): Int {
val anchor = findAnchor(s1, s2)
return when {
anchor.isEmpty() -> 0
else -> {
val s1MatchIndex = s1.indexOf(anchor)
val s2MatchIndex = s2.indexOf(anchor)
val frontS1 = s1.take(s1MatchIndex)
val frontS2 = s2.take(s2MatchIndex)
val endS1 = s1.substring(s1MatchIndex + anchor.length)
val endS2 = s2.substring(s2MatchIndex + anchor.length)

val frontCount = getMatchCount(frontS1, frontS2)
val endCount = getMatchCount(endS1, endS2)
return frontCount + anchor.length + endCount
}
val frontCount = getMatchCount(frontS1, frontS2)
val endCount = getMatchCount(endS1, endS2)
return frontCount + anchor.length + endCount
}
}
}

private fun findAnchor(s1: String, s2: String): String {
var longestLength = 0
var longest = ""
for (i in s1.indices) {
for (j in i + 1..s1.length) {
val substring = s1.substring(i, j)
if (substring.length > longestLength && s2.contains(substring)) {
longestLength = substring.length
longest = substring
}
private fun findAnchor(s1: String, s2: String): String {
var longestLength = 0
var longest = ""
for (i in s1.indices) {
for (j in i + 1..s1.length) {
val substring = s1.substring(i, j)
if (substring.length > longestLength && s2.contains(substring)) {
longestLength = substring.length
longest = substring
}
}
return longest
}
return longest
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,14 @@ import ca.solostudios.stringsimilarity.interfaces.StringSimilarity
*
* @author solonovamax
*/
public class NormalizedLCS : MetricStringDistance, NormalizedStringDistance, NormalizedStringSimilarity {
private val lcs = LCS()

public object NormalizedLCS : MetricStringDistance, NormalizedStringDistance, NormalizedStringSimilarity {
override fun distance(s1: String, s2: String): Double {
if (s1 == s2)
return 0.0
if (s1.isEmpty() || s2.isEmpty())
return 1.0

val distance = lcs.distance(s1, s2)
val distance = LCS.distance(s1, s2)
return (2 * distance) / (s1.length + s2.length + distance)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import ca.solostudios.stringsimilarity.factories.similarityTests
import io.kotest.core.spec.style.FunSpec

class LCSTest : FunSpec({
val lcs = LCS()
val lcs = LCS

include(metricDistanceTests(lcs))
include(similarityTests(lcs))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import ca.solostudios.stringsimilarity.factories.precomputedSimilarityTests
import io.kotest.core.spec.style.FunSpec

class RatcliffObershelpTest : FunSpec({
val ratcliffObershelp = RatcliffObershelp()
val ratcliffObershelp = RatcliffObershelp

include(normalizedDistanceTests(ratcliffObershelp))
include(normalizedSimilarityTests(ratcliffObershelp))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import ca.solostudios.stringsimilarity.factories.precomputedSimilarityTests
import io.kotest.core.spec.style.FunSpec

class NormalizedLCSTest : FunSpec({
val normalizedLCS = NormalizedLCS()
val normalizedLCS = NormalizedLCS

include(metricDistanceTests(normalizedLCS))
include(normalizedDistanceTests(normalizedLCS, false))
Expand Down

0 comments on commit 499c349

Please sign in to comment.