diff options
author | Arne Juul <arnej@yahooinc.com> | 2023-10-02 11:01:18 +0000 |
---|---|---|
committer | Arne Juul <arnej@yahooinc.com> | 2023-10-02 11:01:18 +0000 |
commit | e24710791f3b0e90b4f7414b68c1eb3f204d2d7c (patch) | |
tree | 53934a796ca339066643dbe9fd9d4e49890733fa /container-search | |
parent | 5595873db0a1114cd9fda1b80266e279b00cbf8e (diff) |
add Normalizer classes
Diffstat (limited to 'container-search')
3 files changed, 94 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/ranking/LinearNormalizer.java b/container-search/src/main/java/com/yahoo/search/ranking/LinearNormalizer.java new file mode 100644 index 00000000000..0f87d0f0b52 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/ranking/LinearNormalizer.java @@ -0,0 +1,31 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.ranking; + +class LinearNormalizer extends Normalizer { + + LinearNormalizer(String name, String input, int maxSize) { + super(name, input, maxSize); + } + + void normalize() { + double min = data[0]; + double max = data[0]; + for (int i = 1; i < size; i++) { + min = Math.min(min, data[i]); + max = Math.max(max, data[i]); + } + min = Math.max(min, -Float.MAX_VALUE); + max = Math.min(max, Float.MAX_VALUE); + double scale = 0.0; + double midpoint = (min + max) * 0.5; + if (max > min) { + scale = 1.0 / (max - min); + } + for (int i = 0; i < size; i++) { + double old = data[i]; + data[i] = 0.5 + scale * (old - midpoint); + } + } + + String normalizing() { return "linear"; } +} diff --git a/container-search/src/main/java/com/yahoo/search/ranking/Normalizer.java b/container-search/src/main/java/com/yahoo/search/ranking/Normalizer.java new file mode 100644 index 00000000000..987065b29f2 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/ranking/Normalizer.java @@ -0,0 +1,31 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.ranking; + +abstract class Normalizer { + + private final String name; + private final String needInput; + protected final double[] data; + protected int size = 0; + + Normalizer(String name, String needInput, int maxSize) { + this.name = name; + this.needInput = needInput; + this.data = new double[maxSize]; + } + + int addInput(double value) { + data[size] = value; + return size++; + } + + double getOutput(int index) { return data[index]; } + + String name() { return name; } + + String input() { return needInput; } + + abstract void normalize(); + + abstract String normalizing(); +} 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..7862e54c32e --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/ranking/ReciprocalRankNormalizer.java @@ -0,0 +1,32 @@ +// Copyright Yahoo. 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(String name, String input, int maxSize, double k) { + super(name, input, 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++) { + temp[i] = new IdxScore(i, data[i]); + } + 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 + "}"; } +} |