mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 11:50:54 +07:00
[ai-completion] ML-3971 implement computeRelaxedEditDistance
GitOrigin-RevId: d79f3ace6267214141d714c2fb027e34846b5fa3
This commit is contained in:
committed by
intellij-monorepo-bot
parent
334749adbb
commit
78d63773ad
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user