Skip to content

Commit

Permalink
Separating out the metrics.
Browse files Browse the repository at this point in the history
Signed-off-by: jzonthemtn <[email protected]>
  • Loading branch information
jzonthemtn committed Dec 5, 2024
1 parent cec6de1 commit 8d80e2e
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 177 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 java.util.List;

public class DcgSearchMetric extends SearchMetric {

protected final List<Double> relevanceScores;

public DcgSearchMetric(final int k, final List<Double> relevanceScores) {
super(k);
this.relevanceScores = relevanceScores;
}

@Override
public String getName() {
return "dcg_at_" + k;
}

@Override
public double calculate() {

double dcg = 0.0;
for(int i = 0; i < relevanceScores.size(); i++) {
double relevance = relevanceScores.get(i);
dcg += relevance / Math.log(i + 2); // Add 2 to avoid log(1) = 0
}
return dcg;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 java.util.List;

public class NdcgSearchMetric extends DcgSearchMetric {

private final List<Double> idealRelevanceScores;

public NdcgSearchMetric(final int k, final List<Double> relevanceScores, final List<Double> idealRelevanceScores) {
super(k, relevanceScores);
this.idealRelevanceScores = idealRelevanceScores;
}

@Override
public String getName() {
return "ndcg_at_" + k;
}

@Override
public double calculate() {

double dcg = super.calculate();

double idcg = 0.0;
for(int i = 0; i < idealRelevanceScores.size(); i++) {
double relevance = idealRelevanceScores.get(i);
idcg += relevance / Math.log(i + 2); // Add 2 to avoid log(1) = 0
}

if(idcg == 0) {
return 0;
}

return dcg / idcg;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 java.util.List;

public class PrecisionSearchMetric extends SearchMetric {

private final List<Double> relevanceScores;

public PrecisionSearchMetric(final int k, final List<Double> relevanceScores) {
super(k);
this.relevanceScores = relevanceScores;
}

@Override
public String getName() {
return "precision_at_" + k;
}

@Override
public double calculate() {

// TODO: Implement this.
return 0.0;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public abstract class SearchMetric {

private static final Logger LOGGER = LogManager.getLogger(SearchMetric.class);

protected int k;

public abstract String getName();

public abstract double calculate();

public SearchMetric(final int k) {
this.k = k;
}

public int getK() {
return k;
}

}

This file was deleted.

Loading

0 comments on commit 8d80e2e

Please sign in to comment.