Skip to content

Commit

Permalink
Adding precision test.
Browse files Browse the repository at this point in the history
Signed-off-by: jzonthemtn <[email protected]>
  • Loading branch information
jzonthemtn committed Dec 11, 2024
1 parent df5d9a1 commit b9bf8ca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public String getName() {
@Override
public double calculate() {

int numberOfRelevantItems = 0;
double numberOfRelevantItems = 0;

for(final double relevanceScore : relevanceScores) {
if(relevanceScore >= threshold) {
numberOfRelevantItems++;
}
}

return (double) numberOfRelevantItems / (double) k;
return numberOfRelevantItems / (double) k;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public void testCalculate() {
final List<Double> relevanceScores = List.of(1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 0.0);

final NdcgSearchMetric ndcgSearchMetric = new NdcgSearchMetric(k, relevanceScores);
final double dcg = ndcgSearchMetric.calculate();
final double ndcg = ndcgSearchMetric.calculate();

assertEquals(0.7151195094457645, dcg, 0.0);
assertEquals(0.7151195094457645, ndcg, 0.0);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.eval.metrics;

import org.opensearch.test.OpenSearchTestCase;

import java.util.List;

public class PrecisionSearchMetricTest extends OpenSearchTestCase {

public void testCalculate() {

final int k = 5;
final double threshold = 1.0;
final List<Double> relevanceScores = List.of(1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 1.0, 2.0, 3.0, 0.0);

final PrecisionSearchMetric precisionSearchMetric = new PrecisionSearchMetric(k, threshold, relevanceScores);
final double precision = precisionSearchMetric.calculate();

assertEquals(1.8, precision, 0.0);

}

}

0 comments on commit b9bf8ca

Please sign in to comment.