|
Lucene example source code file (QualityStats.java)
The Lucene QualityStats.java source code
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.benchmark.quality;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.ArrayList;
/**
* Results of quality benchmark run for a single query or for a set of queries.
*/
public class QualityStats {
/** Number of points for which precision is computed. */
public static final int MAX_POINTS = 20;
private double maxGoodPoints;
private double recall;
private double pAt[];
private double pReleventSum = 0;
private double numPoints = 0;
private double numGoodPoints = 0;
private double mrr = 0;
private long searchTime;
private long docNamesExtractTime;
/**
* A certain rank in which a relevant doc was found.
*/
public static class RecallPoint {
private int rank;
private double recall;
private RecallPoint(int rank, double recall) {
this.rank = rank;
this.recall = recall;
}
/** Returns the rank: where on the list of returned docs this relevant doc appeared. */
public int getRank() {
return rank;
}
/** Returns the recall: how many relevant docs were returned up to this point, inclusive. */
public double getRecall() {
return recall;
}
}
private ArrayList<RecallPoint> recallPoints;
/**
* Construct a QualityStats object with anticipated maximal number of relevant hits.
* @param maxGoodPoints maximal possible relevant hits.
*/
public QualityStats(double maxGoodPoints, long searchTime) {
this.maxGoodPoints = maxGoodPoints;
this.searchTime = searchTime;
this.recallPoints = new ArrayList<RecallPoint>();
pAt = new double[MAX_POINTS+1]; // pAt[0] unused.
}
/**
* Add a (possibly relevant) doc.
* @param n rank of the added doc (its ordinal position within the query results).
* @param isRelevant true if the added doc is relevant, false otherwise.
*/
public void addResult(int n, boolean isRelevant, long docNameExtractTime) {
if (Math.abs(numPoints+1 - n) > 1E-6) {
throw new IllegalArgumentException("point "+n+" illegal after "+numPoints+" points!");
}
if (isRelevant) {
numGoodPoints+=1;
recallPoints.add(new RecallPoint(n,numGoodPoints));
if (recallPoints.size()==1 && n<=5) { // first point, but only within 5 top scores.
mrr = 1.0 / n;
}
}
numPoints = n;
double p = numGoodPoints / numPoints;
if (isRelevant) {
pReleventSum += p;
}
if (n<pAt.length) {
pAt[n] = p;
}
recall = maxGoodPoints<=0 ? p : numGoodPoints/maxGoodPoints;
docNamesExtractTime += docNameExtractTime;
}
/**
* Return the precision at rank n:
* |{relevant hits within first <code>n hits}| /
Other Lucene examples (source code examples)Here is a short list of links related to this Lucene QualityStats.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.