[ai-completion] ML-3971 implement computeRelaxedEditDistance

GitOrigin-RevId: d79f3ace6267214141d714c2fb027e34846b5fa3
This commit is contained in:
Oleg Taratukhin
2024-09-27 21:42:32 +02:00
committed by intellij-monorepo-bot
parent 334749adbb
commit 78d63773ad

View File

@@ -1,5 +1,7 @@
package com.intellij.cce.metric
import org.apache.commons.text.similarity.LevenshteinDistance
object RelaxedSimilarityUtils {
private val nonValuable = Regex("[^a-zA-Z0-9_+\\-*%=&|@\$?]")
@@ -45,4 +47,31 @@ object RelaxedSimilarityUtils {
else -> RelaxedResult.NO
}
}
fun computeRelaxedEditDistance(
middle: String,
completion: String,
prefix: String,
suffix: String,
threshold: Float,
stripChars: Boolean = false,
): RelaxedResult {
if (middle.isBlank() || completion.isBlank()) return RelaxedResult.NO
val missingCode = middle + suffix
val completionLines = preProcessLines(completion, prefix, suffix, stripChars)
val middleLines = preProcessLines(middle, prefix, suffix, stripChars).toSet()
val prefixMatch = missingCode.startsWith(completion.trim())
val distanceCalculator = LevenshteinDistance.getDefaultInstance()
val linesMatched = completionLines.count { line ->
middleLines.any { distanceCalculator.apply(it, line) <= threshold }
}
return when {
linesMatched == completionLines.size -> RelaxedResult.MULTI
linesMatched > 0 || prefixMatch -> RelaxedResult.ANY
else -> RelaxedResult.NO
}
}
}