refactor(impression): add feature attribute as index, which will work with high-confidence impressions

This commit is contained in:
2026-05-18 23:05:35 +08:00
parent 11aae1a353
commit 047d1b56fe
@@ -8,11 +8,13 @@ import kotlin.concurrent.withLock
class Entity @JvmOverloads constructor( class Entity @JvmOverloads constructor(
val uuid: String = UUID.randomUUID().toString(), val uuid: String = UUID.randomUUID().toString(),
private val relations: MutableMap<String, MutableMap<String, Double>> = mutableMapOf(), private val relations: MutableMap<String, MutableMap<String, Double>> = mutableMapOf(),
private val impressions: MutableMap<String, ImpressionData> = mutableMapOf() private val impressions: MutableMap<String, IndexableData> = mutableMapOf(),
private val features: MutableMap<String, IndexableData> = mutableMapOf()
) { ) {
private val impressionLock = ReentrantLock() private val impressionLock = ReentrantLock()
private val relationLock = ReentrantLock() private val relationLock = ReentrantLock()
private val featureLock = ReentrantLock()
@JvmOverloads @JvmOverloads
fun updateRelation( fun updateRelation(
@@ -27,18 +29,38 @@ class Entity @JvmOverloads constructor(
fun updateImpression( fun updateImpression(
impression: String, impression: String,
newImpression: String? = null, newImpression: String? = null,
strength: Double = 1.0 confidence: Double = 1.0
): ImpressionData = impressionLock.withLock { ): IndexableData = impressionLock.withLock {
if (newImpression == null) { if (newImpression == null) {
impressions.computeIfAbsent(impression) { ImpressionData(strength) } impressions.computeIfAbsent(impression) { IndexableData(confidence) }
.also { it.confidence = strength } .also { it.confidence = confidence }
} else { } else {
impressions.remove(impression) impressions.remove(impression)
ImpressionData(strength).also { IndexableData(confidence).also {
impressions[newImpression] = it impressions[newImpression] = it
} }
}.also {
if (it.confidence >= 0.9) {
featureLock.withLock { features[impression] = it }
} }
} }
}
fun updateFeature(feature: String, newFeature: String? = null, confidence: Double = 1.0) = featureLock.withLock {
if (newFeature == null) {
features.computeIfAbsent(feature){ IndexableData(confidence) }
.also { it.confidence = confidence }
}else{
features.remove(feature)
IndexableData(confidence).also {
features[newFeature] = it
}
}
}
fun removeFeature(feature: String) = featureLock.withLock {
features.remove(feature)
}
fun removeImpression(impression: String) = impressionLock.withLock { fun removeImpression(impression: String) = impressionLock.withLock {
impressions.remove(impression) impressions.remove(impression)
@@ -78,7 +100,7 @@ class Entity @JvmOverloads constructor(
}.toSet() }.toSet()
} }
data class ImpressionData( data class IndexableData(
var confidence: Double var confidence: Double
) { ) {
private val vectors: ConcurrentHashMap<String, DoubleArray> = ConcurrentHashMap() private val vectors: ConcurrentHashMap<String, DoubleArray> = ConcurrentHashMap()