Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added max/min Scores to Pecentage Heuristic #26027

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@


import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -33,15 +34,28 @@
public class PercentageScore extends SignificanceHeuristic {
public static final String NAME = "percentage";

public PercentageScore() {
private static final ParseField MAX_SCORE = new ParseField("max_score");
private static final ParseField MIN_SCORE = new ParseField("min_score");
private final double maxScore;
private final double minScore;

public PercentageScore(double minScore,double maxScore) {
this.minScore = minScore;
this.maxScore = maxScore;
}

public PercentageScore(StreamInput in) {
// Nothing to read.
public PercentageScore() {
this(0,1);
}
public PercentageScore(StreamInput in) throws IOException{
minScore = in.readLong();
maxScore = in.readLong();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeDouble(minScore);
out.writeDouble(maxScore);
}

@Override
Expand All @@ -51,17 +65,39 @@ public String getWriteableName() {

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME).endObject();
builder.startObject(NAME)
.field(MIN_SCORE.getPreferredName(), minScore)
.field(MAX_SCORE.getPreferredName(), maxScore)
.endObject();
return builder;
}

public double getMinScore() {
return minScore;
}

public double getMaxScore() {
return maxScore;
}

public static SignificanceHeuristic parse(XContentParser parser)
throws IOException, QueryShardException {
// move to the closing bracket
if (!parser.nextToken().equals(XContentParser.Token.END_OBJECT)) {
throw new ElasticsearchParseException("failed to parse [percentage] significance heuristic. expected an empty object, but got [{}] instead", parser.currentToken());
double minScore = 0;
double maxScore = 1;
XContentParser.Token token = parser.nextToken();
while (!token.equals(XContentParser.Token.END_OBJECT)) {
if (MIN_SCORE.match(parser.currentName())) {
parser.nextToken();
minScore = parser.doubleValue();
} else if (MAX_SCORE.match(parser.currentName())) {
parser.nextToken();
maxScore = parser.doubleValue();
} else {
throw new ElasticsearchParseException("failed to parse percent heuristic. unknown field [{}]", parser.currentName());
}
token = parser.nextToken();
}
return new PercentageScore();
return new PercentageScore(minScore,maxScore);
}

/**
Expand All @@ -75,7 +111,11 @@ public double getScore(long subsetFreq, long subsetSize, long supersetFreq, long
// avoid a divide by zero issue
return 0;
}
return (double) subsetFreq / (double) supersetFreq;
double score = (double) subsetFreq / (double) supersetFreq;
if ( score >= minScore && score <= maxScore) {
return score;
}
return 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,39 @@ public void testAssertions() throws Exception {
testBackgroundAssertions(new MutualInformation(true, true), new MutualInformation(true, false));
testBackgroundAssertions(new ChiSquare(true, true), new ChiSquare(true, false));
testBackgroundAssertions(new GND(true), new GND(false));
testAssertions(new PercentageScore());
testAssertions(new PercentageScore(0,1));
testAssertions(new JLHScore());
}

public void testBasicScoreProperties() {
basicScoreProperties(new JLHScore(), true);
basicScoreProperties(new GND(true), true);
basicScoreProperties(new PercentageScore(), true);
basicScoreProperties(new PercentageScore(0, 1), true);
basicScoreProperties(new MutualInformation(true, true), false);
basicScoreProperties(new ChiSquare(true, true), false);
}

public void testPercentScoreProperties() {
SignificanceHeuristic heuristic = new PercentageScore(0,0);
assertThat(heuristic.getScore(1, 1, 1, 3), equalTo(0.0));
heuristic = new PercentageScore(1,1);
assertThat(heuristic.getScore(1, 1, 2, 3), equalTo(0.0));
assertThat(heuristic.getScore(2, 2, 2, 10), equalTo(1.0));

}

public void testPercentHeuristicParse() throws IOException {
SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList());
ParseFieldRegistry<SignificanceHeuristicParser> significanceHeuristicParserRegistry = searchModule.getSignificanceHeuristicParserRegistry();
PercentageScore percentageScore = (PercentageScore) parseFromString(significanceHeuristicParserRegistry,"\"percentage\" : { \"min_score\" : 0.0 , \"max_score\" : 1.0 }");
assertThat(percentageScore.getMaxScore(), equalTo(1.0));
assertThat(percentageScore.getMinScore(), equalTo(0.0));
percentageScore = (PercentageScore) parseFromString(significanceHeuristicParserRegistry,"\"percentage\" : {}");
assertThat(percentageScore.getMaxScore(), equalTo(1.0));
assertThat(percentageScore.getMinScore(), equalTo(0.0));
}


public void basicScoreProperties(SignificanceHeuristic heuristic, boolean test0) {
assertThat(heuristic.getScore(1, 1, 1, 3), greaterThan(0.0));
assertThat(heuristic.getScore(1, 1, 2, 3), lessThan(heuristic.getScore(1, 1, 1, 3)));
Expand Down