diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/ranking/ReciprocalRankNormalizer.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/ranking/ReciprocalRankNormalizer.java | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/ranking/ReciprocalRankNormalizer.java b/container-search/src/main/java/com/yahoo/search/ranking/ReciprocalRankNormalizer.java new file mode 100644 index 00000000000..fca920a7a65 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/ranking/ReciprocalRankNormalizer.java @@ -0,0 +1,34 @@ +// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.ranking; + +import java.util.Arrays; + +class ReciprocalRankNormalizer extends Normalizer { + + private final double k; + + ReciprocalRankNormalizer(int maxSize, double k) { + super(maxSize); + this.k = k; + } + + static record IdxScore(int index, double score) {} + + void normalize() { + if (size < 1) return; + IdxScore[] temp = new IdxScore[size]; + for (int i = 0; i < size; i++) { + double val = data[i]; + if (Double.isNaN(val)) val = Double.NEGATIVE_INFINITY; + temp[i] = new IdxScore(i, val); + } + Arrays.sort(temp, (a, b) -> Double.compare(b.score, a.score)); + for (int i = 0; i < size; i++) { + int idx = temp[i].index; + double old = data[idx]; + data[idx] = 1.0 / (k + 1.0 + i); + } + } + + String normalizing() { return "reciprocal-rank{k:" + k + "}"; } +} |