-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
vitrivr-engine-query/src/main/kotlin/org/vitrivr/engine/query/transform/ScoreAggregator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.vitrivr.engine.query.transform | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import org.vitrivr.engine.core.model.retrievable.Retrieved | ||
import org.vitrivr.engine.core.operators.Operator | ||
import org.vitrivr.engine.core.operators.retrieve.Transformer | ||
|
||
class ScoreAggregator( | ||
override val input: Operator<Retrieved>, | ||
private val aggregationMode: AggregationMode = AggregationMode.MAX, | ||
val relationshps: Set<String> = setOf("partOf") | ||
) : Transformer<Retrieved, Retrieved.RetrievedWithScore> { | ||
|
||
enum class AggregationMode { | ||
MAX, | ||
MEAN, | ||
MIN | ||
} | ||
|
||
override fun toFlow(scope: CoroutineScope): Flow<Retrieved.RetrievedWithScore> = | ||
input.toFlow(scope).map { retrieved -> | ||
when { | ||
retrieved is Retrieved.RetrievedWithScore -> retrieved//pass through | ||
retrieved is Retrieved.RetrievedWithRelationship -> { //aggregate | ||
|
||
val scores = | ||
retrieved.relationships.filter { rel -> rel.pred in this.relationshps && rel.obj.first == retrieved.id } | ||
.map { if (it.sub.second is Retrieved.RetrievedWithScore) (it.sub.second as Retrieved.RetrievedWithScore).score else 0f } | ||
|
||
val score = if (scores.isEmpty()) { | ||
0f | ||
} else { | ||
when (aggregationMode) { | ||
AggregationMode.MAX -> scores.max() | ||
AggregationMode.MEAN -> scores.sum() / scores.size | ||
AggregationMode.MIN -> scores.min() | ||
} | ||
} | ||
|
||
Retrieved.PlusScore(retrieved, score) | ||
|
||
} | ||
|
||
else -> Retrieved.PlusScore(retrieved, 0f) | ||
|
||
} | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...engine-query/src/main/kotlin/org/vitrivr/engine/query/transform/ScoreAggregatorFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.vitrivr.engine.query.transform | ||
|
||
import org.vitrivr.engine.core.model.metamodel.Schema | ||
import org.vitrivr.engine.core.model.retrievable.Retrieved | ||
import org.vitrivr.engine.core.operators.Operator | ||
import org.vitrivr.engine.core.operators.retrieve.Transformer | ||
import org.vitrivr.engine.core.operators.retrieve.TransformerFactory | ||
|
||
class ScoreAggregatorFactory : TransformerFactory<Retrieved, Retrieved.RetrievedWithScore> { | ||
override fun newTransformer( | ||
input: Operator<Retrieved>, | ||
schema: Schema, | ||
properties: Map<String, String> | ||
): Transformer<Retrieved, Retrieved.RetrievedWithScore> { | ||
|
||
val aggregation = properties["aggregation"]?.uppercase()?.let { | ||
try { | ||
ScoreAggregator.AggregationMode.valueOf(it) | ||
} catch (e: IllegalArgumentException) { | ||
null | ||
} | ||
} ?: ScoreAggregator.AggregationMode.MAX | ||
|
||
val relationships = properties["relationships"]?.split(",")?.map { s -> s.trim() }?.toSet() ?: setOf("partOf") | ||
|
||
return ScoreAggregator(input, aggregation, relationships) | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...resources/META-INF/services/org.vitrivr.engine.core.operators.retrieve.TransformerFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
org.vitrivr.engine.query.transform.RelationExpanderFactory | ||
org.vitrivr.engine.query.transform.RelationExpanderFactory | ||
org.vitrivr.engine.query.transform.ScoreAggregatorFactory |