mirror of
https://github.com/slhaf/Partner.git
synced 2026-10-25 02:43:49 +00:00
Compare commits
2
Commits
047d1b56fe
...
f3213675ff
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f3213675ff | ||
|
|
26ef5d875d |
+70
@@ -0,0 +1,70 @@
|
|||||||
|
package work.slhaf.partner.core.cognition.impression
|
||||||
|
|
||||||
|
import org.w3c.dom.Document
|
||||||
|
import org.w3c.dom.Element
|
||||||
|
import work.slhaf.partner.core.cognition.context.BlockContent
|
||||||
|
import java.util.concurrent.atomic.AtomicReference
|
||||||
|
|
||||||
|
class ActiveEntity @JvmOverloads constructor(
|
||||||
|
timestamp: Long = System.currentTimeMillis(),
|
||||||
|
private val _evidences: MutableList<String> = mutableListOf(),
|
||||||
|
) : BlockContent("active_entity_$timestamp", "impression") {
|
||||||
|
val evidences: List<String> get() = _evidences
|
||||||
|
|
||||||
|
private val _subject = AtomicReference("UNKNOWN")
|
||||||
|
val subject: String get() = _subject.get()
|
||||||
|
|
||||||
|
private val _projectedFeatures: MutableMap<String, Double> = mutableMapOf()
|
||||||
|
val projectedFeatures: Map<String, Double> get() = _projectedFeatures
|
||||||
|
|
||||||
|
private val _projectedImpressions: MutableMap<String, Double> = mutableMapOf()
|
||||||
|
val projectedImpressions: Map<String, Double> get() = _projectedImpressions
|
||||||
|
|
||||||
|
fun addEvidence(evidence: String) = synchronized(_evidences) {
|
||||||
|
_evidences.add(evidence)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateSubject(subject: String) = _subject.set(subject)
|
||||||
|
|
||||||
|
fun addProjectedFeatures(vararg features: Pair<String, Double>) = synchronized(_projectedFeatures) {
|
||||||
|
features.forEach { _projectedFeatures[it.first] = it.second }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addProjectedImpressions(vararg impressions: Pair<String, Double>) = synchronized(_projectedImpressions) {
|
||||||
|
impressions.forEach { _projectedImpressions[it.first] = it.second }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun fillXml(document: Document, root: Element) {
|
||||||
|
appendTextElement(document, root, "subject", subject)
|
||||||
|
|
||||||
|
appendListElement(
|
||||||
|
document,
|
||||||
|
root,
|
||||||
|
"evidences",
|
||||||
|
"evidence",
|
||||||
|
synchronized(_evidences) { _evidences.toList() }
|
||||||
|
)
|
||||||
|
|
||||||
|
appendListElement(
|
||||||
|
document,
|
||||||
|
root,
|
||||||
|
"projected_features",
|
||||||
|
"feature",
|
||||||
|
synchronized(_projectedFeatures) { _projectedFeatures.entries.toList() }
|
||||||
|
) { entry ->
|
||||||
|
setAttribute("confidence", entry.value.toString())
|
||||||
|
textContent = entry.key
|
||||||
|
}
|
||||||
|
|
||||||
|
appendListElement(
|
||||||
|
document,
|
||||||
|
root,
|
||||||
|
"projected_impressions",
|
||||||
|
"impression",
|
||||||
|
synchronized(_projectedImpressions) { _projectedImpressions.entries.toList() }
|
||||||
|
) { entry ->
|
||||||
|
setAttribute("confidence", entry.value.toString())
|
||||||
|
textContent = entry.key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ 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 subject: String,
|
||||||
private val relations: MutableMap<String, MutableMap<String, Double>> = mutableMapOf(),
|
private val relations: MutableMap<String, MutableMap<String, Double>> = mutableMapOf(),
|
||||||
private val impressions: MutableMap<String, IndexableData> = mutableMapOf(),
|
private val impressions: MutableMap<String, IndexableData> = mutableMapOf(),
|
||||||
private val features: MutableMap<String, IndexableData> = mutableMapOf()
|
private val features: MutableMap<String, IndexableData> = mutableMapOf()
|
||||||
@@ -33,24 +34,28 @@ class Entity @JvmOverloads constructor(
|
|||||||
): IndexableData = impressionLock.withLock {
|
): IndexableData = impressionLock.withLock {
|
||||||
if (newImpression == null) {
|
if (newImpression == null) {
|
||||||
impressions.computeIfAbsent(impression) { IndexableData(confidence) }
|
impressions.computeIfAbsent(impression) { IndexableData(confidence) }
|
||||||
.also { it.confidence = confidence }
|
.also {
|
||||||
|
it.confidence = confidence
|
||||||
|
if (it.confidence >= 0.9) {
|
||||||
|
featureLock.withLock { features[impression] = it }
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
impressions.remove(impression)
|
impressions.remove(impression)
|
||||||
IndexableData(confidence).also {
|
IndexableData(confidence).also {
|
||||||
impressions[newImpression] = it
|
impressions[newImpression] = it
|
||||||
}
|
|
||||||
}.also {
|
|
||||||
if (it.confidence >= 0.9) {
|
if (it.confidence >= 0.9) {
|
||||||
featureLock.withLock { features[impression] = it }
|
featureLock.withLock { features[newImpression] = it }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateFeature(feature: String, newFeature: String? = null, confidence: Double = 1.0) = featureLock.withLock {
|
fun updateFeature(feature: String, newFeature: String? = null, confidence: Double = 1.0) = featureLock.withLock {
|
||||||
if (newFeature == null) {
|
if (newFeature == null) {
|
||||||
features.computeIfAbsent(feature){ IndexableData(confidence) }
|
features.computeIfAbsent(feature) { IndexableData(confidence) }
|
||||||
.also { it.confidence = confidence }
|
.also { it.confidence = confidence }
|
||||||
}else{
|
} else {
|
||||||
features.remove(feature)
|
features.remove(feature)
|
||||||
IndexableData(confidence).also {
|
IndexableData(confidence).also {
|
||||||
features[newFeature] = it
|
features[newFeature] = it
|
||||||
|
|||||||
Reference in New Issue
Block a user